[英]Use sed to match end of line NOT preceded immediately by a certain character
I have a set of lines followed by '\\' I want to print up to the line not containing the '\\' character at the end of the line. 我有一组行后跟'\\',我想打印到该行末尾不包含'\\'字符的行。 Note that xx# yy# change per file and to be re-usable, I want my script to print lines from match1 to '\\' end of line.
请注意,每个文件的xx#yy#都会更改,并且可以重新使用,我希望我的脚本将行从match1打印到行的'\\'。
match1 xx1 xx1 xx1 xx1 \
yy1 yy1 yy1 yy1 \
zz\<1\> zz1 zz1 zz1
xx2 xx2 xx2 xx2 \
...
output should be: 输出应为:
match1 xx1 xx1 xx1 xx1 \
yy1 yy1 yy1 yy1 \
zz\<1\> zz1 zz1 zz1
My best effort came up with: 我最大的努力是:
sed -n '/match1/,/\\/p' file
but does not work. 但不起作用。 Any help or suggestions are appreciated.
任何帮助或建议,表示赞赏。
The following command will do what you want: 以下命令将执行您想要的操作:
sed -n '/match1/,/[^\\]$/p' file
Using the ^
at the start of a character class negates the match. 在字符类开头使用
^
否定匹配。
Following to glenn jackman's great comment the above solution has a little problem. 继格伦·杰克曼(glenn jackman)的好评之后 ,上述解决方案存在一些问题。 Since
[^\\\\]
requires at least one character it would not work if the first line which not ends with a \\ is an empty line. 由于
[^\\\\]
至少需要一个字符,因此如果未以\\结尾的第一行为空行,则该行将不起作用。 Let me add his solution which should be the preferred one: 让我添加他的解决方案,它应该是首选的解决方案:
sed -n '/match1/,/\(^\|[^\\]\)$/p' file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.