linux sed命令詳解


1、定位

定位一行

x:行號
sed -n '10p' passwd
nl passwd | sed -n '10p'
/pattern/:正則
sed -n '/abc/p' passwd

[root@idtp4 test]# nl -ba passwd |sed -n '/root/p'
     1  root:x:0:0:root:/root:/bin/bash
    11  operator:x:11:0:operator:/root:/sbin/nologin
[root@idtp4 test]#

定位幾行

x,y:行號
nl passed | sed -n '10,20p'
/pattern/,x:正則
sed -n '/mail/,/abc/p' passwd
x,y!:不選擇這一行
nl passwd |sed -n '10!p'
nl passwd | sed -n '10,20!p'

間隔幾行

first~step
first:開始行
step:步進
nl passwd | sed -n '1~2p'

二、基本操作命令

-a(追加行)/i(插入行)
-c(替代行)
-d(刪除行)

$a:在文件末尾追加內容

#在環境變量末尾追加內容
#\n為換行
[root@idtp4 test]# sed -i '$a export LRX_HOME=\nexport path='  profile
[root@idtp4 test]# tail -5f profile
export PATH=$PATH:$ACCUMULO_HOME/bin
export ANT_HOME=/software/apache-ant-1.9.13/bootstrap
export PATH=$PATH:$ANT_HOME/bin
export LRX_HOME= export path=

如果想在添加的內容開頭加空格,就要在$a后加入轉義符“\”。

#添加空格對齊
[root@idtp4 test]# sed -i '$a\  export LRX_HOME\nexport path' profile
[root@idtp4 test]# tail -5f profile
export PATH=$PATH:$ACCUMULO_HOME/bin
export ANT_HOME=/software/apache-ant-1.9.13/bootstrap
export PATH=$PATH:$ANT_HOME/bin
  export LRX_HOME export path

三、常用命令

刪除空行

sed '/^$/d'  filename

替換操作

 s

分隔符:/
全局替換:g

sed 's/nologin/login/' passwd
sed 's/:/%/g' passwd

 

打印時用-n 和p。

替換時用s 和g(全局)。

 

 轉換大小寫

\u \l:對首字母轉大寫、小寫
\U \L:對一串字符轉大寫、小寫

sed 's/^[a-z-_]\+/\u&/' passwd
#將文件夾下的.txt文件名轉換為大寫
ls *.txt | sed 's/^\w\+/\U&/'

其中,&表示替換前的全部內容。

[root@idtp4 test]# ls |sed 's/.*/\u&/'
123.txt
Abc.txt
Access_log
Passwd
Profile
Test.txt
[root@idtp4 test]# ls |sed 's/.*/\U&/'
123.TXT
ABC.TXT
ACCESS_LOG
PASSWD
PROFILE
TEST.TXT
[root@idtp4 test]# ls |sed 's/.*/\l&/'
123.txt
abc.txt
access_log
passwd
profile
test.txt

四、括號

()

  • 括號代表指代內容,可以用1,2指代括號里的內容。
  • 括號要轉義,前面要加\轉義符。

文件里有:w1w2w3的內容,要取w2,可以寫為:

sed  's/w1\(w2\)w3/\1/'

結果為w2。

# 取到第一個ip
[root@idtp4 test]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.166.190  netmask 255.255.240.0  broadcast 172.17.175.255
        ether 00:16:3e:0e:a4:48  txqueuelen 1000  (Ethernet)
[root@idtp4 test]# ifconfig|sed -n '/inet.*bro/p' |sed 's/inet\+\s\([0-9\.]\+\).\snet.*$/\1/'
        172.17.166.190

也可以寫成:

# 取到第一個ip
[root@idtp4 test]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.166.190  netmask 255.255.240.0  broadcast 172.17.175.255
        ether 00:16:3e:0e:a4:48  txqueuelen 1000  (Ethernet)
[root@idtp4 test]# ifconfig|sed -n '/inet.*bro/p'|sed 's/inet.*\s\([0-9\.]\+\).*netmask.*\s\([0-9\.]\+\).*broadcast.*\s\([0-9\.]\)/\3/'
        172.17.175.255

[ ]

  • 表示從里面取任意值。
#0到9中任意的值,包含點,點需要轉義
[0-9\.]
#a到z的任意值,包含-,以括號里面的值開頭,a到z是連續的,連續的用短線連接,結束連續后面要先接下划線,再接短線
^[a-z_-]

+:表示1個以上。

^:表示開頭。

$:表示結束。

&:表示替換前的全部內容。

{}:同時執行多個sed命令。

#取passwd里的用戶名,uid,gid
[root@idtp4 test]# sed 's/\([a-z_-]\+\):x:\([0-9]\+\):\([0-9]\+\):.*$/\1,\2,\3/' passwd
root,0,0
bin,1,1
daemon,2,2

五、r和w

r:讀,不會改變文件內容

w:寫,會改變文件內容,覆蓋寫

[root@idtp4 test]# cat 123.txt
123
456
789
[root@idtp4 test]# cat abc.txt
abc
qwe
sd
#將讀取123.txt內容,並加入到abc.txt的第一行后面,不改變文件內容
[root@idtp4 test]# sed '1r 123.txt' abc.txt
abc
123
456
789
qwe
sd
[root@idtp4 test]# cat 123.txt
123
456
789
[root@idtp4 test]# cat abc.txt
abc
qwe
sd
[root@idtp4 test]# cat 123.txt
123
456
789
[root@idtp4 test]# cat abc.txt
abc
qwe
sd
#將abc.txt的第一行寫入到123.txt里,覆蓋寫
[root@idtp4 test]# sed '1w 123.txt' abc.txt
abc
qwe
sd
[root@idtp4 test]# cat 123.txt
abc
[root@idtp4 test]# cat abc.txt
abc
qwe
sd

q:退出

#打印到第10行,退出
[root@idtp4 test]# nl -ba passwd |sed '10q'
     1  root:x:0:0:root:/root:/bin/bash
     2  bin:x:1:1:bin:/bin:/sbin/nologin
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     6
     7  sync:x:5:0:sync:/sbin:/bin/sync
     8  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     9  halt:x:7:0:halt:/sbin:/sbin/halt
    10  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
[root@idtp4 test]#

六、命令行格式

-i:修改文件內容

-n:忽略默認輸出

[root@idtp4 test]# nl -ba passwd|sed -n '10p' 
10 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
[root@idtp4 test]#

 

-e:跟{}一樣,可以同時寫入多個命令

#刪除2到3行,同時將冒號替換為%號
[root@idtp4 test]# nl -ba passwd |sed -e '10,20d' -e 's/:/%/g'
     1  root%x%0%0%root%/root%/bin/bash
     4  bin%x%1%1%bin%/bin%/sbin/nologin
     5  daemon%x%2%2%daemon%/sbin%/sbin/nologin
     6  adm%x%3%4%adm%/var/adm%/sbin/nologin
     7  lp%x%4%7%lp%/var/spool/lpd%/sbin/nologin
[root@idtp4 test]#

七、腳本格式

sed -f scriptfile file

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM