linuxRegexsed

來源:互聯網
上載者:User

標籤:正則   centos   sed   

sed、awk工具可以實現文本替換並且把替換的文本輸出到螢幕上

sed和awk都是流式編輯器,是針對文檔的行來操作的。sed通常用來替換操作。


樣本的常值內容,以下操作根據此文本。

[[email protected] ~]# cat test.txt rot:x:0:0:rot:/rot:/bin/bashroot:x:0:0:root:/root:/bin/bashdaemon:x:2:2:daemon:/sbin:/sbin/nologinrooooot:x:0:0/roooooot:/bin/bash11111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa


1、列印某一行

sed -n ‘n‘p filename 單引號內的n是一個數字,表示第幾行;p也可以寫到引號內;

樣本,列印passwd第3-5行;

[[email protected] ~]# sed -n ‘3,5‘p /etc/passwddaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

列印所有行

[[email protected] ~]# sed -n ‘1,$‘p /etc/passwd

列印某一行,並且要顯示行號,需要和grep一起使用;

[[email protected] ~]# grep -n ‘.*‘ test.txt | sed -n ‘1‘p1:rrt


2、列印包含某個字串的行

字串需要用/ /括起來;grep中的特殊符號^ $ . * 同樣適用於sed中。

[[email protected] ~]# sed -n ‘/root/‘p /etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin


列印包含/sbin/nologin的行,/需要進行脫意;

[[email protected] ~]# sed -n ‘/\/sbin\/nologin/‘p passwd bin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

列印以d開頭的行;列印以in結尾的;列印r與o字母中間有2個任一字元的;列印包含2個o及多個o的行;

[[email protected] ~]# sed -n ‘/^d/‘p test.txt daemon:x:2:2:daemon:/sbin:/sbin/nologin[[email protected] ~]# sed -n ‘/in$/‘p test.txt daemon:x:2:2:daemon:/sbin:/sbin/nologin[[email protected] ~]# sed -n ‘/r..o/‘p test.txtrooooot:x:0:0/roooooot:/bin/bash[[email protected] ~]# sed -n ‘/ooo*/‘p test.txtroot:x:0:0:root:/root:/bin/bashrooooot:x:0:0/roooooot:/bin/bash


3、-e可以實現多個行為

[[email protected] ~]# sed -e ‘1‘p -e ‘/111/‘p -n test.txt rot:x:0:0:rot:/rot:/bin/bash11111111111111111111111111111111

可以用-e寫多個;-e相當於傳送帶,先匹配最前面的列印,再匹配後面的列印,所以第一行會列印2次;

[[email protected] ~]# sed -e ‘/root/p‘ -e ‘/bash/p‘ -n 1.txtroot:x:0:0:root:/root:/bin/bashroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologinuser1:x:500:500::/home/user1:/bin/bashuser2:x:501:501::/home/user2:/bin/bash


-e後面也可以用;分號;-n不能寫到-e的後面,可以寫在-e的前面或最後面;

[[email protected] ~]# sed -e ‘/root/p;/bash/p‘ -n 1.txtroot:x:0:0:root:/root:/bin/bashroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologinuser1:x:500:500::/home/user1:/bin/bashuser2:x:501:501::/home/user2:/bin/bash


4、刪除某行或者多行

‘d’ 這個字元就是刪除的動作了,不僅可以刪除指定的單行以及多行,而且還可以刪除匹配某個字元的行,另外還可以刪除從某一行一直到文檔末行。


樣本,刪除第一行;刪除1-3行;刪除包含root關鍵詞的行;刪除第3行到最後一行所有行;

[[email protected] ~]# sed ‘1‘d test.txt root:x:0:0:root:/root:/bin/bashdaemon:x:2:2:daemon:/sbin:/sbin/nologinrooooot:x:0:0/roooooot:/bin/bash11111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[[email protected] ~]# sed ‘1,3‘d test.txt rooooot:x:0:0/roooooot:/bin/bash11111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[[email protected] ~]# sed ‘/root/‘d test.txtrot:x:0:0:rot:/rot:/bin/bashdaemon:x:2:2:daemon:/sbin:/sbin/nologinrooooot:x:0:0/roooooot:/bin/bash11111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[[email protected] ~]# sed ‘3,$‘d test.txt rot:x:0:0:rot:/rot:/bin/bashroot:x:0:0:root:/root:/bin/bash


5、替換字元或替換字串

樣本,替換第一行字母r為R;替換1-2行中ot為to;替換所有行中ot為to;

[[email protected] ~]# sed ‘1s/r/R/g‘ test.txt RRt[[email protected] ~]# sed ‘1,2s/ot/to/g‘ test.txt rto:x:0:0:rto:/rto:/bin/bashroto:x:0:0:roto:/roto:/bin/bashdaemon:x:2:2:daemon:/sbin:/sbin/nologinrooooot:x:0:0/roooooot:/bin/bash11111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[[email protected] ~]# sed ‘s#ot#to#g‘ test.txt rto:x:0:0:rto:/rto:/bin/bashroto:x:0:0:roto:/roto:/bin/bashdaemon:x:2:2:daemon:/sbin:/sbin/nologinrooooto:x:0:0/roooooto:/bin/bash11111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

