简体   繁体   English

使用sed匹配行尾,NOT后面紧跟某个字符

[英]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.

相关问题 匹配“感叹号”字符“不紧跟单词” - Match 'exclamation mark' character 'not immediately preceded by a word' 正则表达式:匹配不紧随字母数字字符之后的字符串 - Regular Expression: Match a string not immediately preceded by or followed by a alphanumeric character 任何方式匹配模式EITHER先于OR后跟某个字符? - Any way to match a pattern EITHER preceded OR followed by a certain character? 匹配字符,但不匹配 - Match character but not when preceded by 正则表达式:不匹配某个字符前面的数字 - regex: don't match number preceded by certain character 匹配单词前面没有字符 - Match word not preceded by character 匹配模式前面没有字符 - Match pattern not preceded by character 在bash中使用sed来匹配特定字符,只要该字符不位于其他任何字符之前或之后 - Using sed in bash to match a specific character as long as it is not preceded or followed by any other character 使用sed或awk,如何从当前行的末尾匹配回指定的字符? - With sed or awk, how do I match from the end of the current line back to a specified character? 模仿否定的向后看以匹配 JavaScript 正则表达式中没有紧跟在特定字符之前的模式 - Mimicking negative lookbehind to match a pattern not immediately preceded with a specific character in JavaScript regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM