Sed and awk common command examples

Source: Internet
Author: User

process file line processing for a row of sed loops
sed-n '/abc/p ' file will print the contents of the mode space by default
line matches the ABC print out-R supports extended delimiters
- N does not let the default print mode space
sed [email protected] sed ' s/abc/def ' file after remote login to modify the contents of file
sed-n ' 5p ' file prints the fifth line
sed-n ' $p ' file prints the last line
sed-n ' 1,3p ' file print one to three lines
sed-n ' 1,~3p ' file prints three lines from the first line
sed-i ' 3i aaaaaaa ' file in front of the third row plus aaaaaaa
sed-i ' $a aaaaa ' file joins AAAAA on the last line
sed-i '/^tom/i aaaaaa ' file adds aaaaaa to the line preceded by Tom
sed-i ' s/abc/def/' file change the first ABC of each line into Def
sed-i ' s/abc/def/g ' file change all ABC into def for each line
sed-i ' s/abc/def/3 ' file change the third ABC of each line into Def
sed-i ' 1,2s/abc/def/' file change one to two lines ABC into def
s swap matching fields C for matching rows
sed-i ' 1,2s/^.*$/def/' file change one to two lines into Def
sed-i ' 4c aaaaaaaaa ' file changes the fourth line to AAAAAAAA
sed-i '/^selinux/c selinux ' changes all rows starting with SELinux to SELinux
sed-i ' 4d ' to remove line fourth
sed-i '/^tom/d ' file delete all lines starting with Tom
sed-i '/^$/d ' file to delete empty lines
sed-i '/^\s$/d ' file delete starts with a blank line \s represents a space or tab
sed-i '/^abc/s/tom/jerry/' file first finds the line that starts with ABC change Tom to Jerry
sed-i '/^mike/s/^/# ' file notes the Mike line
sed-n ' 1p; 3p ' file prints first and third lines
sed-i-E ' 1s/jerry/tom/; -e 3s/mike/xyz/' file multiple strips are executed with-e
The sed-i '/abc/,/^def/s/123/456/' file changes the line between lines starting with ABC and the line of Def to 456
sed-n ' 1,/def/p ' file is printed from the first line until it touches the Def line
#------------------------------------------------------------------------
awk Line-by-row field Loop command
The default is to print the entire line by default with a space as a separator $0-f the separator
awk ' begin{print ' aaaa "}/tom/{print $2}end{print" bbbbbbb "} ' file1first, execute the statement in begin to read from the file whether the first row matches/tom if there is no execution will not be executed until after reading, then run End statement can choose the begin match the ENG.
awk '/^mike/{print} ' file
awk ' nr==2{print ' file NR is the number of line number NF fields
awk ' nr==2| | Nr==4{print "file reads only two fixed line numbers
awk ' {print $} ' file $ entire row
awk ' bengin{fs= ': "ofs=" "}/bash$/{print $} ' file FS specifies a delimiter: OFS specifies the field delimiter for the output
TR ":" "" to change a colon to a space
awk ' begin{i=10;echo$i} '
awk-v v1= $a-v v2= $b ' begin{print v1+v2} ' to pass Bash's value to awk
awk-f:-v v1= $a ' $0~v1 ' {print $} ~ Match Condition
awk ' begin{i=0;} {i++} End{print i} ' file
awk '/^root/{$3+=10;print $} ' file
awk ' $1~ ' root ' {print '} ' file
awk ' $1==500{print $ ' file first field is
awk ' $NF ~ "bash" {print $} ' file last field has bash
awk ' $3>=500&&$3<=503{print} ' file
awk-f:-v v1= $a ' $1~^v1 ' file prints a line beginning with variable a
awk-f: ' {if ($1~/^root/) print '} ' file prints the first field of the first field that begins with the root of the row
awk ' System ("Useradd") ' File System ' () ' command ' called

Sed and awk common command examples

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.