與vim編輯器裡面替換一樣的命令格式,s代表替換,g代表全域,不加g的話只替換行的第一個;可以使用# @作為分隔字元。

1s代表只替換第一行;1,3s代表第一行到第三行;不加行數的話預設為全部行。


樣本,刪除文檔中所有的數字‘s/[0-9]//g‘  如刪除文檔中所有的字母‘s/[a-zA-Z]//g‘刪除所有的數字和字母‘s/[0-9a-zA-Z]//g‘

[[email protected] ~]# sed ‘s/[0-9]//g‘ test.txt rot:x:::rot:/rot:/bin/bashroot:x:::root:/root:/bin/bashdaemon:x:::daemon:/sbin:/sbin/nologinrooooot:x::/roooooot:/bin/bashaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

樣本,刪除文檔中所有的字母

[[email protected] ~]# sed ‘s/[a-z]//g‘ 1.txt ::0:0:/://::11:0::/://::11:0::/://::0:0:/://1111111111111111111111111111111

樣本,刪除文檔中不是英文字母的(數字和特殊符號)

[[email protected] ~]# sed ‘s/[^a-z]//g‘ 1.txt rotxrotbinbashoperatorxoperatorrootsbinnologinoperatorxoperatorroootsbinnologinrooootxroooootbinbashaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

樣本,刪除文檔中不是數位(英文字母和特殊符號)

[[email protected] ~]# sed ‘s/[^0-9]//g‘ 1.txt 00110110001111111111111111111111111111111


樣本,把文檔中的小寫字母全部替換為大寫字母 ‘s/[a-z]/\u&/g‘ 

[[email protected] ~]# sed ‘s/[a-z]/\u&/g‘ 1.txt ROT:X:0:0:/ROT:/BIN/BASHOPERATOR:X:11:0:OPERATOR:/ROOT:/SBIN/NOLOGINOPERATOR:X:11:0:OPERATOR:/ROOOT:/SBIN/NOLOGINROOOOT:X:0:0:/ROOOOOT:/BIN/BASH1111111111111111111111111111111AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

樣本,把文檔中的大寫字母全部替換為小寫字母 ‘s/[A-Z]/\l&/g‘ 

[[email protected] ~]# sed ‘s/[A-Z]/\l&/g‘ 1.txt rot:x:0:0:/rot:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologinoperator:x:11:0:operator:/rooot:/sbin/nologinroooot:x:0:0:/rooooot:/bin/bash1111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

替換的同時加-n p 列印特定的行;

[[email protected] ~]# sed -n ‘1s/[a-z]/\u&/g‘p 1.txt ROT:X:0:0:/ROT:/BIN/BASH


6、調換二個字串的位置

sed -r ‘s/([^:]+)(:.*:)([^:]+$)/\3\2\1/‘

[^:]非冒號,不是冒號的所有字元

-r, --regexp-extended  在指令碼中使用擴充Regex; 加r選項的意思為後面不用再加脫意\  

( )括弧括起來是分段的意思,

\3\2\1    是代表3調換1,後面的內容調換到前面


樣本,aa與ff調換;加-r選項不用加脫意符號\;不加-r ( )括弧左右都需要加\脫意;調換多行用最後一個;

[[email protected] ~]# cat 1.txt aa:bb:cc:dd:ee:ff[[email protected] ~]# sed -r ‘s/(aa)(.*)(ff)/\3\2\1/‘ 1.txtff:bb:cc:dd:ee:aa[[email protected] ~]# sed ‘s/\(aa\)\(.*\)\(ff\)/\3\2\1/‘ 1.txt ff:bb:cc:dd:ee:aa[[email protected] ~]# sed -r ‘s/([^:]+)(:.*:)([^:]+$)/\3\2\1/‘ 1.txt ff:bb:cc:dd:ee:aa


樣本,rot與/bin/bash進行調換

[[email protected] ~]# grep ‘rot‘ test.txtrot:x:0:0:rot:/rot:/bin/bash[[email protected] ~]# grep ‘rot‘ test.txt |sed ‘s/\(rot\)\(.*\)\(\/bin\/bash\)/\3\2\1/‘/bin/bash:x:0:0:rot:/rot:rot[[email protected] ~]# grep ‘rot‘ test.txt |sed -r ‘s/(rot)(.*)(/bin/bash)/\3\2\1/‘sed:-e 運算式 #1,字元 18:“s”的未知選項[[email protected] ~]# grep ‘rot‘ test.txt |sed -r ‘s/(rot)(.*)(\/bin\/bash)/\3\2\1/‘/bin/bash:x:0:0:rot:/rot:rot

加-r  後()內的/bin/bash 也還需要脫意才可以;


-i 選項    直接替換字元,並且儲存原檔案裡面。使用i選項前,記得備份原檔案。

[[email protected] ~]# sed -r -i ‘s/([^:]+)(:.*:)([^:]+$)/\3\2\1/‘ 1.txt [[email protected] ~]# cat 1.txt ff:bb:cc:dd:ee:aa




本文出自 “模範生的學習部落格” 部落格,請務必保留此出處http://8802265.blog.51cto.com/8792265/1632293

linuxRegexsed

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.