awk
, grep
, sed
Are linux
three weapon manipulate text, but also must master linux
one command. Three functions are working with text, but the focus is different, which is a awk
most powerful but also the most complex. grep
Or simply to find more suitable for matching text, sed
is more suitable for matching to edit the text, awk
is more suitable for formatted text, the text is more complex formatting.
All of the following experiments output, are test file test.log
contents as a benchmark:
20170102 admin,password Open 20170801 nmask,nmask close 20180902 nm4k,test filter
AWK
Is a language processing text files, text analysis is a powerful tool; awk
is listed as the division count, $0
represents all the columns, $1
the first column, $2
the second column.
awk parameters
-F
Input file specifies off separators, such as-F
:-v
Assigning a user-defined variable, such as-va=1
-f
Read from a script fileawk
command
NOTE: only lists the most common parameters
Separator
Dividing each row by row space, and outputs the first and the third column
Awk $ '$ {Print. 1,. 3} $ ' the test.log
# or $ CAT the test.log | awk '{. 1 Print $, $}. 3 '
Custom delimiter
Use "," segmentation, parameters -F
awk -F, '{print $1,$2}' test.log
A plurality of separators, first dividing spaces, and then dividing the result re-use "," split
Awk -F $ '[,] ' '. 1 {Print $, $ 2, $}. 3 ' the test.log have a space before the comma Note #