Linux sed and awk

Source: Internet
Author: User

Sed

Strem Editor Editor

The SED editor is a row-by-row processing of the contents of a file. The content being processed is stored in the pattern space (buffer), and the output or file is modified as specified by the option when processing is complete.

SED is mainly used to automatically edit one or more files, to simplify the repeated operation of files, non-interactive compilation files

Grammar:

Sed "Options" ' "Command" ' "FileName"

Options

-N suppresses automatic (default) output reads the next input line,-n the line number to read

-e executes multiple SED commands

-F Run Script

-I edit file contents

-i.bak edit while creating a backup of. bak

-R using extended regular expressions

Command

A is appended after the match

I insert after matching

P Printing

D Delete

R/R Read File/Line

W Save

s find

C Replace

Y Replace

h/h Copy/Append mode space (buffer) to storage space

g/g paste Fetch/Append to mode space from storage space

X two exchange of space content

n/n Copy/Append the next line of content to the current

D Delete the contents before \ n

P Print the contents of the previous \ n

B Unconditional Jump

T satisfies a match after the jump

T meet jump when match is not met

Display the contents of the third line of the file

[Email protected] ~]#sed-N 3p/etc/passwd to the third row p prints outdaemon:x:2:2:d aemon:/sbin:/sbin/Nologin[[email protected]~]#sed-N'1,3p'/etc/passwd displaying 1-3 rows of contentroot:x:0:0: root:/root:/bin/bashbin:x:1:1: bin:/bin:/sbin/nologindaemon:x:2:2:d aemon:/sbin:/sbin/Nologin[[email protected]~]#sed-N'1,3!p'/etc/passwd Plus exclamation point is all but 1-3 show everything elseadm:x:3:4: adm:/var/adm:/sbin/NologinLP: x:4:7:LP:/var/spool/lpd:/sbin/NologinSync: x:5:0:Sync:/sbin:/bin/Syncshutdown:x:6:0: shutdown:/sbin:/sbin/shutdownhalt:x:7:0: halt:/sbin:/sbin/haltmail:x:8: A: mail:/var/spool/mail:/sbin/nologinoperator:x: One:0: operator:/root:/sbin/nologingames:x: A: -: games:/usr/games:/sbin/NologinFTP: x: -: -: FTP user:/var/FTP:/sbin/nologinnobody:x: About: About: nobody:/:/sbin/Nologinavahi-AUTOIPD:X: the: the: Avahi ipv4ll stack:/var/lib/avahi-autoipd:/sbin/nologinsystemd-BUS-PROXY:X:999:997: Systemd Bus proxy:/:/sbin/nologinsystemd-NETWORK:X:998:996: Systemd Network management:/:/sbin/nologindbus:x:Bayi:Bayi: System Message bus:/:/sbin/nologinpolkitd:x:997:995: User forPolkitd:/:/sbin/nologin

Show the third row and the next three rows

[Email protected] ~]#sed-N'3,+3p'/etc/passwddaemon:x:2:2:d aemon:/sbin:/sbin/nologinadm:x:3:4: adm:/var/adm:/sbin/NologinLP: x:4:7:LP:/var/spool/lpd:/sbin/NologinSync: x:5:0:Sync:/sbin:/bin/Sync

Insert "# # #" At the head of the file, insert "@@@" at the end of the file, and replace the third line of the file with "$$$"

