【三剑客】之sed使用总结

三剑客 Liemer_Lius 497℃

sed作为Linux三剑客命令之一,使用场景极多,总结常用和比较高级的用法,供后续参考。

sed的选项

> 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)
  -c, --copy
                 use copy instead of rename when shuffling files in -i mode
  -b, --binary
                 does nothing; for compatibility with WIN32/CYGWIN/MSDOS/EMX (
                 open files in binary mode (CR+LFs are not treated specially))
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -r, --regexp-extended
                 use extended regular expressions in the script.
  -s, --separate
                 consider files as separate rather than as a single continuous
                 long stream.
  -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

示例:sed -i 实现文件字符串替换

> sed -i.bak 's/j/a/g' a.txt   # -i可以用替换字符串的形式编辑文件,-i后面紧跟一个字符串(不能加空格),可以在替换前将文件做一个备份,比如位置是.bak,备份文件就是file.bak
> ls -lhrt
total 12K
-rw-------. 1 root root 1.3K Apr 29 07:37 anaconda-ks.cfg
-rw-r--r--  1 root root   25 Apr 30 03:51 a.txt.bak
-rw-r--r--  1 root root   25 Apr 30 03:53 a.txt
> diff a.txt a.txt.bak 
1c1
< dlkdakakadfaadkfaakdafka
---
> dlkdjkjkjdfjadkfjakdjfkj

常用方法

1、匹配某个字符串开头到文件末尾删除

sed "/^# ================================我是禁止/,\$d" data.sh  # 这里注意,""是有变量替换的,因此需要\脱义,单引号就不需要
sed '/^# ================================我是禁止/,$d' data.sh

2、匹配字符串1到字符串2之间的内容删除

sed '/10.3.52.40/,/10.3.52.41/d' a.txt

3、匹配字符串,在后面的第三行新增内容

# cat a.txt
2022-03-15 15:39:44
DC_BAK_DIR=yb-ys
SUB_ENV=test
POD_LOG_DIR=/data/log/sql-pipeline/yb-ys/test/hNstdltTbM8hPut2J1TPNRirCYcFfBVf
RANDOM_DIR=hNstdltTbM8hPut2J1TPNRirCYcFfBVf
NAME_SPACE=c87e2267-1001-4c70-bb2a-ab41f3b81aa3
INSTANCE_ID=test-sql-pipeline-v2-6675df7596-g8jsl
APP_NAME=YWB-sql-pipeline-v2-迭代更新测试-忽略告警

# sed '/c87e2267-1001-4c70-bb2a-ab41f3b81aa3/{n;n;s#$#\nTest add things...#}' a.txt  # n;代表1行
2022-03-15 15:39:44
DC_BAK_DIR=yb-ys
SUB_ENV=test
POD_LOG_DIR=/data/log/sql-pipeline/yb-ys/test/hNstdltTbM8hPut2J1TPNRirCYcFfBVf
RANDOM_DIR=hNstdltTbM8hPut2J1TPNRirCYcFfBVf
NAME_SPACE=c87e2267-1001-4c70-bb2a-ab41f3b81aa3
INSTANCE_ID=test-sql-pipeline-v2-6675df7596-g8jsl
APP_NAME=YWB-sql-pipeline-v2-迭代更新测试-忽略告警
Test add things...

4、去掉每行最后的一个字符

sed 's/.$//'

5、去掉颜色

sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"

 

转载请注明:liutianfeng.com » 【三剑客】之sed使用总结

喜欢 (2)

评论已关闭。