sed & awk & grep topic (reproduced)

grep, sed and awk are quite useful!

gerp finds, sed edits, and awk analyzes and processes according to the content.



awk (keyword: analysis & processing) line-by-line analysis and processing awk 'condition type 1 { action 1 } condition type 2 { action 2 }' filename, awk can also read the standard input from the previous instruction.
Compared with sed, it is often used to process a whole line. Awk is more inclined to divide a line into several "fields" (areas) to process, the default separator It is the space bar or the tab key.
For example :
last -n 5 | awk '{print $1 "\t" $3}' Here, no space between $1"\t"$3 in the curly brackets can be added, but it is better to add a Space, and also note that "\t" has double quotation marks, because these contents are all within single quotation marks.
$0 represents the entire line $1 represents the first area, and so on
. The processing flow of awk is:
1. Read the first line, Fill the first line of data into variables such as $0, $1...
2. According to the conditions, execute the action
3. Then execute the next line.
Therefore , AWK processes one line at a time, and the smallest unit processed in one time is an area
There are also 3 variables, NF: the number of fields processed in each line, NR is currently processed to which line FS is the current separator
Logical judgment> < >= <= == !== , directly use = for assignment
cat /etc/passwd | awk '{FS=":"} $3<10 {print $1 "\t" $3}' First define the delimiter as:, then judge, pay attention, judge that it is not written in {}, then Execute an action, FS=":" This is an action, an assignment action, not a judgment, so it is not written in {}
BEGIN END, giving the programmer an initialization and finishing work, the operations listed after BEGIN are in {} Will be executed before awk starts scanning the input, and the operations in END{} will be executed after scanning the input file.
awk '/test/ {print NR}' abc prints the line number of the line with test, note //You can use regular expressions between
awk {}, you can use if else, for(i=0;i<10;i++), i=1 while(i<NF)
can be seen, many uses of awk are equivalent to C language, such as "\t" delimiter, print format, if, while, for, etc.

awk is quite a complex tool. When it is actually used, please add it. (picture about tools)



sed(keyword: edit) The text editing tool sed in line units can directly modify the file, but this is generally not recommended. You can analyze the
basic working method of standard input: sed [-nef] '[action]' [input text]
-n : quiet mode, general sed In usage, the data from stdin is generally listed on the screen. If the -n parameter is used, only the line processed by sed is listed.
-e : Multiple editing, for example, if you want to delete a line at the same time, and want to change other lines, Then you can use sed -e '1,5d' -e 's/abc/xxx/g' filename
-f : First write the sed action in a file, and then through sed -f scriptfile you can directly execute the sed action in the scriptfile (no experiment is successful, not recommended)
-i : Direct editing, this time is the real change The contents of the file are gone, and everything else is just to change the display. (Deprecated)
Action:
a new, a can be followed by a string, and the string will appear on a new line. (next line)
c replace, c The following strings, these strings can replace the line d between n1 and n2 to be
deleted, and nothing is
followed by i to insert, the latter strings will appear on the previous line.
P will be printed, the selected data will be listed, usually and sed -n work together sed -n '3p' print only the 3rd line
s replace, similar to the replacement in vi, 1,20s/old/new/g

[line-address]q exit, match a line to exit, improve efficiency

[line-address]r The matched line reads a file, for example: sed '1r qqq' abc , note that the written text is written after the first line, which is the second line

[line-address]w file, write the matched line to a file, for example: sed -n '/m/w qqq' abc , read the line with m from abc and write it to the qqq file, note that this writing is overwritten. For


example :
sed '1d' abc delete the first line in the abc file, note that all lines except the first line will be displayed at this time, because the first line has been deleted (the actual file is not deleted, but only displayed was deleted)
sed -n '1d' abc does not display anything, because the line processed by sed is a delete operation, so it is not realistic.
sed '2,$d' abc deletes all the content from the second line to the last line in abc, Note that the $ sign in the regular expression indicates the end of the line, but it does not say the end of the line, it will refer to the end of the last line, starting with ^, if the beginning of the line is not specified, then it is the beginning of the first line
sed '$d' abc Only the last line is deleted, because the end of that line is not specified, it is considered to be the end of the last line
sed '/test/d' abc All lines with test in the file are deleted, all
sed '/test/a RRRRRRR' abc will be RRRRRRR It is also possible to append to the next line
with all lines with test. It is also possible to replace all lines with test with RRRRRRR through the line sed '1,5c RRRRRRR' abc sed '/test/c RRRRRRR' abc, of course, it can also be done by line Replacement, such as sed '1,5c RRRRRRR' abc



grep (keyword: interception) Text collection tool, very powerful in combination with regular expressions
Main parameters []
-c : only output matching lines
-I : case insensitive
-h : Do not display file names when
querying multiple files -l : When querying multiple files, only output file names that contain matching characters
-n : Display matching line numbers and lines
-v : Display all lines that do not contain matching text (I often use remove grep itself)
basically works: grep the content filename to match, for example:
grep 'test' d* show all lines containing test in files starting with d
grep 'test' aa bb cc show lines containing test in aa bb cc files
grep '[az]\{5}\' aa show all A string containing at least 5 consecutive lowercase letters

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326222543&siteId=291194637