sed ' 1i### ' /etc/passwd# #root: x:0:0: root:/root:/bin/bashbin:x:  1:1: bin:/bin:/sbin/nologindaemon:x:2:2:d aemon:/sbin:/sbin/ Nologi   This after the change is not saved, the original file is still the same old
[Email protected] ~]#sed '1i###'/etc/passwd>a.txt REDIRECT [[email protected]~]#sed '1i###'/etc/passwd>>a.txt Append [[email protected]~]#HeadA.txt # # #root: x:0:0: root:/root:/bin/bashbin:x:1:1: bin:/bin:/sbin/nologindaemon:x:2:2:d aemon:/sbin:/sbin/nologinadm:x:3:4: adm:/var/adm:/sbin/NologinLP: x:4:7:LP:/var/spool/lpd:/sbin/NologinSync: x:5:0:Sync:/sbin:/bin/Syncshutdown:x:6:0: shutdown:/sbin:/sbin/shutdownhalt:x:7:0: halt:/sbin:/sbin/haltmail:x:8: A: Mail:/var/spool/mail:/sbin/nologin
[Email protected] ~]#sed '[Email protected]@@'/etc/passwd>>a.txt Append @@@ $ on the end of the file to represent the trailing a for append [[email protected]~]#Taila.txt gdm:x: the: the::/var/lib/gdm:/sbin/Nologingnome-INITIAL-SETUP:X:989:984::/run/gnome-initial-setup/:/sbin/nologinavahi:x: -: -: Avahi mdns/dns-sd stack:/var/run/avahi-daemon:/sbin/nologinpostfix:x: the: the::/var/spool/postfix:/sbin/nologinsshd:x: About: About:P rivilege-separated ssh:/var/empty/sshd:/sbin/nologinntp:x: -: -::/etc/ntp:/sbin/nologintcpdump:x: the: the::/:/sbin/nologinzq:x: +: +: zq:/home/zq:/bin/bashapache:x: -: -: apache:/usr/share/httpd:/sbin/nologin@@@
sed ' 3c$$$ ' /etc/passwd   third line insert $$$root:x:0:0: root:/root:/bin/bashbin:x :1:1: bin:/bin:/sbin/nologin$$$

Awk

Awk is an excellent text processing tool, one of the most powerful data processing engines available in Linux and UNIX environments. The maximum functionality of this programming and data manipulation language depends on the knowledge that one person has. Awk named: Alfred Aho Peter Weinberger and Brian Kemighan the initials of a three-person surname.

In the simplest sense, awk is a programming language tool for working with text. Any awk statement is made up of patterns and actions, and an awk script can have multiple statements. The mode determines the trigger condition and the trigger time of the action statement.

Special fields:

The BEGIN statement sets the count and print header information before any action.

The end statement outputs statistical results, which are executed after the action is completed.

The delimiter default is a space, you can use-f, to change to a comma delimiter-F, or to a colon-f

Eg: Prepare the content to be displayed

Vim Result.txt

Aa7  -  - Panax Notoginseng  -  $BB8  -  -  -  -  -cc 9  +  in  the  the  -DD 6  -  -  $  $  About
[Email protected] ~]#awk "{print $}"Result.txt double quotes How can not, big God if see please give a comment [[email protected]~]#awk '{print $}'Result.txt $ Show All contents AA7  -  - Panax Notoginseng  -  $BB8  -  -  -  -  -cc 9  +  in  the  the  -DD 6  -  -  $  $  About[[Email protected]~]#awk '{print $}'result.txt The first column of the display content AabbccDD

Display the first column of/etc/passwd with: as a delimiter

awk ' {print $} ' /etc/passwdrootbindaemonadmLPsyncshutdownhaltmailoperator 

Show first and third column contents

 [[email protected] ~]# awk   " Span style= "COLOR: #800000" >{print $1,$3}   "  result.txt AA  17  BB  18  cc  19  dd  16 a bit python takes the meaning of the elements inside the dictionary  
awk ' BEGIN {print "Name level result\n"} {print $1,$2,$3} end{print "END of Class1 Results"} '  7 8 cc 9 DD  6End of Class1 results This is a    bit more python nested meaning, it is not given the value of a name, nor nested is to give the value of a name assigned 
[Email protected] ~]#awk '$ >= 7 {print $}'result.txt and SQL Python are connected to AA.7  -  - Panax Notoginseng  -  $BB8  -  -  -  -  -cc 9  +  in  the  the  -
awk ' {if (= = = "AA" | | $2== "8") print $} '  7 8 18     The ubiquitous if  or   

Linux sed and awk

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.