查找文件中出现的所有单词实例,后跟等号和引号

时间:2017-10-02 00:23:34

标签: string bash find

我的文件中有一个文件system =“Some Value”,文件长1行。

如果文件格式正确,我知道我可以这样做:

cat filename | grep 'system="'

我会逐行接收它们。但是由于每个段落的文件都是多段和1行,我不能。

将所有单词系统加上它的值转储到另一个文件中会很好:

system="Some words"
system="Some other words"
system="and more"

2 个答案:

答案 0 :(得分:0)

由于你没有向我们展示你的Input_file,所以我认为以下是你的Input_file。

cat Input_file
system="Some words" system="Some other words" system="and more"

现在,以下代码将帮助我们将输出作为您显示的输出。

awk '{gsub(/ system/,"\nsystem");print}'  Input_file

输出如下。

system="Some words"
system="Some other words"
system="and more"

说明:只需使用awk的全局替换实用程序将字符串system的所有匹配项替换为newline system。然后打印线。

编辑或者使用sed解决方案,您只需将所有空间系统替换为新的线路系统,如下所示。

sed 's/ system/\nsystem/g'  Input_file

答案 1 :(得分:0)

本杰明有正确的答案。

grep -o 'system="[^"]*"' filename > final.txt

似乎工作得很好。

感谢帮助!