sed的总结笔记
那我先建立一個測試的額數據[root@g script]# cat example101,John Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager[root@g script]# 首先說明一下sed的工作流程 ?sed讀入一行放入模式空間(保留空間,在家里面有個百寶箱,你將自己的東西放進去) 后面再跟如命令。輸出再講模式空間清空。然后讀入第二行一直讀到最后。當然第一步就是打印命令的實現:1:用P來打印[root@g script]# sed p example101,John Doe,CEO101,John Doe,CEO102,Jason Smith,IT Manager102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin103,Raj Reddy,Sysadmin104,Anand Ram,Developer104,Anand Ram,Developer105,Jane Miller,Sales Manager105,Jane Miller,Sales Manager[root@g script]# 當用P打印的話會輸出2行那怎樣讓他取消打印2行呢》》 ?用-n參數#sed -n ?'p' example打印第一行或打印1-3行[root@g script]#[root@g script]# sed -n '1 p' example101,John Doe,CEO[root@g script]#[root@g script]#[root@g script]# sed -n '1,3 p' example101,John Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin用正則表達式結合sed 命令使用一定注意要注意模式 ?在‘’中加上// ?如‘/Jason/ p’打印Jason 所在行[root@g script]# sed -n '/Jason/ p' example 102,Jason Smith,IT Manager[root@g script]# 打印Jason至Jane所在的行[root@g script]# sed -n '/Jason/,/Jane/ p' example102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager打印以數字開頭的所在行[root@g script]# sed -n '/^[0-9]/ p' example101,John Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager打印以Manage結尾的行[root@g script]# sed -n '/Manager$/ p' example102,Jason Smith,IT Manager105,Jane Miller,Sales Manager還可以結合標簽使用,打印以數字開頭,連續重復三次的行[root@g script]# sed -n '/^[0-9]\{3\}/ p' example101,John Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager[root@g script]# 隔一行打印一次[root@g script]# sed -n '1~2 p' example101,John Doe,CEO103,Raj Reddy,Sysadmin105,Jane Miller,Sales Manager2:刪除命令d和前面的一樣;刪除出現 Developer出現的行[root@g script]#[root@g script]# sed ? '/Developer/ d ' example101,John Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin105,Jane Miller,Sales Manager3:連接命令[root@g script]# sed -e '/Developer/ d' -e '/105/ d' example101,John Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin4:神奇的替換命令那先記個格式吧sed的替換格式是sed ?'s///' example中間家3個斜杠用來做替換替換分為用地址匹配和用樣式匹配的。先來看看地址替換:[root@g script]# sed '1 s/John/jesn/' example101,jesn Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager樣式替換:[root@g script]#[root@g script]# sed -n '/102/ s/Manager/jesn/p' example102,Jason Smith,IT jesn也可以兩者結合更加精確:[root@g script]#[root@g script]# sed '1,/John/ s/CEO/CTO/' example101,John Doe,CTO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager[root@g script]# 將替換的行寫到另一個文件中去(僅僅是替換的行)root@g script]# sed '1,/John/ s/CEO/CTO/w output.txt' example101,John Doe,CTO[root@g script]# cat output.txt101,John Doe,CTO全局替換 g:[root@g script]# sed 's/1/CTO/' exampleCTO01,John Doe,CEOCTO02,Jason Smith,IT ManagerCTO03,Raj Reddy,SysadminCTO04,Anand Ram,DeveloperCTO05,Jane Miller,Sales Managg后面加上數字便是改行中所碰到的第幾個[root@g script]#[root@g script]# sed 's/1/CTO/g2' example10CTO,John Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager[root@g script]# 5:不區分大小寫的操作 i[root@g script]# sed 's/jason/AAA/gi' example101,John Doe,CEO102,AAA Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager[root@g script]# 6:將原串的數據加到replace的數據中去 ?&[root@g script]# sed 's/jason/[&]/i' example101,John Doe,CEO102,[Jason] Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager7:將前面開頭的字符串都帶個{}; ?[root@g script]# sed 's/^[0-9]\{3\}/{&}/' example{101},John Doe,CEO{102},Jason Smith,IT Manager{103},Raj Reddy,Sysadmin{104},Anand Ram,Developer{105},Jane Miller,Sales Manager8:sed直接將原文件就修改的 ?-i[root@g script]# sed -i 's/john/jesn/i' example[root@g script]# cat example101,jesn Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager9:作業源文件替換成如下格式:[101][102][103][104][105]答案:解法一:[root@g script]# sed 's/,.*//' example | sed 's/^[0-9]\{3\}/[&]/'[101][102][103][104][105]其中的第一個sed 將以逗號開頭的后面所有用空格替換 ?那么 .* ?就特別重要 表示,后面有零個或多個字符 如果只跟* 的話那么只能替換出逗號;解法二:[root@g script]# sed 's/\(^[0-9]*\)\(,.*\)/{\1}/' example{101}{102}{103}{104}{105}紅色標注的是使用標簽進行替代。解法三:[root@g script]# sed 's/\([^,]*\).*/[\1]/' example[101][102][103][104][105]紅字表示的是以非,開頭的進行匹配,則匹配到數字后就結束了。 則在打出.* 就是匹配全部 ?然后用標簽\1 替代10:截取ip地址:解法一:使用cut[root@g script]# ifconfig eth0 | grep 'inet addr' | cut -d: -f 2 | cut -d" " -f 18.8.8.2解法二:使用sed[root@g script]#[root@g script]# ifconfig eth0 | sed -n '2 p' | sed 's/\([^0-9]*\)//' | sed 's/ .*//'8.8.8.2sed先找出那行,然后將非數字開頭的用空格進行匹配,就把8.8.8.2開頭的字幕匹配掉了。然后再以空格開始的用空格進行匹配掉解法三:使用高端大氣上檔次的標簽[root@g script]# ifconfig eth0 | sed -n '2 p' | sed 's/\([^0-9]*\)\([0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}\).*/\2/'8.8.8.2哈哈: ?那就給你說下怎么解決的 ? ,首先依舊是將這行找到,然后將要匹配的東西在中間分成三個標簽,然后將各個標簽描述出來,最后只需打印出第二個標簽就ok了。 ?inet addr:8.8.8.2Bcast:8.8.8.255 Mask:255.255.255.0就是只要求把藍色的部分打出來就好了解法四:awk [root@g script]#[root@g script]# ifconfig eth0 | sed -n '2 p' | awk -F":" '{print $2}' | awk -F" " '{print $1}'8.8.8.211:去除注釋和空行:解法一:[root@g script]# sed 's/#.*//' my.cnf | sed '/^$/ d'[client]port = 3306socket = /tmp/mysql.sock[mysqld]port = 3306socket = /tmp/mysql.sockskip-lockingkey_buffer = 16Mmax_allowed_packet = 1Mtable_cache = 64sort_buffer_size = 512Knet_buffer_length = 8K^$表示空行11:追加命令 a在第二行后添加一行[root@g script]# sed '2 a 203,ridy,R&D' example101,jesn Doe,CEO102,Jason Smith,IT Manager203,ridy,R&D103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager在行尾添加一行[root@g script]# sed '$ a 205,nihao salesman' example101,jesn Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager205,nihao salesman在后面添加多行
[root@g script]# sed '/jesn/ a\> 201 nihao ,smith\> 202 nihao ,jnny> ' example101,jesn Doe,CEO201 nihao ,smith202 nihao ,jnny102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager
在前面添加多行 i[root@g script]#[root@g script]# sed '/jesn/ i\201 nihao ,smith\202 nihao ,jnny' example201 nihao ,smith202 nihao ,jnny101,jesn Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager12:change改變更改命令: c[root@g script]#[root@g script]# sed '/jesn/ c ridy' exampleridy102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager
13:打印行號:[root@g script]# sed '/jesn/ = ' example1101,jesn Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager14:字符轉換:y///[root@g script]# sed 'y/j/J/' example ?101,Jesn Doe,CEO102,Jason Smith,IT Manager103,RaJ Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager15:退出命令,當讀到第5行時讓他退出 q[root@g script]# sed '3 q' example101,jesn Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin
[root@g script]# sed '/jesn/ a\> 201 nihao ,smith\> 202 nihao ,jnny> ' example101,jesn Doe,CEO201 nihao ,smith202 nihao ,jnny102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager
在前面添加多行 i[root@g script]#[root@g script]# sed '/jesn/ i\201 nihao ,smith\202 nihao ,jnny' example201 nihao ,smith202 nihao ,jnny101,jesn Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager12:change改變更改命令: c[root@g script]#[root@g script]# sed '/jesn/ c ridy' exampleridy102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager
13:打印行號:[root@g script]# sed '/jesn/ = ' example1101,jesn Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager14:字符轉換:y///[root@g script]# sed 'y/j/J/' example ?101,Jesn Doe,CEO102,Jason Smith,IT Manager103,RaJ Reddy,Sysadmin104,Anand Ram,Developer105,Jane Miller,Sales Manager15:退出命令,當讀到第5行時讓他退出 q[root@g script]# sed '3 q' example101,jesn Doe,CEO102,Jason Smith,IT Manager103,Raj Reddy,Sysadmin
轉載于:https://blog.51cto.com/jesnridy/1386465
總結
- 上一篇: android 仿人人网滑动侧边栏
- 下一篇: iptables二之防火墙SNAT源地址