4.shell programming - Text Processing Three Musketeers sed

4.1.sed options

 sed, the stream editor. Standard output file line by line or process.

Syntax

  • The first: stdout | sed [option] "pattern command"
  • The second: sed [option] "pattern command" file

 Options

  • -n print-only mode matching lines
  • sed -e to edit directly from the command line, the default option
  • -f editing actions are saved in a file, specify the file execution
  • -r supports extended regular expressions
  • -i directly modify the contents of the file

Examples

[root@VM_0_9_centos shell_learn]# cat test.txt 
I love python
I love PYTHON
Hadoop is bigdata frame
[root@VM_0_9_centos shell_learn]# sed -n 'p' test.txt 
I love python
I love PYTHON
Hadoop is bigdata frame
[root@VM_0_9_centos shell_learn]# sed -n '/python/p' test.txt 
I love python
[root@VM_0_9_centos shell_learn]# sed -n -e '/python/p' -e '/PYTHON/p' test.txt 
I love python
I love PYTHON
[root@VM_0_9_centos shell_learn]#

-f option to edit the text into action

[root@VM_0_9_centos shell_learn]# cat edit.txt 
/python/p
[root@VM_0_9_centos shell_learn]# sed -n -f edit.txt test.txt 
I love python
[root@VM_0_9_centos shell_learn]#

 -i modification

sed -i 's/love/like/g' test.txt

Detailed pattern of 4.2.sed

pattern with published

 

(1) directly specifying the line number

[root@VM_0_9_centos shell_learn]# sed -n '17p' /etc/passwd
dbus:x:81:81:System message bus:/:/sbin/nologin

(2) specify the starting and ending line number line number

[root@VM_0_9_centos shell_learn]# sed -n '10,13p' /etc/passwd
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
[root@VM_0_9_centos shell_learn]# 

(3) specify the starting line number, and then back rows N

[root@VM_0_9_centos shell_learn]# sed -n '10,+5p' /etc/passwd
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
[root@VM_0_9_centos shell_learn]# 

(4) / pattern1 line / regular expression matching

[root@VM_0_9_centos shell_learn]# sed -n '/derek/p' /etc/passwd
derektest:x:1001:1001::/home/derektest:/bin/bash
derek:x:1002:1002::/home/derek:/bin/bash
[root@VM_0_9_centos shell_learn]# 

(5)/pattern1/,/pattern2/

[root@VM_0_9_centos shell_learn]# sed -n '/nginx/,/derek/p' /etc/passwd
nginx:x:993:991:Nginx web server:/var/lib/nginx:/sbin/nologin
memcached:x:992:990:Memcached daemon:/run/memcached:/sbin/nologin
redis:x:991:989:Redis Database Server:/var/lib/redis:/sbin/nologin
derektest:x:1001:1001::/home/derektest:/bin/bash
[root@VM_0_9_centos shell_learn]# 

(6)linenumber,/pattern1/

30 matches from the start line until the match to the end of the line derek

[root@VM_0_9_centos shell_learn]# sed -n '30,/derek/p' /etc/passwd
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
nginx:x:993:991:Nginx web server:/var/lib/nginx:/sbin/nologin
memcached:x:992:990:Memcached daemon:/run/memcached:/sbin/nologin
redis:x:991:989:Redis Database Server:/var/lib/redis:/sbin/nologin
derektest:x:1001:1001::/home/derektest:/bin/bash
[root@VM_0_9_centos shell_learn]#

(7)/pattern1/,linenumber

Starting nginx line to end line 35

[root@VM_0_9_centos shell_learn]# sed -n '/nginx/,35p' /etc/passwd
nginx:x:993:991:Nginx web server:/var/lib/nginx:/sbin/nologin
memcached:x:992:990:Memcached daemon:/run/memcached:/sbin/nologin
redis:x:991:989:Redis Database Server:/var/lib/redis:/sbin/nologin
derektest:x:1001:1001::/home/derektest:/bin/bash
derek:x:1002:1002::/home/derek:/bin/bash
[root@VM_0_9_centos shell_learn]# 

The delete 4.3.sed

(1)p

Inquire

sed -N '1p' test.txt

(2) d delete

Delete 1 to 3 lines

sed -i '1,3d' test.txt

Remove to begin with '' Beau '', in order to "Simp" beginning, middle, all rows

test.txt

[root@VM_0_9_centos shell_learn]# cat test.txt 
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
[root@VM_0_9_centos shell_learn]#
sed -i '/^Beau/,/^Simp/d' test.txt
[root@VM_0_9_centos shell_learn]# cat test.txt 
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts

