如何在正则表达式之后打印单词但不打印相似的单词?
我想要一个 awk 或 sed 命令来打印正则表达式之后的单词。
我想找到一个单词后面的单词,但不是看起来相似的单词。
该文件如下所示:
somethingsomething
X-Windows-Icon=xournal
somethingsomething
Icon=xournal
somethingsomething
somethingsomething
我想要来自“Icon=xournal”的“xournal”。这就是我到目前为止已经走了多远。我也尝试过 AWK 字符串,但也不成功。
cat "${file}" | grep 'Icon=' | sed 's/.*Icon=//' >> /tmp/text.txt
但我得到了两个,所以文本文件给出了两个我不想要的xournal。
I want an awk or sed command to print the word after regexp.
I want to find the WORD after a WORD but not the WORD that looks similar.
The file looks like this:
somethingsomething
X-Windows-Icon=xournal
somethingsomething
Icon=xournal
somethingsomething
somethingsomething
I want "xournal" from the one that say "Icon=xournal". This is how far i have come until now. I have tried an AWK string too but it was also unsuccessful.
cat "${file}" | grep 'Icon=' | sed 's/.*Icon=//' >> /tmp/text.txt
But i get both so the text file gives two xournal which i don't want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
^
将模式锚定在行的开头。你甚至可以直接在 sed 中进行 grep 操作:你也可以使用 awk,我认为它读起来更好一些。使用
=
作为字段分隔符,如果字段 1 是Icon
则打印字段 2:Use
^
to anchor the pattern at the beginning of the line. And you can even do the grepping directly within sed:You could also use awk, which I think reads a little better. Using
=
as the field separator, if field 1 isIcon
then print field 2:即使 Perl 不是标签之一,这也可能有用。
如果您对 Perl 感兴趣,这个小程序将为您完成任务:
这是输出:
解释:
while (<>)
从作为参数给出的文件中获取输入数据执行时在命令行上。(/Icon\=/i)
是if
条件中使用的正则表达式。$'
将打印正则表达式后面的行部分。This might be useful even though Perl is not one of the tags.
In case if you are interested in Perl this small program will do the task for you:
This is the output:
explanation:
while (<>)
takes the input data from the file given as an argument on the command line while executing.(/Icon\=/i)
is the regex used in theif
condition.$'
will print the part of the line after the regex.您所需要的只是:
All you need is: