Linux cut, awk, grep, sed

Source: Internet
Author: User

Cut is a selection command that analyzes a piece of data and takes out what we want. In general, the selection of information is usually for "line" for analysis, not the entire information analysis

Cut [-bn] [file] or cut [-c] [file] or cut [-DF] [file]

The UT command cuts bytes, characters, and fields from each line of the file and writes those bytes, characters, and fields to standard output.
If you do not specify a File parameter, the Cut command reads standard input. One of the-B,-C, or-f flags must be specified.

Main parameters
-B:
split in bytes. These byte locations will ignore multibyte character boundaries unless the-n flag is also specified.
- C: split in characters.
- D: custom delimiter, default is tab.
- F: used with-D to specify which area to display.
- N: cancels splitting multibyte characters. Used only with the-B flag. If the last byte of the character falls within the range of <br/> indicated by the List parameter of the-B flag, the character will be written out; otherwise, the character will be excluded.

The cut command mainly accepts three positioning methods:

First, byte (bytes), with option-B

Second, character (characters), with option-C

Third, domain (fields), with option-f

-B supports the notation of form 3-5, and multiple positions are separated by commas.

Who|cut-b 3-5,8

Cat/etc/passwd|head-n 5|cut-d:-F 1

Use-D to set the delimiter as a colon, and then use-F to set the first domain I want to take, and then press ENTER, all the usernames are listed.

Awk

awk usage: awk ' pattern {action} ' awk is for each row, so it is hard escaped in the unit of behavior, "soft escape" can only escape some meta-characters, \ can escape some metacharacters and wildcards

variable name meaning
ARGC Command Line arguments
ARGV command-line array of elements
FileName current Input file name
FNR the record number in the current file
FSEnter the field delimiter, which defaults to a space
RS Input Record delimiter
NF number of fields in the current record
NR Record Count so far
OFSOutput field delimiter
ORS Output Record delimiter

1. The awk '/101/' file displays a matching line containing 101 of the files.
awk '/101/,/105/' file
awk ' $ = = 5 ' file
awk ' $ = = ' CT ' file note must be enclosed with double quotes
Awk ' $ * $ >100 ' file
awk ' $ >5 && $2<=15 ' file


2. awk ' {print nr,nf,$1, $NF,} ' file displays the current record number, number of fields, and the first and last fields of each row of the files.
awk '/101/{print $1,$2 + ten} ' file displays the first to second field plus 10 of the matching line of the files.
awk '/101/{print $1$2} ' file
awk '/101/{print $ A} ' file displays the first to second field of a matching row of file files, but there is no delimiter in the middle of the display.


3, DF | awk ' $4>1000000 ' obtains input through pipe breaks, such as: Displays rows where the 4th field satisfies a condition.


4, awk -f  "|"   ' {print $1} '    file         according to the new delimiter ' | ' to operate.  
   awk   ' begin { fs= "[:  \t|]"  } 
   {print $1,$2,$3} '        file          by setting the input delimiter (fs= "[:  \t|]" ) to modify the input delimiter.  

   sep= "|" &NBSP
   awk -F  $Sep   ' {print $1} '   file   According to the environment variable Sep value as the delimiter.     
   awk -F  ' [ :\t|] '   ' {print $1} '  file   is delimited by the value of a regular expression, which represents a space,:, TAB, | And as a delimiter. &NBSP
   awk -F  ' [] '      ' {print $1} '  file    follows the value of the regular expression as a delimiter, which represents [,]


5, awk-f awkfile file through the contents of awkfile files in turn control.
Cat Awkfile
/101/{print "\047 hello! \047 "}--print ' hello! after encountering a matching line  ‘. \047 represents single quotation marks .
{Print $1,$2}-Prints the first two fields of each row because there is no mode control.


6. awk ' $ ~/101/{print $} ' file displays the row (record) of the first field in the document that matches 101.


7. awk ' BEGIN {ofs= '% '}
{print $1,$2} ' file modifies the output format by setting the output delimiter (ofs= "%").


8, awk    ' begin { max=100 ;p rint  "max="  max}             BEGIN  Represents an operation that is performed before any rows are processed.  
   {max= ($1 >max ?$1:max);  print $1, "Now max is   "Max}"  file           gets the maximum value of the first field in the file.  
    expression 1? expression 2: Expression 3  equivalent to:  
   if  (expression 1)  
        expression 2 
   else 
         expression 3 
   awk  ' {print  ($1>4 ?  high  "$1: " low  "$"} '  file 


9. awk ' $ * $ >100 {print $} ' file displays the first field in the file that matches 101 of the row (record).


10. awk ' {$ = = ' Chi ' {$ = ' China '; print} ' file replaces the 3rd field and then displays the row (record) after it finds a matching row.
awk ' {$7%= 3; print $7} ' file will divide the 7th field by 3 and assign the remainder to the 7th domain and reprint.


11. awk '/tom/{wage=$2+$3; printf wage} ' file finds a matching row and assigns a value to the variable wage and prints the variable.


12. awk '/tom/{count++;}
End {print "Tom was found" count "Times"} ' file end means that processing is done after all input lines have been processed.


13. awk ' Gsub (/\$/, ""), Gsub (/,/, ""); cost+=$4;
END {print "The total is $" cost> "filename"} ' file Gsub function replaces $ with an empty string and outputs the result to filename.
1 2 3 $1,200.00
1 2 3 $2,300.00
1 2 3 $4,000.00

awk ' {gsub (/\$/, ""); Gsub (/,/, "");
if ($4>1000&&$4<2000) c1+=$4;
else if ($4>2000&&$4<3000) c2+=$4;
else if ($4>3000&&$4<4000) c3+=$4;
else c4+=$4; }
END {printf "c1=[%d];c2=[%d];c3=[%d];c4=[%d]\n", c1,c2,c3,c4} "' File
Completion of conditional statements by if and else if

awk ' {gsub (/\$/, ""); Gsub (/,/, "");
if ($4>3000&&$4<4000) exit;
else c4+=$4; }
END {printf "c1=[%d];c2=[%d];c3=[%d];c4=[%d]\n", c1,c2,c3,c4} "' File
Exits on a condition by exit, but the end operation is still performed.
awk ' {gsub (/\$/, ""); Gsub (/,/, "");
if ($4>3000) next;
else c4+=$4; }
END {printf "c4=[%d]\n", C4} "' File
The row is skipped on a condition by next, and the action is performed on the next line.


14, awk ' {print filename,$0} ' file1 file2 file3>fileall file1, file2, file3 file contents are written in Fileall, in the format of
Prints the file and the predecessor file name.


15. awk ' $1!=previous {close (previous); previous=$1}
{Print substr ($0,index, "" ") + 1) >$1} ' Fileall re-split the merged file into 3 files. and consistent with the original document.


16. awk ' BEGIN {' Date ' |  Getline D; Print D} ' sends the execution result of date to getlineby pipe, assigns the variable d, and prints.


17. awk ' BEGIN {System ("echo \" Input your Name:\\c\ "");GetlineD;print "\nyour name is", D, "\b!\n"} '
PassGetlineThe command interactively enters name and displays it.
awk ' BEGIN {fs= ': "; WhileGetline< "/etc/passwd" >0) {if ($1~ "050[0-9]_") print "}} '
Print the user name in the/etc/passwd file that contains the 050x_ user name.

18. awk ' {i=1;while (I&LT;NF) {print NF, $i; i++}} ' file implements looping through a while statement.
awk ' {for (i=1;i<nf;i++) {print NF, $i}} ' file implements loops through a for statement.
Type file|awk-f "/" '
{for (i=1;i<nf;i++)
{if (i==nf-1) {printf "%s", $i}
else {printf "%s/", $i}} ' shows the full path of a file.
Display dates with for and if
awk ' BEGIN {
for (j=1;j<=12;j++)
{flag=0;
printf "\n%d month \ n", J;
for (i=1;i<=31;i++)
{
if (j==2&&i>28) flag=1;
if (j==4| | j==6| | j==9| | j==11) &&i>30) flag=1;
if (flag==0) {printf "%02d%02d", j,i}
}
}
}‘


19. Calling system variables in awk must be in single quotation marks, or in double quotes, to indicate a string
Flag=abcd
awk ' {print ' $Flag '} ' result is ABCD
awk ' {print ' $Flag '} ' result is $flag

===============================

$awk ' begin{total=0}{total+=$4}end{print total} ' a.txt-----sum the fourth field of the A.txt file!

    • $ awk '/^ (no|so)/' test     -----Print all lines that begin with mode no or so.

    • $ awk '/^[ns]/{print $ ' test      -----Print this record if the record starts with N or S.

    • $ awk ' $ ~/[0-9][0-9]$/(print $ ' test        ----- This record is printed if the first field ends with two digits.

    • $ awk ' $ = = | | $ < test     -----If the first or equal to 100 or the second field is less than 50, the line is printed.

    • $ awk ' $ = test   -----If the first field is not equal to 10, print the line.

    • $ awk '/test/{print $ test -----If the record contains a regular expression test, the first field is added 10 and printed out.

    • $ awk ' {print ($ > 5? "OK" $: "Error" ($)} ' test  -----If the first field is greater than 5, the expression value after the question mark is printed, otherwise the expression value after the colon is printed.

    • $ awk '/^root/,/^mysql/' test       ---- Prints all records in the range of records that begin with the regular expression "root" to the record beginning with the regular expressions. If a record of the beginning of a new regular expression root is found, continue printing until the next record begins with the regular expression MySQL, or to the end of the file.

Linux cut, awk, grep, sed

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.