sed語法支持兩種格式,分別是
對以上的幾個英語字符進行說明:
需要說明的是,使用時address和command會挨在一起,中間沒有空格,上面語法中間加空格是為了說明這是兩個部分
以下為sed命令的工作流程圖
這裡的選項就是指sed之後的options,常見的選項具體如下:
-e ,--expression=script 以選項中指定的script參數來處理文本文件,可以指定多個命令-f file,--files=script以文件中的指定的參數來處理文本文件-n ,--quiet ,--slient取消默認輸出,sed默認會輸出所有文本內容,使用-n參數後只顯示處理過的行-r ,--regexp-extended支持擴展正則表達式-i,--in-place[=SUFFIX]直接修改文件內容
這裡的地址就是語法格式中的address部分,是在單引號裡的前一部分 。
替換標記說明linenumber指定特定行號startline,endline指定起始行號和結束行號linenumber,+nn為數字,表示從指定行號向後n行/pattern/已正則表達式表示的匹配模式/pattern1, /pattern2/從模式匹配1到模式匹配2pattern/,x在給定行號上查詢包含模式的行x,/pattern/通過行號和模式查詢匹配的行x,y!查詢不包含指定行號x和y的行
這裡的指令就是command,這個一般放在單引號內,同時和要處理的字符放在一起,只是command放在字符的前面。
a 追加,向匹配行後面插入內容 i 插入,向匹配行前插入內容c 更改,更改匹配行的內容d 刪除,刪除匹配行的內容s 使用替換模式替換相應模式s/patten/newstring 替換,把patten匹配到的內容換成newstringp 列印,列印出匹配的內容,通過與-n選項配合使用= 標號,用來將匹配的行前標號n 讀取下一行,遇到n會自動自動跳入下一行r 將內容讀入文件{} 命令間的傳遞,類似於管道符|w 將匹配內容寫入文件W 將匹配到行的第一行,保存到file中。
正在匹配在上面已經介紹到,詳情請見正則表達式介紹。具體見:
以下主要針對地址指令的實戰練習
先準備數據文件aa.txt,通過cat查看aa.txt文件內容
[root@localhost test]# cat aa.txt 總用量 28lrwxrwxrwx. 1 root root 7 9月 10 06:12 bin -> usr/bindr-xr-xr-x. 6 root root 4096 9月 10 06:44 bootdrwxr-xr-x. 20 root root 3360 9月 29 05:19 devdrwxr-xr-x. 3 root root 20 9月 14 03:08 docker_registrydrwxr-xr-x. 2 root root 41 9月 17 06:23 docker_studydrwxr-xr-x. 151 root root 12288 9月 29 05:20 etcdrwxr-xr-x. 3 root root 18 9月 10 06:57 homelrwxrwxrwx. 1 root root 7 9月 10 06:12 lib -> usr/liblrwxrwxrwx. 1 root root 9 9月 10 06:12 lib64 -> usr/lib64drwxr-xr-x. 2 root root 6 4月 11 2018 mediadrwxr-xr-x. 2 root root 21 9月 19 21:51 mntdrwxr-xr-x. 4 root root 34 9月 11 06:53 optdr-xr-xr-x. 307 root root 0 9月 29 05:19 procdr-xr-x---. 7 root root 266 9月 29 05:20 rootdrwxr-xr-x. 45 root root 1320 9月 29 06:26 runlrwxrwxrwx. 1 root root 8 9月 10 06:12 sbin -> usr/sbindrwxr-xr-x. 2 root root 6 4月 11 2018 srvdr-xr-xr-x. 13 root root 0 9月 29 05:19 sysdrwxr-xr-x. 3 root root 43 9月 26 19:11 testdrwxrwxrwt. 24 root root 4096 9月 29 06:38 tmpdrwxr-xr-x. 13 root root 155 9月 10 06:12 usrdrwxr-xr-x. 22 root root 4096 9月 10 06:45 var
顯示文件中的第2行內容 .
5.刪除包含d字符的行
6.列印匹配以d開頭到以l開頭的行(多次匹配)
7.列印dev到第5行,其中不包含第5行
8.列印第2行到匹配到dev之間的行
9.除3~16行之間的數據,將其它數據輸出
以下主要正則正則匹配的實戰練習
先準備數據文件,通過cat命令查看java.conf文件
[root@localhost test]# [root@localhost test]# cat java.conf # System-wide Java configuration file -*- sh -*-# Location of jar files on the systemJAVA_LIBDIR=/usr/share/java# Location of arch-specific jar files on the systemJNI_LIBDIR=/usr/lib/java# Root of all JVM installationsJVM_ROOT=/usr/lib/jvm# You can define a system-wide JVM root here if you're not using the# default one.## If you have a base JRE package installed# (e.g. java-1.6.0-openjdk):#JAVA_HOME=$JVM_ROOT/jre## If you have a devel JDK package installed# (e.g. java-1.6.0-openjdk-devel):#JAVA_HOME=$JVM_ROOT/java# Options to pass to the java interpreter#JAVACMD_OPTS=# You can disable ABRT Java Connector by setting JAVA_ABRT to "off".# See: https://github.com/jfilak/abrt-java-connector/#JAVA_ABRT=off[root@localhost test]#
以下主要針對各命令的練習,如s,i,a,r,w,p等
13.在前三行的行首添加hello 。
以上所有的替換或變化都不會改變原文件的內容 。
23.將指定文件abc.txt中的內容插入到info文件的第三行