sed基础
$ cat data5.txt
This is a test line.
This is a different line.
设置sed选项和动作为空,直接输出到STDOUT:
$ sed '' data5.txt
This is a test line.
This is a different line.
按行执行,在默认输出的基础上p:
$ sed 'p' data5.txt
This is a test line.
This is a test line.
This is a different line.
This is a different line.
p指明打印替换后的行:
$ sed 's/test/trial/p' data5.txt
This is a trial line. (p指明打印替换后的行)
This is a trial line.
This is a different line.
silent/quiet模式:指默认输出不要输出
$ sed -n 's/test/trial/p' data5.txt 😇
This is a trial line.
$ echo "this is a test" | sed 'p'
this is a test
this is a test
$ echo "this is a test" | sed -n 'p'
this is a test
设置字符边界
use the word-boundary expression (\b) at both ends of the search string. This ensures the partial words are not matched.
$ cat file.txt
123 Foo foo foo
foo /bin/bash Ubuntu foobar 456
$ sed -i 's/\bfoo\b/linux/g' file.txt
$ cat file.txt
123 Foo linux linux
linux /bin/bash Ubuntu foobar 456
模式匹配设置大小写不敏感
To make the pattern match case insensitive, use the I flag. In the example below we are using both the g and I flags:
$ cat file.txt
123 Foo foo foo
foo /bin/bash Ubuntu foobar 456
$ sed -i 's/foo/linux/gI' file.txt
$ cat file.txt
123 linux linux linux
linux /bin/bash Ubuntu linuxbar 456
使用正则表达式搜索
You can also use regular expressions. For example, to search all 3 digit numbers and replace them with the string number you would use:
$ cat file.txt
123 Foo foo foo
foo /bin/bash Ubuntu foobar 456
$ sed -i 's/\b[0-9]\{3\}\b/number/g' file.txt
$ cat file.txt
number Foo foo foo
foo /bin/bash Ubuntu foobar number
使用&代替匹配模式
Another useful feature of sed is that you can use the ampersand character & which corresponds to the matched pattern. The character can be used multiple times.
For example, if you want to add curly braces {} around each 3 digit number, type:
$ cat file.txt
123 Foo foo foo
foo /bin/bash Ubuntu foobar 456
$ sed -i 's/\b[0-9]\{3\}\b/{&}/g' file.txt
$ cat file.txt
{123} Foo foo foo
foo /bin/bash Ubuntu foobar {456}
修改的同时备份
Last but not least, it is always a good idea to make a backup when editing a file with sed. To do that, just provide an extension for the backup file to the -i option. For example, to edit the file.txt and save the original file as file.txt.bak you would use:
$ sed -i.bak 's/foo/linux/g' file.txt
$ ls file*
file.init file.txt file.txt.bak
Recursive Find and Replace
Sometimes you may want to recursively search directories for files containing a string and replace the string in all files. This can be done using commands such as find or grep to recursively find files in the directory and piping the file names to sed.
The following command will recursively search for files in the current working directory and pass the file names to sed.
$ find . -type f -exec sed -i 's/foo/bar/g' {} +
To avoid issues with files containing space in their names, use the -print0 option, which tells find to print the file name, followed by a null character and pipe the output to sed using xargs -0 :
find . -type f -print0 | xargs -0 sed -i 's/foo/bar/g'
评论
发表评论