sample.txt:
this is a teacher
this is a professor
that is a boy
that is a girl
she is beautiful lady
he lost his way
sed命令使用变量
1. 搜索指定字符串的行,并打印出来
root# sed -n '/this/p' sample.txt
this is a teacher
this is a professor
将this以变量替换来搜索,并打印出来
root# word=this
root# sed -n '/'$word'/p' sample.txt
this is a teacher
this is a professor
2. 搜索指定行号,并打印出来
root# sed -n '2p' sample.txt
this is a professor
将行号以变量形式替换,并打印出来
root# lineno=2
root# sed -n "$lineno"'p' sample.txt
this is a professor
3. 搜索双模式匹配的行,并打印出来
(a):A与B关系
root# sed -n '/that/{/is/p}' sample.txt
that is a boy
that is a girl
将that和is以变量形式替换,并打印出来
root# sed -n '/'$word1'/{/'$word2'/p}' sample.txt
that is a boy
that is a girl
(b):A或B关系
root# sed -n '/that\|way/p' sample.txt 或者用( sed -n -e '/that/p' -e '/way/p' sample.txt)
that is a boy
that is a girl
he lost his way
4. 替换指定字符串,如用THAT 替换that
root# sed 's/that/THAT/g' sample.txt
this is a teacher
this is a professor
THAT is a boy
THAT is a girl
she is beautiful lady
he lost his way
只显示替换行
root# sed -n 's/that/THAT/g''p' sample.txt
THAT is a boy
THAT is a girl
将THAT以变量形式替换掉that,并显示替换行
root# sed -n 's/that/'$word'/g''p' sample.txt
THAT is a boy
THAT is a girl
word不加单引号则打印出:
$word is a boy
$word is a girl
5. 替换模式boy的匹配行中指定字符串that,并打印出来
root# sed -n '/boy/ s/that/THAT/g''p' sample.txt
THAT is a boy
将THAT以变量形式替换掉that,并显示替换行
root# sed -n '/boy/ s/that/'$word'/g''p' sample.txt
THAT is a boy
6. 替换模式(that与girl中有任意个字符)匹配行中指定字符串,并打印出来
root#sed -n '/that.*girl/ s/that/THAT/g''p' sample.txt
THAT is a girl
当模式中任意个字符用“.*”作为通配符。
7. 替换多模式匹配
多个匹配可以用匿名管道方式过滤
root#$ sed -n '/that/{/is/p}' sample.txt |sed -n 's/boy/BOY/g'p
that is a BOY
但是这样替换不能够将sample.txt文件中对应部分替换,如果要替换,可以先获取改行行号,然后做替换动作。
root# linenos=`sed -n '/that/{/is/=}' sample.txt`
root# echo $linenos
3 4
root# for lineno in $linenos
> do
> sed -i ''"$lineno"' {s/boy/BOY/g; s/girl/GIRL/g}' sample.txt
> done
root# cat sample.txt
this is a teacher
this is a professor
that is a BOY
that is a GIRL
she is beautiful lady
he lost his way
this is a teacher
this is a professor
that is a boy
that is a girl
she is beautiful lady
he lost his way
sed命令使用变量
1. 搜索指定字符串的行,并打印出来
root# sed -n '/this/p' sample.txt
this is a teacher
this is a professor
将this以变量替换来搜索,并打印出来
root# word=this
root# sed -n '/'$word'/p' sample.txt
this is a teacher
this is a professor
2. 搜索指定行号,并打印出来
root# sed -n '2p' sample.txt
this is a professor
将行号以变量形式替换,并打印出来
root# lineno=2
root# sed -n "$lineno"'p' sample.txt
this is a professor
3. 搜索双模式匹配的行,并打印出来
(a):A与B关系
root# sed -n '/that/{/is/p}' sample.txt
that is a boy
that is a girl
将that和is以变量形式替换,并打印出来
root# sed -n '/'$word1'/{/'$word2'/p}' sample.txt
that is a boy
that is a girl
(b):A或B关系
root# sed -n '/that\|way/p' sample.txt 或者用( sed -n -e '/that/p' -e '/way/p' sample.txt)
that is a boy
that is a girl
he lost his way
4. 替换指定字符串,如用THAT 替换that
root# sed 's/that/THAT/g' sample.txt
this is a teacher
this is a professor
THAT is a boy
THAT is a girl
she is beautiful lady
he lost his way
只显示替换行
root# sed -n 's/that/THAT/g''p' sample.txt
THAT is a boy
THAT is a girl
将THAT以变量形式替换掉that,并显示替换行
root# sed -n 's/that/'$word'/g''p' sample.txt
THAT is a boy
THAT is a girl
word不加单引号则打印出:
$word is a boy
$word is a girl
5. 替换模式boy的匹配行中指定字符串that,并打印出来
root# sed -n '/boy/ s/that/THAT/g''p' sample.txt
THAT is a boy
将THAT以变量形式替换掉that,并显示替换行
root# sed -n '/boy/ s/that/'$word'/g''p' sample.txt
THAT is a boy
6. 替换模式(that与girl中有任意个字符)匹配行中指定字符串,并打印出来
root#sed -n '/that.*girl/ s/that/THAT/g''p' sample.txt
THAT is a girl
当模式中任意个字符用“.*”作为通配符。
7. 替换多模式匹配
多个匹配可以用匿名管道方式过滤
root#$ sed -n '/that/{/is/p}' sample.txt |sed -n 's/boy/BOY/g'p
that is a BOY
但是这样替换不能够将sample.txt文件中对应部分替换,如果要替换,可以先获取改行行号,然后做替换动作。
root# linenos=`sed -n '/that/{/is/=}' sample.txt`
root# echo $linenos
3 4
root# for lineno in $linenos
> do
> sed -i ''"$lineno"' {s/boy/BOY/g; s/girl/GIRL/g}' sample.txt
> done
root# cat sample.txt
this is a teacher
this is a professor
that is a BOY
that is a GIRL
she is beautiful lady
he lost his way