技.藝.道:查漏補缺之-sed

一、sed

1.簡介

sed是一個用來編輯文本的指令。

2.示例

基本語法:

sed [options] '[匹配模式] sed的內部命令' file1

[options]:默認是腳本命令模式“-e”,即通過後面單引號中的腳本命令來操作指定的文本文件,也可以通過"-f" 改成腳本文件模式,即用指定sed腳本文件操作指定文本文件。還有其他模式,需要的時候可以查看"sed -h",我翻譯了一版在附錄中了。

匹配模式:

行數:指定操作的行號,直接用數字即可,如sed ‘10 a\abcd’ log.txt。表示操作的對象是文件的第10行。

行區間:固定起止的行區間:'3,8'表示第3行到第8行。

不固定起止的行區間:'5,$'表示從第5行到結尾。

 

 

第一類:增

‘{行號} a\{內容}’:每行後面追加一行{內容};

‘{行號} i\{內容}’:每行前面插入一行{內容}。

(行號不寫,則表示此次操作的對象是所有行)

例:

# 輸入文件原樣輸出,用來和後面的處理結果進行對照
root@ubuntu:/leonRain/test# cat log.txt 
1 aaa ccc awqr
2 ssss ddd eee lll
3 00 98u kjlh llx 223 hh 
4 233 ssSSs

# 在每行內容的下一行創建空行並插入指定內容,a,理解爲追加吧,自然在後面
root@ubuntu:/leonRain/test# sed 'a\heheda' log.txt
1 aaa ccc awqr
heheda
2 ssss ddd eee lll
heheda
3 00 98u kjlh llx 223 hh 
heheda
4 233 ssSSs
heheda

# 在每行內容的上一行創建空行並插入指定內容,i,理解爲插入吧,自然不在最後
root@ubuntu:/leonRain/test# sed 'i\heheda' log.txt
heheda
1 aaa ccc awqr
heheda
2 ssss ddd eee lll
heheda
3 00 98u kjlh llx 223 hh 
heheda
4 233 ssSSs

第二類:刪

‘{行號} d’

(行號不寫,則表示此次操作的對象是所有行)

# 原始輸入文件以作對照
root@ubuntu:/leonRain/test# cat log.txt 
1 aaa ccc awqr
2 ssss ddd eee lll
3 00 98u kjlh llx 223 hh 
4 233 ssSSs

# 對第4行數據執行刪除(d)操作
root@ubuntu:/leonRain/test# sed '4 d' log.txt 
1 aaa ccc awqr
2 ssss ddd eee lll
3 00 98u kjlh llx 223 hh 

# 對第2行數據執行刪除(d)操作
root@ubuntu:/leonRain/test# sed '2 d' log.txt 
1 aaa ccc awqr
3 00 98u kjlh llx 223 hh 
4 233 ssSSs

 

第三類:改

替換操作用s:

例:'s/aaa/lalala/'   將aaa替換爲lalala。

root@ubuntu:/leonRain/test# cat log.txt
1 aaa ccc awqr
2 ssss ddd eee lll
3 00 98u kjlh llx 223 hh 
4 233 ssSSs

root@ubuntu:/leonRain/test# sed 's/aaa/lalala/' log.txt
1 lalala ccc awqr
2 ssss ddd eee lll
3 00 98u kjlh llx 223 hh 
4 233 ssSSs

修改用c:將第二行修改成"aaaccc"

root@ubuntu:/leonRain/test# cat log.txt
1 aaa ccc awqr
2 ssss ddd eee lll
3 00 98u kjlh llx 223 hh 
4 233 ssSSs

root@ubuntu:/leonRain/test# sed '2c aaaccc' log.txt
1 aaa ccc awqr
aaaccc
3 00 98u kjlh llx 223 hh 
4 233 ssSSs

第四類:查

全文查看

類比SQL:

select * from tableA;

命令:

root@ubuntu:/leonRain/test# sed '' log.txt
1 aaa ccc awqr
2 ssss ddd eee lll
3 00 98u kjlh llx 223 hh 
4 233 ssSSs

查看指定行

p:打印出找到的行(原始的數據也會打印一份,若想不打印原始數據,可以使用-n)

'1p':查看第一行數據。

root@ubuntu:/leonRain/test# cat log.txt
1 aaa ccc awqr
2 ssss ddd eee lll
3 00 98u kjlh llx 223 hh 
4 233 ssSSs

root@ubuntu:/leonRain/test# sed '1p' log.txt
1 aaa ccc awqr
1 aaa ccc awqr
2 ssss ddd eee lll
3 00 98u kjlh llx 223 hh 
4 233 ssSSs

root@ubuntu:/leonRain/test# sed -n '1p' log.txt
1 aaa ccc awqr

‘/aaa/ p’:查看含有字符串“aaa”的行。(兩個正斜槓之間的內容可以是正則表達式)

root@ubuntu:/leonRain/test# cat log.txt 
1 aaa ccc awqr
2 ssss ddd eee lll
3 00 98u kjlh llx 223 hh 
4 233 ssSSs

root@ubuntu:/leonRain/test# sed -n '/aaa/ p' log.txt 
1 aaa ccc awqr

附錄:sed -h 內容及翻譯

root@ubuntu:/leonRain/test# sed -h
sed: invalid option -- 'h'
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
用法:sed [選項]..{只有其他腳本纔有腳本} [輸入文件]...
  -n, --quiet, --silent
                 suppress automatic printing of pattern space
				 禁止自動打印圖案空間【言下之意:只打印模式匹配到的行】
  -e script, --expression=script
                 add the script to the commands to be executed
				 將腳本添加到要執行的命令
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
				 將腳本文件的內容添加到要執行的命令中
  --follow-symlinks
                 follow symlinks when processing in place
				 處理到位時遵循符號鏈接
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if SUFFIX supplied)
				 在適當位置編輯文件(如果提供SUFFIX,則進行備份)
                 【言下之意是:直接修改文件內容】
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
				 爲“l”命令指定所需的換行長度
  --posix
                 disable all GNU extensions.
				 禁用所有GNU擴展。
  -E, -r, --regexp-extended
                 use extended regular expressions in the script
				 在腳本中使用擴展的正則表達式
                 (for portability use POSIX -E).
				 便攜式使用POSIX -E
  -s, --separate
                 consider files as separate rather than as a single,
                 continuous long stream.
				 將文件視爲獨立文件,而不是單個連續的長文件流。
      --sandbox
                 operate in sandbox mode.
				 在沙盒模式下運行。
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
				 從輸入文件中加載最少量的數據,並更頻繁地刷新輸出緩衝區
  -z, --null-data
                 separate lines by NUL characters
				 用NUL字符分隔行
      --help     display this help and exit
	             顯示此幫助並退出
      --version  output version information and exit
                 輸出版本信息並退出

If no -e, --expression, -f, or --file option is given, then the first 
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.
如果未提供-e,-expression,-f或--file選項,
則將第一個非選項參數用作要解釋的sed腳本。 
其餘所有參數均爲輸入文件的名稱; 
如果未指定輸入文件,那麼將讀取標準輸入。
【言下之意:-e是默認選項】

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章