技.艺.道:查漏补缺之-sed

一、sed

1.简介

sed是一个用来编辑文本的指令。

2.示例

基本语法:

sed [options] '[匹配模式] sed的内部命令' file1

[options]:默认是脚本命令模式“-e”,即通过后面单引号中的脚本命令来操作指定的文本文件,也可以通过"-f" 改成脚本文件模式,即用指定sed脚本文件操作指定文本文件。还有其他模式,需要的时候可以查看"sed -h",我翻译了一版在附录中了。

匹配模式:

行数:指定操作的行号,直接用数字即可,如sed ‘10 a\abcd’ log.txt。表示操作的对象是文件的第10行。

行区间:固定起止的行区间:'3,8'表示第3行到第8行。

不固定起止的行区间:'5,$'表示从第5行到结尾。

 

 

第一类:增

‘{行号} a\{内容}’:每行后面追加一行{内容};

‘{行号} i\{内容}’:每行前面插入一行{内容}。

(行号不写,则表示此次操作的对象是所有行)

例:

# 输入文件原样输出,用来和后面的处理结果进行对照
root@ubuntu:/leonRain/test# cat log.txt 
1 aaa ccc awqr
2 ssss ddd eee lll
3 00 98u kjlh llx 223 hh 
4 233 ssSSs

# 在每行内容的下一行创建空行并插入指定内容,a,理解为追加吧,自然在后面
root@ubuntu:/leonRain/test# sed 'a\heheda' log.txt
1 aaa ccc awqr
heheda
2 ssss ddd eee lll
heheda
3 00 98u kjlh llx 223 hh 
heheda
4 233 ssSSs
heheda

# 在每行内容的上一行创建空行并插入指定内容,i,理解为插入吧,自然不在最后
root@ubuntu:/leonRain/test# sed 'i\heheda' log.txt
heheda
1 aaa ccc awqr
heheda
2 ssss ddd eee lll
heheda
3 00 98u kjlh llx 223 hh 
heheda
4 233 ssSSs

第二类:删

‘{行号} d’

(行号不写,则表示此次操作的对象是所有行)

# 原始输入文件以作对照
root@ubuntu:/leonRain/test# cat log.txt 
1 aaa ccc awqr
2 ssss ddd eee lll
3 00 98u kjlh llx 223 hh 
4 233 ssSSs

# 对第4行数据执行删除(d)操作
root@ubuntu:/leonRain/test# sed '4 d' log.txt 
1 aaa ccc awqr
2 ssss ddd eee lll
3 00 98u kjlh llx 223 hh 

# 对第2行数据执行删除(d)操作
root@ubuntu:/leonRain/test# sed '2 d' log.txt 
1 aaa ccc awqr
3 00 98u kjlh llx 223 hh 
4 233 ssSSs

 

第三类:改

替换操作用s:

例:'s/aaa/lalala/'   将aaa替换为lalala。

root@ubuntu:/leonRain/test# cat log.txt
1 aaa ccc awqr
2 ssss ddd eee lll
3 00 98u kjlh llx 223 hh 
4 233 ssSSs

root@ubuntu:/leonRain/test# sed 's/aaa/lalala/' log.txt
1 lalala ccc awqr
2 ssss ddd eee lll
3 00 98u kjlh llx 223 hh 
4 233 ssSSs

修改用c:将第二行修改成"aaaccc"

root@ubuntu:/leonRain/test# cat log.txt
1 aaa ccc awqr
2 ssss ddd eee lll
3 00 98u kjlh llx 223 hh 
4 233 ssSSs

root@ubuntu:/leonRain/test# sed '2c aaaccc' log.txt
1 aaa ccc awqr
aaaccc
3 00 98u kjlh llx 223 hh 
4 233 ssSSs

第四类:查

全文查看

类比SQL:

select * from tableA;

命令:

root@ubuntu:/leonRain/test# sed '' log.txt
1 aaa ccc awqr
2 ssss ddd eee lll
3 00 98u kjlh llx 223 hh 
4 233 ssSSs

查看指定行

p:打印出找到的行(原始的数据也会打印一份,若想不打印原始数据,可以使用-n)

'1p':查看第一行数据。

root@ubuntu:/leonRain/test# cat log.txt
1 aaa ccc awqr
2 ssss ddd eee lll
3 00 98u kjlh llx 223 hh 
4 233 ssSSs

root@ubuntu:/leonRain/test# sed '1p' log.txt
1 aaa ccc awqr
1 aaa ccc awqr
2 ssss ddd eee lll
3 00 98u kjlh llx 223 hh 
4 233 ssSSs

root@ubuntu:/leonRain/test# sed -n '1p' log.txt
1 aaa ccc awqr

‘/aaa/ p’:查看含有字符串“aaa”的行。(两个正斜杠之间的内容可以是正则表达式)

root@ubuntu:/leonRain/test# cat log.txt 
1 aaa ccc awqr
2 ssss ddd eee lll
3 00 98u kjlh llx 223 hh 
4 233 ssSSs

root@ubuntu:/leonRain/test# sed -n '/aaa/ p' log.txt 
1 aaa ccc awqr

附录:sed -h 内容及翻译

root@ubuntu:/leonRain/test# sed -h
sed: invalid option -- 'h'
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
用法:sed [选项]..{只有其他脚本才有脚本} [输入文件]...
  -n, --quiet, --silent
                 suppress automatic printing of pattern space
				 禁止自动打印图案空间【言下之意:只打印模式匹配到的行】
  -e script, --expression=script
                 add the script to the commands to be executed
				 将脚本添加到要执行的命令
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
				 将脚本文件的内容添加到要执行的命令中
  --follow-symlinks
                 follow symlinks when processing in place
				 处理到位时遵循符号链接
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if SUFFIX supplied)
				 在适当位置编辑文件(如果提供SUFFIX,则进行备份)
                 【言下之意是:直接修改文件内容】
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
				 为“l”命令指定所需的换行长度
  --posix
                 disable all GNU extensions.
				 禁用所有GNU扩展。
  -E, -r, --regexp-extended
                 use extended regular expressions in the script
				 在脚本中使用扩展的正则表达式
                 (for portability use POSIX -E).
				 便携式使用POSIX -E
  -s, --separate
                 consider files as separate rather than as a single,
                 continuous long stream.
				 将文件视为独立文件,而不是单个连续的长文件流。
      --sandbox
                 operate in sandbox mode.
				 在沙盒模式下运行。
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
				 从输入文件中加载最少量的数据,并更频繁地刷新输出缓冲区
  -z, --null-data
                 separate lines by NUL characters
				 用NUL字符分隔行
      --help     display this help and exit
	             显示此帮助并退出
      --version  output version information and exit
                 输出版本信息并退出

If no -e, --expression, -f, or --file option is given, then the first 
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.
如果未提供-e,-expression,-f或--file选项,
则将第一个非选项参数用作要解释的sed脚本。 
其余所有参数均为输入文件的名称; 
如果未指定输入文件,那么将读取标准输入。
【言下之意:-e是默认选项】

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章