在写各种script的时候,或者系统命令的时候,总少不了文本处理,天天在各种编程语言之间环绕,有些常用的处理方法还是总结下,以免生疏
LiHui@GodLike ~/work
$ cat lihui.txt
##################################
#number
123
#word
onetwothree
#match
1:one 2:two 3:three
#json
{"1":"one", "2":"two", "3":"three"}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1:将某行或者某些行打印出来,-n配合p
LiHui@GodLike ~/work
$ sed -n '5p' lihui.txt
onetwothree
LiHui@GodLike ~/work
$ sed -n '$p' lihui.txt
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
LiHui@GodLike ~/work
$ sed -n '2, 9p' lihui.txt
#number
123
#word
onetwothree
#match
1:one 2:two 3:three
#json
{"1":"one", "2":"two", "3":"three"}
LiHui@GodLike ~/work
$ sed -n 'p' lihui.txt
##################################
#number
123
#word
onetwothree
#match
1:one 2:two 3:three
#json
{"1":"one", "2":"two", "3":"three"}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2:删除某行或者某些行,带上d
LiHui@GodLike ~/work
$ sed '3d' lihui.txt
##################################
#number
#word
onetwothree
#match
1:one 2:two 3:three
#json
{"1":"one", "2":"two", "3":"three"}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
LiHui@GodLike ~/work
$ sed '2, $d' lihui.txt
##################################
3:全部匹配输出满足匹配内容的行,正则表达式匹配即可
LiHui@GodLike ~/work
$ sed -n '/one/p' lihui.txt
onetwothree
1:one 2:two 3:three
{"1":"one", "2":"two", "3":"three"}
LiHui@GodLike ~/work
$ sed -n '/\#/p' lihui.txt
##################################
#number
#word
#match
#json
4:匹配出关心的内容,然后需要二次处理进行替换,通过s和g
LiHui@GodLike ~/work
$ sed -n '/one/p' lihui.txt | sed 's/1/100/g'
onetwothree
100:one 2:two 3:three
{"100":"one", "2":"two", "3":"three"}
LiHui@GodLike ~/work
$ sed -n '/\#/p' lihui.txt | sed 's/\#/\?/g'
??????????????????????????????????
?number
?word
?match
?json
5:指定的位置,或者指定内容后面添加行,通过a
LiHui@GodLike ~/work
$ sed '1a How are you' lihui.txt
##################################
How are you
#number
123
#word
onetwothree
#match
1:one 2:two 3:three
#json
{"1":"one", "2":"two", "3":"three"}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
LiHui@GodLike ~/work
$ sed '1, 2a How are you' lihui.txt
##################################
How are you
#number
How are you
123
#word
onetwothree
#match
1:one 2:two 3:three
#json
{"1":"one", "2":"two", "3":"three"}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
LiHui@GodLike ~/work
$ sed '1a How\nare\nyou' lihui.txt
##################################
How
are
you
#number
123
#word
onetwothree
#match
1:one 2:two 3:three
#json
{"1":"one", "2":"two", "3":"three"}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
LiHui@GodLike ~/work
$ sed '/json/a How are you' lihui.txt
##################################
#number
123
#word
onetwothree
#match
1:one 2:two 3:three
#json
How are you
{"1":"one", "2":"two", "3":"three"}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6:直接修改文件内容,全剧替换,而不是将处理的信息打印出来,-i
LiHui@GodLike ~/work
$ sed -i 's/\#/%/g' lihui.txt
LiHui@GodLike ~/work
$ cat lihui.txt
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%number
123
%word
onetwothree
%match
1:one 2:two 3:three
%json
{"1":"one", "2":"two", "3":"three"}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
LiHui@GodLike ~/work
$ sed -i '/%/d' lihui.txt
LiHui@GodLike ~/work
$ cat lihui.txt
123
onetwothree
1:one 2:two 3:three
{"1":"one", "2":"two", "3":"three"}
LiHui@GodLike ~/work
$ sed -i '/}/a From lihui' lihui.txt
LiHui@GodLike ~/work
$ cat lihui.txt
123
onetwothree
1:one 2:two 3:three
{"1":"one", "2":"two", "3":"three"}
From lihui
LiHui@GodLike ~/work
$ sed -i '/}/ s/\"//g' lihui.txt
LiHui@GodLike ~/work
$ cat lihui.txt
123
onetwothree
1:one 2:two 3:three
{1:one, 2:two, 3:three}
From lihui
7:最后有一点要注意的,这里的sed命令都是在命令行执行的,要修改的内容都是直接写出来,假如是通过shell脚本来替换,一定要注意单引号和双引号
下面这个例子,可以看到shell脚本并没有达到one=》four的替换
LiHui@GodLike ~/work
$ cat lihui.txt
123
onetwothree
1:one 2:two 3:three
{1:one, 2:two, 3:three}
From lihui
LiHui@GodLike ~/work
$ cat hello.sh
#!/bin/bash
one=one
four=four
myfile=lihui.txt
sed -i 's/$one/$four/g' $myfile
LiHui@GodLike ~/work
$ sh hello.sh
LiHui@GodLike ~/work
$ cat lihui.txt
123
onetwothree
1:one 2:two 3:three
{1:one, 2:two, 3:three}
From lihui
但是将shell脚本里的单引号修改成双引号,就大功告成了,也就是shell里,跟perl是一样的,双引号才能变量内插,单引号还是当作字符串的
LiHui@GodLike ~/work
$ cat hello.sh
#!/bin/bash
one=one
four=four
myfile=lihui.txt
sed -i "s/$one/$four/g" $myfile
LiHui@GodLike ~/work
$ sh hello.sh
LiHui@GodLike ~/work
$ cat lihui.txt
123
fourtwothree
1:four 2:two 3:three
{1:four, 2:two, 3:three}
From lihui
而命令行里,通过下面sed命令显然是可以直接替换的
$ sed -i 's/one/four/g' lihui.txt
