第七章 流編輯什麼是sed挑選編輯器
UNIX/Linux世界葉,有許多的文字編輯器可供選擇。例如,最常使用的VI和emacs。在有了自己最熟悉的編輯利器後,才能輕鬆處理UNIX下各種管理和編輯任務。
像VI,emacs這類編輯器,被稱為互動式編輯器。互動式編輯器雖然很棒,但是當我們需要在程式中完成文本處理工作時,它就幫不上忙了。此時就需要一些能夠在命令列完成的編輯工具。
我們期待一切管理流程都能自動化,包括能夠以批處理的方式編輯檔案。許多文本編輯的需求都是對文本的每一行進行相同的操作。這樣的處理就能夠用sed來完成。
sed號稱流編輯器。什麼是流編輯器呢?流編輯器可以對從管道這樣的標準輸入接收的資料進行編輯。因此,無需將要編輯的資料存放區在磁碟上的檔案中。因為可以輕易將資料管道輸出到sed,所以,將sed用作強大的shell指令碼中長而複雜的管道很容易。
Sed的版本
查看版本方式
[houchangren@ebsdi-23260-oozie ~]$ sed--versionGNU sed version 4.1.5Copyright (C) 2003 Free SoftwareFoundation, Inc.This is free software; see the source forcopying conditions. There is NOwarranty; not even for MERCHANTABILITY orFITNESS FOR A PARTICULAR PURPOSE,to the extent permitted by law.
Sed執行個體Sed的工作方式
sed通過對輸入資料執行任意數量使用者指定的編輯操作(命令)。sed是基於行的,因此
按順序對每一行執行命令。然後,sed將其結果寫入標準輸出(stdout ),它不修改任何輸入
檔案。
參數列表參考:
http://www.cnblogs.com/edwardlost/archive/2010/09/17/1829145.html
樣本:
[houchangren@ebsdi-23260-oozie shell]$ head-n5 /etc/passwd > /tmp/passwd.bak[houchangren@ebsdi-23260-oozie shell]$cat /tmp/passwd.bakroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin[houchangren@ebsdi-23260-oozie shell]$ sed-e 'd' /tmp/passwd.bak[houchangren@ebsdi-23260-oozie shell]$ sed-e '1d' /tmp/passwd.bakbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin[houchangren@ebsdi-23260-oozie shell]$ sed-e '3d' /tmp/passwd.bakroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
上方的‘d’就是參數應用就是預設的就是刪除每行,指定數字就刪除第幾行的記錄,第一次刪除了第一行1行顯示後四條。第二次刪除第3條,顯示剩餘其他4條。
NOTE
在該例中,還有幾件事要注意:
1)根本沒有修改/tmp/passwd.bak這還是因為sed唯讀取在命令列指定的檔案,將其用作輸入-它不試圖修改該檔案。
2)要注意的事是sed是面向行的。’d’命令不是簡單地告訴sed一下子刪除所有輸入資料。相反,sed逐行將/etc/passwd.bak的每一行讀入其稱為模式緩衝區的內部緩衝區一旦將一行讀入模式緩衝區,它就執行’d’命令,然後列印模式緩衝區的內容(在本例中沒有內容).如果不使用地址,命令將應用到所有行。
3)括起‘d’命令的單引號的用法。養成使用單引號來括起scd命令的習慣是個好主意,這樣可以禁用shell擴充。
Sed工作的位址範圍
指定命令的操作列區域,比如下邊1-2行和3-6行,當然沒有第6行,就只有多少刪除多少了。
[houchangren@ebsdi-23260-oozie shell]$ sed-e '1,2d' /tmp/passwd.bak
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[houchangren@ebsdi-23260-oozie shell]$ sed-e '3,6d' /tmp/passwd.bak
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
sed忽略注釋
[houchangren@ebsdi-23260-oozie shell]$ cat/etc/rc.local#!/bin/sh## This script will be executed *after* allthe other init scripts.# You can put your own initialization stuffin here if you don't# want to do the full Sys V style initstuff. touch /var/lock/subsys/localhive --service hiveserver &[houchangren@ebsdi-23260-oozie shell]$ sed-e '/^#/d' /etc/rc.local | more touch /var/lock/subsys/localhive --service hiveserver & [houchangren@ebsdi-23260-oozie shell]$ sed-e '/^[^#]/d' /etc/rc.local | more#!/bin/sh## This script will be executed *after* allthe other init scripts.# You can put your own initialization stuffin here if you don't# want to do the full Sys V style initstuff.
規則運算式
Sed中使用的規則運算式字元
| |
|
字元 |
描述 |
|
^ |
與行首匹配 |
|
$ |
與行尾匹配 |
|
. |
與任意一個字元匹配 |
|
* |
與前一個字元的零個或多個出現匹配 |
|
[] |
與[]之內的所有字元匹配 |
| |
Sed規則運算式執行個體
| |
|
規則運算式 |
描述 |
|
/./ |
將與包含至少一個字元的任何行匹配 |
|
/../ |
將與包含至少兩個字元的任何行匹配 |
|
/^#/ |
將與以‘#’開頭的任意行匹配,通常這是注釋 |
|
/}$/ |
將與‘}’結束的任意行匹配 |
|
/} *$/ |
注意在}後面有一個空格,這將與‘}’後而跟隨零個或多個空格結束的任意行匹配配 |
|
/[abc]/ |
將與包含小寫字母‘a,b,c的任意行匹配 |
|
/^[abc]/ |
將與以a,b,c任何開始的任何行匹配 |
| |
-n參數 這個選項告訴sed 除非明確要求列印模式空間,否則不這樣做。
[houchangren@ebsdi-23260-oozie shell]$ sed-n -e '/^[echo]/p' user_login.shecho " user $1 is on"elseecho " user $1 is off" [houchangren@ebsdi-23260-oozie shell]$ sed-n -e '/[abc]/p' user_login.sh#!/bin/bashfunction user_login(){echo " user $1 is on"echo " user $1 is off" [houchangren@ebsdi-23260-oozie shell]$ cata.c#include <stdio.h>#include <math.H> int main (){// int base,n;// scanf("%d,%d\n",&b,&n) }[houchangren@ebsdi-23260-oozie shell]$ sed-n -e '/main[[:space:]]*(/,/^ }/p' a.c | moreint main (){// int base,n;// scanf("%d,%d\n",&b,&n) }[houchangren@ebsdi-23260-oozie shell]$ sed-n -e '/^\//p' a.c | more// int base,n;// scanf("%d,%d\n",&b,&n) 強大的Sed功能替換
替換公式: sed -e s[符號][要替換的字元][符號][替換後的字元][符號][g]
執行個體:
[houchangren@ebsdi-23260-oozie data]$ cattwister.txt //查看一資訊I wish to wish the wish you wish to wish,but if you wish the wish the witch wishes, I won't wish the wish you wish towish.I wish to wish the wish you wish to wish,but if you wish the wish the witch wishes, I won't wish the wish you wish towish.I wish to wish the wish you wish to wish,but if you wish the wish the witch wishes, I won't wish the wish you wish towish.I wish to wish the wish you wish to wish,but if you wish the wish the witch wishes, I won't wish the wish you wish towish. [houchangren@ebsdi-23260-oozie data]$ sed-e 's/wish/want/' twister.txt //替換每一行的第一個匹配I want to wish the wish you wish to wish,but if you wish the wish the witch wantes, I won't wish the wish you wish towish.I want to wish the wish you wish to wish,but if you wish the wish the witch wantes, I won't wish the wish you wish towish.I want to wish the wish you wish to wish,but if you wish the wish the witch wantes, I won't wish the wish you wish towish.I want to wish the wish you wish to wish,but if you wish the wish the witch wantes, I won't wish the wish you wish towish. [houchangren@ebsdi-23260-oozie data]$ sed-e 's/wish/want/g' twister.txt //替換匹配到的所有的I want to want the want you want to want,but if you want the want the witch wantes, I won't want the want you want towant.I want to want the want you want to want,but if you want the want the witch wantes, I won't want the want you want towant.I want to want the want you want to want,but if you want the want the witch wantes, I won't want the want you want towant.I want to want the want you want to want,but if you want the want the witch wantes, I won't want the want you want towant. [houchangren@ebsdi-23260-oozie data]$ sed-e '1,2s/wish/want/' twister.txt //替換1到2行的第一個匹配I want to wish the wish you wish to wish,but if you wish the wish the witch wantes, I won't wish the wish you wish towish.I wish to wish the wish you wish to wish,but if you wish the wish the witch wishes, I won't wish the wish you wish towish.I wish to wish the wish you wish to wish,but if you wish the wish the witch wishes, I won't wish the wish you wish towish.I wish to wish the wish you wish to wish,but if you wish the wish the witch wishes, I won't wish the wish you wish towish. [houchangren@ebsdi-23260-oozie data]$ sed-e '1,2s/wish/want/g' twister.txt //替換1到2行的所有匹配I want to want the want you want to want,but if you want the want the witch wantes, I won't want the want you want towant.I wish to wish the wish you wish to wish,but if you wish the wish the witch wishes, I won't wish the wish you wish towish.I wish to wish the wish you wish to wish,but if you wish the wish the witch wishes, I won't wish the wish you wish towish.I wish to wish the wish you wish to wish,but if you wish the wish the witch wishes, I won't wish the wish you wish towish. [houchangren@ebsdi-23260-oozie data]$
因為有的時候需要操作’/’符號,那麼再用‘/’線
來做符號就不方便了,可以改成其他的,比如冒號‘:’
[houchangren@ebsdi-23260-oozie data]$ cathivepath.txt/usr/local/hive-0.7.1-cdh3u6/bin/hive/usr/local/hive-0.7.1-cdh3u6/bin/hive/usr/local/hive-0.7.1-cdh3u6/bin/hive/usr/local/hive-0.7.1-cdh3u6/bin/hive[houchangren@ebsdi-23260-oozie data]$ sed-e s:/usr/local:/usr/lib:g hivepath.txt/usr/lib/hive-0.7.1-cdh3u6/bin/hive/usr/lib/hive-0.7.1-cdh3u6/bin/hive/usr/lib/hive-0.7.1-cdh3u6/bin/hive/usr/lib/hive-0.7.1-cdh3u6/bin/hive
過濾所有的html標籤執行個體
[houchangren@ebsdi-23260-oozie data]$ cattest.html<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.0 Transitional//EN"><HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <BODY> I'mtest html </BODY></HTML> [houchangren@ebsdi-23260-oozie data]$ sed-e 's/<[^>]*>//g' test.html New DocumentI'mtest html
組合命令組合多條命令
在同時使用多個命令的時候可以使用分號“;”
[houchangren@ebsdi-23260-oozie data]$ sed-e '=' fruit.txt // 等號=是顯示行號1%%banae 2banana 3apple4Presimmon5%%banae 6apple 7Banana 8orange 9presimmon[houchangren@ebsdi-23260-oozie data]$ sed-e '=;p' fruit.txt //顯示行號和列印預設會列印所以兩次1%%banae %%banae 2banana banana 3appleapple4PresimmonPresimmon5%%banae %%banae 6apple apple 7Banana Banana 8orange orange 9presimmonpresimmon[houchangren@ebsdi-23260-oozie data]$ sed-n -e '=;p' fruit.txt //指定-n後列印p參數的命令1%%banae 2banana 3apple4Presimmon5%%banae 6apple 7Banana 8orange 9Persimmon[houchangren@ebsdi-23260-oozie data]$ sed-n -e 'p' -e = fruit.txt //-e可以指定多個命令%%banae 1banana 2apple3Presimmon4%%banae 5apple 6Banana 7orange 8presimmon9
有時候當太多的命令需要串連一起執行的時候-e可能也不夠使用了,可以使用-f指定文本,然後在文本中編寫命令
[houchangren@ebsdi-23260-oozie data]$ cathivepath.txt/usr/local/hive-0.7.1-cdh3u6/bin/hive/usr/local/hive-0.7.1-cdh3u6/bin/hive/usr/local/hive-0.7.1-cdh3u6/bin/hive/usr/local/hive-0.7.1-cdh3u6/bin/hive//1d是刪除一行,接著是/usr/local替換成/usr/bin,接著列印,接著顯示行號[houchangren@ebsdi-23260-oozie data]$ cat../sed/test.sed 1ds:/usr/local/:/usr/lib/:gp=[houchangren@ebsdi-23260-oozie data]$ sed-n -f ../sed/test.sed hivepath.txt/usr/lib/hive-0.7.1-cdh3u6/bin/hive2/usr/lib/hive-0.7.1-cdh3u6/bin/hive3/usr/lib/hive-0.7.1-cdh3u6/bin/hive4
將多條命令應用到一個位址範圍
在指定了一個位址範圍比如 1-5行,然後執行多個操作
[houchangren@ebsdi-23260-oozie data]$ head-n10 /etc/passwd > pwd.piece[houchangren@ebsdi-23260-oozie data]$ catpwd.pieceroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinnews:x:9:13:news:/etc/news:[houchangren@ebsdi-23260-oozie data]$ sed -n -e'1,5{s:/bin/bash:/bin/sh:g;s/:/|/g;p}' pwd.piece[houchangren@ebsdi-23260-oozie data]$ sed-n -e '1,5{s:/bin/bash:/bin/sh:g;s/:/|/g;p}' pwd.pieceroot|x|0|0|root|/root|/bin/shbin|x|1|1|bin|/bin|/sbin/nologindaemon|x|2|2|daemon|/sbin|/sbin/nologinadm|x|3|4|adm|/var/adm|/sbin/nologinlp|x|4|7|lp|/var/spool/lpd|/sbin/nologin[houchangren@ebsdi-23260-oozie data]$ [houchangren@ebsdi-23260-oozie data]$ cat../sed/pwd.sed1,5{s:/bin/bash:/bin/sh:gs/:/|/gp}[houchangren@ebsdi-23260-oozie data]$ sed-n -f ../sed/pwd.sed pwd.pieceroot|x|0|0|root|/root|/bin/shbin|x|1|1|bin|/bin|/sbin/nologindaemon|x|2|2|daemon|/sbin|/sbin/nologinadm|x|3|4|adm|/var/adm|/sbin/nologinlp|x|4|7|lp|/var/spool/lpd|/sbin/nologin執行個體的例子
LINUX和DOS/Windows系統純文字格式的換行方式是不同的。這個指令碼將UNIX風格的文本轉換成DOS/windows格式。你可能知道,基於DOS、windows的文字檔在每一行末尾有一個CR(斷行符號)和LF(換行),而UNIX文本只有一個換行。有時可能需要將某unix文本移至
Windows系統,該指令碼將為你執行必需的格式轉換.
>>> sed -e ‘s/$/\r/’myunix.txt > mydos.txt
在該指令碼中,‘$’規則運算式將與行的末尾匹配,而`\r'告訴sed在其之前插入個斷行符號。
在換行之前插入斷行符號,每一行立即就以CR/LF結束。
反之,有好多的時候下載的網路檔案是dos/windows檔案,在unix操作確實有問題,比如bash。用sed調用將把dos/windows格式的文本轉換成可信賴的unix格式
>>> sed –e ‘s/.$//’ mydos.txt >myunix.txt
該指令碼的工作原理很簡單:替代規則運算式與一行的的最末字元匹配,而該字元恰好就是斷行符號。我們用Null 字元替換它,從而將其從輸出中徹底刪除。如果使用改指令碼,並注意到己經刪除了輸出中每行的最末字元,那麼,你就指定了已經是UNIX式的文字檔。
Sed實踐
要求:
1. 把“小明“的名字改成“李小明”;
2. 刪除頭三行
3. 顯示5-10行
4. 刪除包含“排除“的行
5. 顯示所有生日在Nov和Dec之間的行
6. 所有姓張開頭的行,前邊標記 ***
7. 用“試用期人員“替換包含”試用“的行
8. 把劉發明的生日 1986/11/11
9. 刪除所有的空白行
10. 寫一個指令碼,將第一行插入Personnel File ,刪除所有以500結尾的工資,顯示檔案內容,把電話號碼和生日顛倒一下,在檔案的結尾添加the end。
11.把包含“試用”的行中姓名提取出來,然後正行替換成“試用人員:”+name
資料內容:
[houchangren@ebsdi-23260-oozie data]$ cat persons.txt小明:010-68239343:我是經理:1988/01/10:5000黎明:010-68239343:我是經理:1988/08/10:5000張學友:010-68239343:我是經理:1988/04/10:5000 周雲飛:010-68239343:業務排除了。:1988/10/10:500麗澤卡:010-68239343:沒有描述:1988/12/10:2000劉發明:010-68239343:沒有描述:1988/11/10:5340 大荒西:010-68239343:沒有描述:1988/10/10:1000斯蒂芬:010-68239343:沒有描述:1988/10/10:1000甄格大:010-68239343:試用期中.:1988/10/10:200 任務操作:1.[houchangren@ebsdi-23260-oozie data]$ sed-n -e 's:小明:李小明:gp' persons.txt李小明:010-68239343:我是經理:1988/01/10:50002.[houchangren@ebsdi-23260-oozie data]$ sed -e '1,3d' persons.txt 周雲飛:010-68239343:業務排除了。:1988/10/10:500麗澤卡:010-68239343:沒有描述:1988/12/10:2000劉發明:010-68239343:沒有描述:1988/11/10:5340 大荒西:010-68239343:沒有描述:1988/10/10:1000斯蒂芬:010-68239343:沒有描述:1988/10/10:1000甄格大:010-68239343:試用期中.:1988/10/10:2003.周雲飛:010-68239343:業務排除了。:1988/10/10:500麗澤卡:010-68239343:沒有描述:1988/12/10:2000劉發明:010-68239343:沒有描述:1988/11/10:5340 4.[houchangren@ebsdi-23260-oozie data]$ sed -e '/排除/d' persons.txt小明:010-68239343:我是經理:1988/01/10:5000黎明:010-68239343:我是經理:1988/08/10:5000張學友:010-68239343:我是經理:1988/04/10:5000 麗澤卡:010-68239343:沒有描述:1988/12/10:2000劉發明:010-68239343:沒有描述:1988/11/10:5340 大荒西:010-68239343:沒有描述:1988/10/10:1000斯蒂芬:010-68239343:沒有描述:1988/10/10:1000甄格大:010-68239343:試用期中.:1988/10/10:200 5.[houchangren@ebsdi-23260-oozie data]$ sed -n '/[:::][0-9]*[:/:]1[1-2]/p' persons.txt麗澤卡:010-68239343:沒有描述:1988/12/10:2000劉發明:010-68239343:沒有描述:1988/11/10:53406.[houchangren@ebsdi-23260-oozie data]$ sed -n 's/^張/***張/p' persons.txt***張學友:010-68239343:我是經理:1988/04/10:5000 7.[houchangren@ebsdi-23260-oozie data]$ sed -e 's/^.*試用.*$/試用期人員/g' persons.txt小明:010-68239343:我是經理:1988/01/10:5000黎明:010-68239343:我是經理:1988/08/10:5000張學友:010-68239343:我是經理:1988/04/10:5000 周雲飛:010-68239343:業務排除了。:1988/10/10:500麗澤卡:010-68239343:沒有描述:1988/12/10:2000劉發明:010-68239343:沒有描述:1988/11/10:5340 大荒西:010-68239343:沒有描述:1988/10/10:1000斯蒂芬:010-68239343:沒有描述:1988/10/10:1000試用期人員 8.[houchangren@ebsdi-23260-oozie data]$ sed -n -e '/劉發明/s/:[0-9]*\/.*\/.*:/:1986\/11\/11:/gp' persons.txt劉發明:010-68239343:沒有描述:1986/11/11:53409.[houchangren@ebsdi-23260-oozie data]$ sed -e '/^$/d' persons.txt小明:010-68239343:我是經理:1988/01/10:5000黎明:010-68239343:我是經理:1988/08/10:5000張學友:010-68239343:我是經理:1988/04/10:5000周雲飛:010-68239343:業務排除了。:1988/10/10:500麗澤卡:010-68239343:沒有描述:1988/12/10:2000劉發明:010-68239343:沒有描述:1988/11/10:5340大荒西:010-68239343:沒有描述:1988/10/10:1000斯蒂芬:010-68239343:沒有描述:1988/10/10:1000甄格大:010-68239343:試用期中.:1988/10/10:200 10. [houchangren@ebsdi-23260-oozie data]$ cat ../sed/person.sed/500$/ds/\(.*\)\(:.*:\)\(.*\)\(:.*:\)\(.*\)/\1\4\3\2\5/g1i personnel file$a the end [houchangren@ebsdi-23260-oozie data]$sed -f ../sed/person.sed persons.txtpersonnel file小明:1988/01/10:我是經理:010-68239343:5000黎明:1988/08/10:我是經理:010-68239343:5000張學友:1988/04/10:我是經理:010-68239343:5000 麗澤卡:1988/12/10:沒有描述:010-68239343:2000劉發明:1988/11/10:沒有描述:010-68239343:5340 大荒西:1988/10/10:沒有描述:010-68239343:1000斯蒂芬:1988/10/10:沒有描述:010-68239343:1000甄格大:1988/10/10:試用期中.:010-68239343:200 the end 11.[houchangren@ebsdi-23260-oozie data]$ sed -n -e '/試用/s/\(.*\):.*:.*:.*:.*/試用期人員:\1/gp' persons.txt | more試用期人員:甄格大