The increase 4.4.sed

(3)a

After the match to the line additional content

[root@VM_0_9_centos shell_learn]# cat test.txt 
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
[root@VM_0_9_centos shell_learn]# sed -i '/Flat/a zhang-derek' test.txt 
[root@VM_0_9_centos shell_learn]# 
[root@VM_0_9_centos shell_learn]# cat test.txt 
Complex is better than complicated.
Flat is better than nested.
zhang-derek
Sparse is better than dense.
Readability counts.
[root@VM_0_9_centos shell_learn]# 

(4)i

Front row to match the additional content

[root@VM_0_9_centos shell_learn]# cat test.txt 
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
[root@VM_0_9_centos shell_learn]# sed -i '/Flat/i zhang-derek' test.txt 
[root@VM_0_9_centos shell_learn]# 
[root@VM_0_9_centos shell_learn]# cat test.txt 
Complex is better than complicated.
zhang-derek
Flat is better than nested.
Sparse is better than dense.
Readability counts.
[root@VM_0_9_centos shell_learn]# 

(5)r

After the line in the specified file appends to climb match

[root@VM_0_9_centos shell_learn]# cat list.txt 
xxxxxxxxxxx
yyyyyyyyyyy
[root@VM_0_9_centos shell_learn]# cat test.txt 
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
[root@VM_0_9_centos shell_learn]# sed -i '/Flat/r list.txt' test.txt 
[root@VM_0_9_centos shell_learn]# 
[root@VM_0_9_centos shell_learn]# cat test.txt 
Complex is better than complicated.
Flat is better than nested.
xxxxxxxxxxx
yyyyyyyyyyy
Sparse is better than dense.
Readability counts.
[root@VM_0_9_centos shell_learn]#

(6)w

The match line to save the contents to other documents

[root@VM_0_9_centos shell_learn]# cat test.txt 
Complex is better than complicated.
Flat is better than nested.
xxxxxxxxxxx
yyyyyyyyyyy
Sparse is better than dense.
Readability counts.
[root@VM_0_9_centos shell_learn]# touch 1.txt
[root@VM_0_9_centos shell_learn]# sed -i '/^xxx/,/^yyy/w 1.txt' test.txt 
[root@VM_0_9_centos shell_learn]# 
[root@VM_0_9_centos shell_learn]# cat 1.txt 
xxxxxxxxxxx
yyyyyyyyyyy
[root@VM_0_9_centos shell_learn]# 

4.5.sed of modification

  • s / pattern / string to replace only the first row
  • s / pattern / string / g to replace all the all the lines
  • s / pattern / string / ig replace all, and a case-insensitive 

 Examples

[root@VM_0_9_centos shell_learn]# cat 2.txt 
i like python
i like english
I like django
I like flask,flask,flask
[root@VM_0_9_centos shell_learn]# sed -i 's/flask/Flask/g' 2.txt 
[root@VM_0_9_centos shell_learn]# 
[root@VM_0_9_centos shell_learn]# cat 2.txt 
i like python
i like english
I like django
I like Flask,Flask,Flask

. Backreferences 4.6

Example one

[root@VM_0_9_centos shell_learn]# cat 3.txt 
hadAAp
hadBBp
hadCCp
hadDDp
[root@VM_0_9_centos shell_learn]# sed -i 's/had..p/&ss/g' 3.txt 
[root@VM_0_9_centos shell_learn]# 
[root@VM_0_9_centos shell_learn]# cat 3.txt 
hadAApss
hadBBpss
hadCCpss
hadDDpss
[root@VM_0_9_centos shell_learn]#

Description: "&" indicates that the matched content, the result is to match all content in the back plus "ss"

Example Two

[root@VM_0_9_centos shell_learn]# cat 3.txt 
hadAApss
hadBBpss
hadCCpss
hadDDpss
[root@VM_0_9_centos shell_learn]# sed -i 's/\(had\)...../\1derek/g' 3.txt 
[root@VM_0_9_centos shell_learn]# 
[root@VM_0_9_centos shell_learn]# cat 3.txt 
hadderek
hadderek
hadderek
hadderek
[root@VM_0_9_centos shell_learn]#

Description: "\ 1" and "&" The difference is that the "\ 1" to reverse part of the matching reference to the content, and then modify it, "&" can only modify the content of the whole match, not split

 

Guess you like

Origin www.cnblogs.com/derek1184405959/p/11111530.html