1. 如何在一個文本檔案中的每一行頭插入一個字元
2. 如何在一個文本檔案中的每一行尾插入一個字元
3. 如何在一個文本檔案中的每一行的指定列插入一串字元
1.awk '{print "x"$0}' urfile
2.awk '{print $0"x"}' urfile
3.awk '$o=$o" x"' urfile
sed全名叫stream editor,流編輯器(也叫行編輯器),其處理文本的方式為一行一行的,不同于vi等全屏編輯器;主要用途為通過比對一個或多個正規表達式來對文本進行處理,實作過濾和轉換文本。
sed 的工作方式
sed 實用工具按順序逐行将檔案讀入到記憶體中。然後,它執行為該行指定的所有操作,并在完成請求的修改之後将該行放回到記憶體中,以将其轉儲至終端。完成了這一行 上的所有操作之後,它讀取檔案的下一行,然後重複該過程直到它完成該檔案。如同前面所提到的,預設輸出是将每一行的内容輸出到螢幕上(特别要注意這點,是以一般要配合-n參數不讓其顯示不需要的)。在這裡,開始涉及到 兩個重要的因素—首先,輸出可以被重定向到另一檔案中,以儲存變化;第二,源檔案(預設地)保持不被修改。sed 預設讀取整個檔案并對其中的每一行進行修改。不過,可以按需要将操作限制在指定的行上。
通過man指令檢視sed幫助如下:
name sed – stream editor for filtering and transforming text synopsis sed [option]… {script-only-if-no-other-script} [input-file]…
用便于了解的表示其用法如下:
sed [options] ‘addresscommand’ file …
其中addresscommand表示對需要處理的範圍(位址)執行的指令
-n: 靜默模式,不再預設顯示模式空間中的内容
-i: 直接修改原檔案
-e script -e script:可以同時執行多個腳本
-f /path/to/sed_script
sed -f /path/to/scripts file
-r: 表示使用擴充正規表達式
-n:顯示出其他資料行的預設操作,隻顯示符合的資料行,如下面加-n選項和不加的顯示效果是不一樣的:
address表示sed處理的範圍,如第20到30行、以root開頭的行等等。主要有如下幾種:
指定行:startline,endline
5,30 表示第5至30行
[root@localhost ~]# sed ‘5,30d’ /etc/passwd ##删除5至30行内容(不會修改原檔案) root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin
$ 表示最後一行
正規表達式比對:/regexp/
sed正規表達式的用法基本與grep一樣,如:
[root@localhost ~]# sed ‘/nologin$/d’ /etc/passwd ##删除以nologin結尾的行 sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt barlow:x:500:500::/home/barlow:/bin/bash
/pattern1/,/pattern2/
第一次被pattern1比對到的行開始,至第一次被pattern2比對到的行結束,這中間的所有行
指定具體的行:linenumber
從某行開始及後面的多少行:addr1,+n
上面的5,30 等于5,+25
[root@localhost ~]# sed ‘5,+25d’ /etc/passwd
在将command之前,需要再次強調的是,address和command之間是直接相連的,中間沒有空格或者其他符号。
command主要有如下這些:
d: 删除符合條件的行;參見前面address部分的示例。
p: 顯示符合條件的行;需要注意的是,文章第二段紅字部分已經強調了,sed預設會把處理的文本一行行輸出到終端,是以直接用p指令看不出什麼效果。
a \string: 在指定的行後面追加新行,内容為string,如下面的例子将在文本最後添加一行内容,注意cat看到結果與sed處理結果的差異:
\n:可以用于換行
i \string: 在指定的行前面添加新行,内容為string
r file: 将指定的檔案的内容添加至符合條件的行處,如在fstab第二行後添加issue檔案的内容
w file: 将位址指定的範圍内的行另存至指定的檔案中;
s/pattern/string/修飾符: 查找并替換,預設隻替換每行中第一次被模式比對到的字元串
修飾符主要有:
g: 全局替換
i: 忽略字元大小寫
注意,替換指令的三個/可以用其他字元替換,如:
s@pattern@string@修飾符 s#pattern#string#修飾符
這些都是可以的,隻要三個分隔符一樣即可。
sed的查找替換功能參照我以前的博文:linux下使用sed指令替換檔案檔案内容
&: 引用模式比對整個串
[root@localhost ~]# sed ‘s/^[ ]*//g’ filename [root@localhost ~]# sed ‘s/^ *//g’ filename [root@localhost ~]# sed ‘s/^[[:space:]]*//g’ filename
行後:
[root@localhost ~]# sed ‘s/pattern/&\n/g’ filename
行前:
[root@localhost ~]# sed ‘s/pattern/\n&/g’ filename
注意:&代表pattern
[root@localhost ~]# sed -e “s/$var1/$var2/g” filename
sed -i ‘1 i\插入字元串’ filename
[root@localhost ~]# sed -i ‘$ a\插入字元串’ filename
[root@localhost ~]# sed -i ‘/pattern/ i “插入字元串”‘ filename
[root@localhost ~]# sed -i ‘/pattern/ a “插入字元串”‘ filename
[root@localhost ~]# grep -v ‘^#’ filename | sed ‘/^[[:space:]]*$/d’
[root@localhost ~]# df -p |sed -r ‘s/[[:space:]]+/:/g’ ##-p 選項表示df輸出資訊不自動換行 ##[[:space:]]+比對表示空格至少出現一次
一、sed (stream editor)
1、定位行:
sed -n '12,~3p' pass #從第12行開始,直到下一個3的倍數行(12-15行)
sed -n '12,+4p' pass #從第12行開始,連續4行(12-16行)
sed -n '12~3p' pass #從第12行開始,間隔3行輸出一次(12,15,18,21...)
sed -n '10,$p' pass #從第10行至結尾
sed -n '4!p' pass #除去第4行
2、正則:'/正則式/'
sed -n '/root/p' /etc/passwd
sed -n '/^root/p' /etc/passwd
sed -n '/bash$/p' /etc/passwd
sed -n '/ro.t/p' /etc/passwd
sed -n '/ro*/p' /etc/passwd
sed -n '/[abc]/p' /etc/passwd
sed -n '/[a-z]/p' /etc/passwd
sed -n '/[^abc]/p' /etc/passwd
sed -n '/^[^abc]/p' /etc/passwd
sed -n '/\<root/p' /etc/passwd
sed -n '/root\>/p' /etc/passwd
3、擴充正則:
sed -n '/root\|yerik/p' /etc/passwd #拓展正則需要轉義
sed -nr '/root|yerik/p' /etc/passwd #加-r參數支援拓展正則
sed -nr '/ro(ot|ye)rik/p' /etc/passwd #比對rootrik和royerik單詞
sed -nr '/ro?t/p' /etc/passwd #?比對0-1次前導字元
sed -nr '/ro+t/p' /etc/passwd #比對1-n次前導字元
sed -nr '/ro{2}t/p' /etc/passwd #比對2次前導字元
sed -nr '/ro{2,}t/p' /etc/passwd #比對多于2次前導字元
sed -nr '/ro{2,4}t/p' /etc/passwd #比對2-4次前導字元
sed -nr '/(root)*/p' /etc/passwd #比對0-n次前導單詞
4、sed編輯(對行的插入、删除、替換操作)
sed '/root/a admin' /etc/passwd #在root行後追加一個admin行
sed '/root/i admin' /etc/passwd #在root行前插入一個admin
sed '/root/c admin' /etc/passwd #将root行替換為admin
sed '/root/d' /etc/passwd #删除含有root的行
s替換
sed -n 's/root/admin/p' /etc/passwd
sed -n 's/root/admin/2p' /etc/passwd #在每行的第2個root作替換
sed -n 's/root/admin/gp' /etc/passwd
sed -n '1,10 s/root/admin/gp' /etc/passwd
sed -n 's/root/aaa&bbb/2p' /etc/passwd #将root替換成aaarootbbb,&作反向引用,代替前面的比對項
sed -ne 's/root/aaa&bbb/' -ne 's/bash/aaa&bbb/p' /etc/passwd #-e将多個指令連接配接起來,将root或bash行作替換
sed -n 's/root/aaa&bbb/;s/bash/aaa&bbb/p' /etc/passwd #與上指令功能相同
sed -nr 's/(root)(.*)(bash)/\3\2\1/p' /etc/passwd #将root與bash位置替換,兩标記替換
或sed -n 's/\(root\)\(.*\)\(bash\)/\3\2\1/p' /etc/passwd
bash:x:0:0:root:/root:/bin/root
y替換
echo "sorry"|sed 'y/ory/abc/' #一一對應替換(sabbc)
6、sed的模式空間和保持空間
h:模式---->保持
h:模式--->>保持
x:模式<--->保持
g:保持---->模式
g:保持--->>模式
例如:
111
222
333
444
# sed '1h;2,3h;4g'
分析
cmd 模式 保持
111 111 \n
1h 111 111
----------->111
222 222 111
2,3h 222 111\n222
----------->222
333 333 111\n222
2,3h 333 111\n222\n333
----------->333
444 444 111\n222\n333
4g 444\n111\n222\n333
----------->444\n111\n222\n333
1-10
11-22
22-33
34-end
7、sed特殊用法
sed -n '/root/w a.txt' #将比對行輸出到檔案
sed '/root/r abc.txt' /etc/passwd #把abc.txt的檔案内容讀入到root比對行後
sed -n '/root/w a.txt'
sed -n '/root/{=;p}' /etc/passwd #列印行号和比對root的行
sed -n '/root/{n;d}' /etc/passwd #将比對root行的下一行删除
sed -n '/root/{n;d}' /etc/passwd #将比對root行和下一行都删除
sed '22{h;d};23,33{h;d};44g' pass
8、sed 腳本編寫方法
<1>從檔案讀入指令
sed -f sed.sh
sed.sh檔案内容:
s/root/yerik/p
s/bash/csh/p
<2>直接運作腳本 ./sed.sh /etc/passwd
#!/bib/sed -f
###################################
二、sed練習
1,删除檔案每行的第一個字元。
sed -n 's/^.//gp' /etc/passwd
sed -nr 's/(.)(.*)/\2/p' /etc/passwd
2,删除檔案每行的第二個字元。
sed -nr 's/(.)(.)(.*)/\1\3/p' /etc/passwd
3,删除檔案每行的最後一個字元。
sed -nr 's/.$//p' /etc/passwd
sed -nr 's/(.*)(.)/\1/p' /etc/passwd
4,删除檔案每行的倒數第二個字元。
sed -nr 's/(.*)(.)(.)/\1\3/p' /etc/passwd
5,删除檔案每行的第二個單詞。
sed -nr 's/([^a-z]*)([a-z]+)([^a-z]+)([a-z]+)(.*)/\1\2\3\5/p' /etc/passwd
6,删除檔案每行的倒數第二個單詞。
sed -nr 's/(.*)([^a-z]+)([a-z]+)([^a-z]+)([a-z]+)([^a-z]*)/\1\2\4\5\6/p' /etc/samba/smb.conf
7,删除檔案每行的最後一個單詞。
sed -nr 's/(.*)([^a-z]+)([a-z]+)([^a-z]*)/\1\2\4/p' /etc/samba/smb.conf
8,交換每行的第一個字元和第二個字元。
sed -nr 's/(.)(.)(.*)/\2\1\3/p' /etc/passwd
9,交換每行的第一個單詞和第二個單詞。
sed -nr 's/([^a-z]*)([a-z]+)([^a-z]+)([a-z]+)(.*)/\1\4\3\2\5/p' /etc/samba/smb.conf
10,交換每行的第一個單詞和最後一個單詞。
sed -nr 's/([^a-z]*)([a-z]+)([^a-z]+)([a-z]+)(.*)/\1\4\3\2\5/p' /etc/passwd
11,删除一個檔案中所有的數字。
sed 's/[0-9]*//g' /etc/passwd
12,删除每行開頭的所有空格。
sed -n 's/^\ *//p' /etc/samba/smb.conf
sed -nr 's/( *)(.*)/\2/p' testp
13,用制表符替換檔案中出現的所有空格。
sed -n 's/\ /\t/gp' pass
14,把所有大寫字母用括号()括起來。
sed -nr 's/([a-z])/(&)/gp' testp
sed -n 's/[a-z]/(&)/gp' testp
15,列印每行3次。
sed 'p;p' pass
16,隔行删除。
sed -n '1~2p' pass
17,把檔案從第22行到第33行複制到第44行後面。
sed '1,21h;22h;23,33h;44g' pass
18,把檔案從第22行到第33行移動到第44行後面。
19,隻顯示每行的第一個單詞。
sed -nr 's/([^a-z]*)([a-z]+)([^a-z]+)(.*)/\2/p' /etc/passwd
20,列印每行的第一個單詞和第三個單詞。
sed -nr 's/([^a-z]*)([a-z]+)([^a-z]+)([a-z]+)([^a-z]+)([a-z]+)(.*)/\2--\4/p' /etc/passwd
21,将格式為 mm/yy/dd 的日期格式換成 mm;yy;dd
date +%m/%y/%d |sed -n 's#/#;#gp'
22, 逆向輸出
cat a.txt
abc
def
xyz
輸出樣式變成