Remove all blank lines, and after each line to add a blank line
sed '/^$/d;G' /etc/fstab
Each line leading "blank character" (spaces, tabs) Delete
sed 's/^[\t ]*//' file
The aaa bbb ccc text are replaced ttt
sed 's@aaa\|bbb\|ccc@ttt@g' file
Will replace yes to no, and replace the case hello characters do not appear only in a row
sed '/hello/ !s/yes/no/g' file
Next line to delete matching:
sed '/UUID/{n;d}' /etc/fstab
Delete matched to the line and the next line:
sed '/UUID/{N;d}' /etc/fstab
Explain n If the UUID is matched, then move to the next line of matching lines, and delete this line
n under input a read row, a new row with processing the next command
Explain N if the UUID is matched, the match line and the next line will be read to sed buffer space, and then delete these two lines
Delete files in each row of the first character:
sed -n 's/^.//p' /etc/fstab
Delete files last character of each line:
sed -n 's/.$//p' /etc/fstab
Delete files second character per line:
sed -nr 's/(.)(.)(.*)/\1\3/p' /etc/fstab
-r表示使用扩展正则表达式
假设有一个文件格式如下
使用 sed 'N;s/\n/\t/' file命令转换成如下格式
原理解释:
N:读取下一行并追加到模式空间中的行后面,当sed读入第一行内容时,由N将下一行的内容追加到模式空间中,此时模式空间
中的内容为"aaa\nbbb",再由编写的匹配规则进行替换,将"\n"替换为了"\t",再执行默认的"p"操作,输出到标准输出
显示奇数行
sed -n '1~2p' file
转载于:https://www.cnblogs.com/linuxboke/p/5607782.html