這篇文章主要講解了“Linux下sed命令的用法介紹”,文中的講解內容簡(jiǎn)單清晰,易于學(xué)習與理解,下面請大家跟著(zhù)小編的思路慢慢深入,一起來(lái)研究和學(xué)習“Linux下sed命令的用法介紹”吧!
Linux sed命令詳細說(shuō)明
sed是一種用于過(guò)濾和轉換文本的流編輯器。用于對輸入流(文件或來(lái)自管道的輸入)執行基本文本轉換。
雖然sed在某些方面類(lèi)似于允許腳本編輯(如ed)的編輯器,但它的工作方式是只傳遞一次輸入,因此效率更高。
思考:
查看當前IP
[root@cjcos01 cjc]# ifconfig
通過(guò)ifconfig雖然可以查看IP,但是打印出很多并不關(guān)注的信息,如何去掉這部分無(wú)用的信息?
可以通過(guò)sed加grep實(shí)現,方法見(jiàn)后面的示例。
測試數據
[root@cjcos01 cjc]# cat t1.txt
tao花庵歌 tao花塢里tao花庵,tao花庵下tao花仙; tao花仙人種tao樹(shù),又摘tao花賣(mài)酒錢(qián)。 酒醒只在花前坐,酒醉還來(lái)花下眠; 半醒半醉日復日,花落花開(kāi)年復年。 但愿老死花酒間,不愿鞠躬車(chē)馬前; 車(chē)塵馬足富者趣,酒盞花枝貧者緣。 若將富貴比貧賤,一在平地一在天; 若將貧賤比車(chē)馬,他得驅馳我得閑。 別人笑我太瘋癲,我笑他人看不穿; 不見(jiàn)五陵豪杰墓,無(wú)花無(wú)酒鋤作田。
1 打印行
打印第二行
[root@cjcos01 cjc]# sed -n '2p' /cjc/t1.txt
tao花塢里tao花庵,tao花庵下tao花仙;
打印第2-5行
[root@cjcos01 cjc]# sed -n '2,5p' /cjc/t1.txt
tao花塢里tao花庵,tao花庵下tao花仙; tao花仙人種tao樹(shù),又摘tao花賣(mài)酒錢(qián)。 酒醒只在花前坐,酒醉還來(lái)花下眠; 半醒半醉日復日,花落花開(kāi)年復年。
打印第10行到結尾行
[root@cjcos01 cjc]# sed -n '10,$p' /cjc/t1.txt
別人笑我太瘋癲,我笑他人看不穿; 不見(jiàn)五陵豪杰墓,無(wú)花無(wú)酒鋤作田。
打印第2行,第6行,第8,9,10行
[root@cjcos01 cjc]# sed -n '2p;6p;8,10p' /cjc/t1.txt
tao花塢里tao花庵,tao花庵下tao花仙; 但愿老死花酒間,不愿鞠躬車(chē)馬前; 若將富貴比貧賤,一在平地一在天; 若將貧賤比車(chē)馬,他得驅馳我得閑。 別人笑我太瘋癲,我笑他人看不穿;
打印含有tao字的行
[root@cjcos01 cjc]# sed -n '/tao/p' /cjc/t1.txt
tao花庵歌 tao花塢里tao花庵,tao花庵下tao花仙; tao花仙人種tao樹(shù),又摘tao花賣(mài)酒錢(qián)。
打印"酒"字開(kāi)頭的行
[root@cjcos01 cjc]# sed -n '/^酒/p' /cjc/t1.txt
酒醒只在花前坐,酒醉還來(lái)花下眠;
打印"。"結尾的行
[root@cjcos01 cjc]# sed -n '/\。$/p' /cjc/t1.txt
tao花仙人種tao樹(shù),又摘tao花賣(mài)酒錢(qián)。 半醒半醉日復日,花落花開(kāi)年復年。 車(chē)塵馬足富者趣,酒盞花枝貧者緣。 若將貧賤比車(chē)馬,他得驅馳我得閑。 不見(jiàn)五陵豪杰墓,無(wú)花無(wú)酒鋤作田。
2 插入行
[root@cjcos01 cjc]# cp t1.txt t1.txt.bak
人為多愁少年老, 花為無(wú)愁老少年。 年老少年都不管,且將詩(shī)酒醉花前。
行前添加,寫(xiě)入源文件
[root@cjcos01 cjc]# sed -i '2i 人為多愁少年老,花為無(wú)愁老少年。' /cjc/t1.txt
[root@cjcos01 cjc]# cat t1.txt
tao花庵歌 人為多愁少年老,花為無(wú)愁老少年。 tao花塢里tao花庵,tao花庵下tao花仙; ......
行后添加(直接修改原文件)
[root@cjcos01 cjc]# sed -i '2a 年老少年都不管,且將詩(shī)酒醉花前。' /cjc/t1.txt
[root@cjcos01 cjc]# cat t1.txt
tao花庵歌 人為多愁少年老,花為無(wú)愁老少年。 年老少年都不管,且將詩(shī)酒醉花前。 tao花塢里tao花庵,tao花庵下tao花仙; ......
3 替換行(直接修改原文件)
[root@cjcos01 cjc]# sed -i '2c 閑來(lái)寫(xiě)就青山賣(mài),不使人間造孽錢(qián)。' /cjc/t1.txt
[root@cjcos01 cjc]# cat t1.txt
tao花庵歌 閑來(lái)寫(xiě)就青山賣(mài),不使人間造孽錢(qián)。 年老少年都不管,且將詩(shī)酒醉花前。 ......
4 替換字符
-n 's/old/new/p' 將文件中每行的第一個(gè)old字符換成new字符,打印出只發(fā)生變化的行,且源文件內容不變
[root@cjcos01 cjc]# sed -n 's/tao/荷/p' /cjc/t1.txt
荷花庵歌 荷花塢里tao花庵,tao花庵下tao花仙; 荷花仙人種tao樹(shù),又摘tao花賣(mài)酒錢(qián)。
-n 's/old/new/pg':將文件中全部的old字符換成new字符,打印出只發(fā)生變化的行,且源文件內容不變。
[root@cjcos01 cjc]# sed -n 's/tao/荷/pg' /cjc/t1.txt
荷花庵歌 荷花塢里荷花庵,荷花庵下荷花仙; 荷花仙人種荷樹(shù),又摘荷花賣(mài)酒錢(qián)。
-n 's/old/new/p3g' :將文件中每行從第3個(gè)old字符開(kāi)始換成new字符,打印出只發(fā)生變化的行,且源文件內容不變
[root@cjcos01 cjc]# sed -n 's/tao/荷/p3g' /cjc/t1.txt
tao花塢里tao花庵,荷花庵下荷花仙; tao花仙人種tao樹(shù),又摘荷花賣(mài)酒錢(qián)。
-i,將文件中每行的第一個(gè)old字符換成new字符,修改源文件內容
[root@cjcos01 cjc]# sed -i 's/tao/荷/g' /cjc/t1.txt
[root@cjcos01 cjc]# sed -i 's/荷/tao/g' /cjc/t1.txt
5 刪除行
刪除第2行
[root@cjcos01 cjc]# sed -i '2d' /cjc/t1.txt
刪除第3到5行
[root@cjcos01 cjc]# sed -i '3,5d' /cjc/t1.txt
刪除第2行,第4,5,6行
[root@cjcos01 cjc]# sed -i '2d;4,6d' /cjc/t1.txt
舉例:
例1: 只顯示ifconfig中的IP地址
[root@cjcos01 ~]# ifconfig |grep "inet"|grep -v "inet6"|grep -v "127.0.0.1"|grep -v "122.1"|sed 's/netmask.*//'|sed 's/^.*inet//' 192.168.38.10
例2:去掉ssh配置文件中的帶#行和空行,不修改源文件,將結果打印到前臺
[root@cjcos01 cjc]# echo >t1.txt
[root@cjcos01 cjc]# cat /etc/ssh/ssh_config > t1.txt
[root@cjcos01 cjc]# sed 's/#.*//g' /cjc/t1.txt |sed '/^$/d'
Host * GSSAPIAuthentication yes ForwardX11Trusted yes SendEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT SendEnv LC_IDENTIFICATION LC_ALL LANGUAGE SendEnv XMODIFIERS
例3:每一行結尾為.的換成!("."需要加轉義符),不改變源文件(指定-i會(huì )改變源文件)
[root@cjcos01 cjc]# sed -n 's/\.$/!/p' /cjc/t1.txt
GSSAPIAuthentication yes! ForwardX11Trusted yes! SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT!
例4:以H開(kāi)頭的行末尾加上@@@
[root@cjcos01 cjc]# sed -n 's/^H.*$/&@@@/p' /cjc/t1.txt
Host *@@@
sed幫助信息:
[root@cjcos01 ~]# sed --help Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]... -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) -c, --copy use copy instead of rename when shuffling files in -i mode -b, --binary does nothing; for compatibility with WIN32/CYGWIN/MSDOS/EMX ( open files in binary mode (CR+LFs are not treated specially)) -l N, --line-length=N specify the desired line-wrap length for the `l' command --posix disable all GNU extensions. -r, --regexp-extended use extended regular expressions in the script. -s, --separate consider files as separate rather than as a single continuous long stream. -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 --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. GNU sed home page: <http://www.gnu.org/software/sed/>. General help using GNU software: <http://www.gnu.org/gethelp/>. E-mail bug reports to: <bug-sed@gnu.org>. Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.
[root@cjcos01 ~]# man sed NAME sed - stream editor for filtering and transforming text SYNOPSIS sed [OPTION]... {script-only-if-no-other-script} [input-file]... DESCRIPTION Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text in a pipeline which particularly distinguishes it from other types of editors. ...... SEE ALSO awk(1), ed(1), grep(1), tr(1), perlre(1), sed.info, any of various books on sed, the sed FAQ (http://sed.sf.net/grabbag/tutorials/sedfaq.txt), http://sed.sf.net/grabbag/. The full documentation for sed is maintained as a Texinfo manual. If the info and sed programs are properly installed at your site, the command info sed
[root@cjcos01 ~]# info sed File: sed.info, Node: Top, Next: Introduction, Up: (dir) sed, a stream editor ******************** This file documents version 4.2.2 of GNU `sed', a stream editor. ......
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站