"Find and Awk Linux"

Source: Internet
Author: User

One, find command find

Exact lookups, which are searched according to the conditions or combination conditions provided, traverse all files, and are therefore slower.

Grammar:

Find Directory Condition action

The default directory is the current directory
The default condition is all conditions
The default action is to display the found information

Conditions (options):

-name filename: Exact match, lookup according to file name, wildcard characters supported-user usename: Find files based on the owner of the file-Group GROUPNAME: Find files based on the genus Group of files-Nouser: Finding files that do not correspond to a master user-Nogroup: Finding files with no corresponding genus-A: With-o: or-not: Non-Find/-not \ (-namepasswd-A-user root \)-inum N: View all filenames with inode number n time unit of day-atime +|-N Access Time-ctime +|-N Change Time-mtime +|-n Modify time unit to minutes-amin +|-N-cmin +|-N-mmin +|-N+Ten: 10 days ago-Ten: Within 10 days-mtime + -: Modified 60 days ago-mtime- -: Modified in 60 days-mtime -: 60 changes before the same time-Type C File is of type c:b block (buffered) Special C character (UN Buffered) Special D directory p named pipe (FIFO) F regularfilel symbolic link; This is nevertrue ifThe-l option or The-follow option isincheffect, unless the symbolic link is broken. If you want to search forSymbolic links when-L isincheffect, use-xtype. S socket-maxdepth2: Finding a Level two subdirectory (top of the list)-perm (permissions)-mode (with): Find all permissions must meet the mode condition of the file/mode (OR): Finds at least one permission bit that satisfies the mode conditionFind/tmp-perm-222(three conditions must be available at the same time)Find/tmp-perm/222(One of the conditions is available)

Action:

   -print: Show    -exec: Executes the corresponding command, without asking the user      {}: The file name   found by the Find command -exec command{} \;    -OK: Execute the corresponding command. Ask   the user before executing -ls: Displays the found file with the Ls-l command after performing a lookup

Find Practice

  1FindThe var directory is subordinate to root, which belongs to all files in the group mail;Find/var-user Root-a-Group Mail2Findfiles in the USR directory that are not root, bin, Qhdlink, and other users; Useradd QhdlinkFind/usr-not \ (-user root-a-user bin-a-user qhdlink \)3Findthe files that have been modified in the last week in the ETC directory and are not owned by the root and student users;Find/etc-mtime-7-a-not-user root-a-not-User Student4) Find files on the current system that are not owned by the master or group and have been accessed in the last 1 days, and change their genus to root;Find/-nouser-o-nogroup-a-atime-1-execChownroot: {} \; 5Findall users in the ETC directory do not have permission to write files, showing their details;Find/etc-not-perm/222-ls

Second, awk

Awk is a very powerful language that can handle data and produce formatted reports. AWK works by reading data files, thinking each row of data as a record (record), dividing each record into fields with field separators, and then outputting the values for each field.

The following is the output fragment that performs PS Auxw:

USER PID%cpu%MEM VSZ RSS TTY
STAT START Time COMMAND
Root 1 0.0 0.0 164 92?
Ss Apr09 0:01 init [5]

Data like this fixed structure, which is handled with AWK, is particularly powerful, and is usually done in just a few lines of program code.

For example, the PID of all trips can be obtained using only the following single instruction:

PS Auxw | awk ' {print $} '
So, how does awk handle every record?

Awk applies a "style {action}" to each record, and executes the specified action if the row conforms to the style. One of the styles or actions can be omitted. If there is only a style, indicates that you want to display a data row that conforms to the style, or if only the action is performed on each data row.

Grammar:

awk " style " file: Displays the data rows that match the style.  awk'{action}'  file: Performs the action in {} on each row.  awk' style {action}' file: Performs the action in {} on a data row that conforms to the style.

Usage

usage of awk 1:awk '/la/'DATAF3 displays data rows that contain LA. Usage of awk 2:awk '{print $, $ $}'DATAF3 Displays the 1th and 2nd fields of each row of the DATAF3. $ $ for the 1th field, and $ for the second field, and other analogy. Usage of awk 3:awk '/la/{print $, $/$}'DATAF3 Displays the 1th and 2nd fields of the data row containing the LA keyword. Usage of awk 4:awk-F:'/^ols3/{print $ $, $4}'/etc/passwdUse Options-F, specify: For delimited characters, account ols3 uid (3rd field) and GID (4th field) are displayed. Usage of awk 5:awk-F:'begin{ofs= "+ + +"}/^ols3/{print $,$2, $3, $4, $5}'/etc/passwdTo: For the delimiter character, + + + for the output field delimiter, the account OLS3 the first 1~5 columns are displayed. Execution Result: OLS3+++x+++1002+++1002+++in this example, the begin{} area instructs Awk to initialize the operation first, which is to set the OFS="+++"。 The function of the variable OFS is to store the delimiter for the output field. Next, look for OLS3 's account line, and when found, use Print to print out the 1th to 5th fields, separated from each other by + +.

Instance

Obtain the IP of the NIC:ifconfig|grep 'inet Addr:'|grepBcast |awk '{print $}'|awk-F:'{print $}'Get network device name:Cat/proc/net/dev |awk-F:'/eth.:| ppp.:| wlan.:/ {Print $}'in this case,-F: Subscript character the separator character to:, and, with the style of multiple selection one/eth.:| ppp.:| wlan.:/. The meaning of this style is: The device name can be eth0:, Ppp1:, Wlan1: One of these 3. Once you have found a string that conforms to the style, remove:, take the first field value, so the possible answer is eth0 or Ppp1 or wlan1.

Get system memory Size: Cat awk ' /memtotal/{print $} ' /proc/Meminfo records the host memory-related data, where memtotal is the memory size, the sample values are       as follows: memtotal:223128  KB Therefore, In Awk's style syntax, using /memtotal/to find this line, and then print the second field, you can get the size of the memory.

Modify the order of the fields in the CSV file: The following is the data file dataf6.csv, want to replace the 2nd field and the 4th field: The township, school name, school website, principal name, school phone, VoIP three yards, school address New Ying, South New Country, http://www.ns12jh.tnc.edu.tw,ABC,06-656313012,1021, no. No. 6675, Xin Ying Public roadCari, Chiali, http://www.jl41jh.tnc.edu.tw,NOP,06-722224432,1146, No. No. 5523, Cari Annan RoadNew Ying, Shin-ying Country Small, http://www.sy53es.tnc.edu.tw,DEF,06-632213642,1482, No. No. 3248, New Ying Rd .practice is as follows: Example --3-1: Chcsv24.SH on. #! /bin/Bash Geneva. Geneva. tmpf='/tmp/tmp.$$' Geneva.CatDataf6.csv |awk-F,'begin{ofs= ","}{print $1,$4,$3,$2,$5,$6,$7}'> $TMPF to.MV-F $TMPF dataf6.csv Line 3, set the temporary file name. Line 4, the contents of the data file are piped to awk for processing. The field-delimited and output-delimited characters of awk are set to,. In the operation of {}, change the order of both the $ and $4, and then turn the results to store in the temporary file. Line 5, overwrite the temporary file with the original file. 

"Find and Awk Linux"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.