Linux sed 命令

Linux sed 命令

Linux sed 命令 | 菜鸟教程 (runoob.com)

sed是一种在线编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。

Linux sed 命令是利用脚本来处理文本文件

sed 可依照脚本的指令来处理、编辑文本文件。

sed 主要用来自动编辑一个或多个文件、简化对文件的反复操作、编写转换程序等。

语法:

sed [-hnV][-e<script>][-f<script文件>][文本文件]

参数说明

  • -e<script>或--expression=<script> 以选项中指定的script来处理输入的文本文件。

  • -f<script文件>或--file=<script文件> 以选项中指定的script文件来处理输入的文本文件。

  • -h或--help 显示帮助。

  • -n或--quiet或--silent 仅显示script处理后的结果。

  • -V或--version 显示版本信息。

动作说明

  • a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)

  • d :删除,因为是删除啊,所以 d 后面通常不接任何东西;

  • i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);

  • p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~

  • s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!

chenqiufen@cpuserver-14:~/Desktop$ sed --help
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -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)
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -E, -r, --regexp-extended
                 use extended regular expressions in the script
                 (for portability use 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
      --help     display this help and exit
      --version  output version information and exit

实例

(1)添加行

chenqiufen@cpuserver-14:~/Desktop$ cat test.txt
qwqeewejkjlj
hilhoihoihoipjol
hihjopihoihjoihj
nojophjphpoh
oiljopjpo

# 在test.txt文件的第四行后添加一行,并将结果输出到标准输出
chenqiufen@cpuserver-14:~/Desktop$ sed -e 4a\newLine test.txt
qwqeewejkjlj
hilhoihoihoipjol
hihjopihoihjoihj
nojophjphpoh
newLine
oiljopjpo

(2)删除

# 删除第2行
chenqiufen@cpuserver-14:~/Desktop$ nl test.txt | sed '2d'
     1  qwqeewejkjlj
     3  hihjopihoihjoihj
     4  nojophjphpoh
     5  oiljopjpo

chenqiufen@cpuserver-14:~/Desktop$ cat test.txt
qwqeewejkjlj
hilhoihoihoipjol
hihjopihoihjoihj
nojophjphpoh
hcihoihoi
buguigiuguoi
hihoihpoih

# 删除第2到最后一行
chenqiufen@cpuserver-14:~/Desktop$ nl test.txt | sed '3,$d'
     1  qwqeewejkjlj
     2  hilhoihoihoipjol

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值