Sed 静默替换文件内容 以及 awk 的简单使用

1. Sed的help 

鸟哥说的 学东西 先看 help 先看man 再google 不好FQ再百度..

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

2. 感觉用的最多的就是 sed -i 静默本地替换文件内容.

简单的练习

vim zhaobsh.txt

zhaobsh is cainiao
zhaobsh is cainiaocainiao

保存之后进行替换

1. 每行增加一个 Yes 

sed -i 's/^/Yes /' zhaobsh.txt

2. 没个末尾增加一个 Enough

sed -i 's/$/ Enough/' zhaobsh.txt

效果

3. 将 cainiao 替换才caibi

sed -i 's/cainiao/caibi/' zhaobsh.txt

4. 替换所有的菜鸟的命令

sed -i 's/cainiao/caibi/g' zhaobsh.txt

5. 删除 包含某字符的一整行

sed -i '/Enough/d' zhaobsh.txt

备注 这里会删掉 Enough 所在的整行信息.

 简单的学习一下  后续工作中 会越来越多的用到.

其他的命令暂时还没学. 用到了 再继续整理.

备注 一些正则表达式的定义相关

^ 表示一行的开头。如:/^#/ 以#开头的匹配。
$ 表示一行的结尾。如:/}$/ 以}结尾的匹配。
\< 表示词首。 如 \<abc 表示以 abc 为首的詞。
\> 表示词尾。 如 abc\> 表示以 abc 結尾的詞。
. 表示任何单个字符。
* 表示某个字符出现了0次或多次。
[ ] 字符集合。 如:[abc]表示匹配a或b或c,还有[a-zA-Z]表示匹配所有的26个字符。如果其中有^表示反,如[^a]表示非a的字符

awk的简单使用 显示 文件的一部分内容

之前删除所有 error的机器 的方法就是使用 awk 来的 

1. 创建测试文件

netstat  -ano >awk.txt

显示所有 第八项目为"/run/systemd/journal/stdout" 的内容

awk '$8=="/run/systemd/journal/stdout" {print $0}' awk.txt

猜你喜欢

转载自www.cnblogs.com/jinanxiaolaohu/p/9448426.html
今日推荐