Unix - 在 grep 结果行之间添加字符串
Unix - Add string between grep resulted lines
任何人都知道一种方法(在 RHEL7 和 AIX5 上工作)来制作一个在 /bin/bash 或 /bin/ksh 上运行的脚本,以使用 grep 在文本文件中搜索 1 个或多个字符串(或另一个函数),将结果放入一个变量中,但在该变量中每次出现的末尾都有一个字符串“\\n”?
例如:
TextFile1.txt
[root@server ~]$ cat TextFile1.txt
aby ORA-3120: unable to xx
sxyy unable to aa
sxyy ORA-3120: unable to aa
ytxy unable to bb
y41y unable to dd
yanby unable to ff
ytxy ORA-3120: unable to bb
y41y ORA-3120: unable to dd
y124gby unable to gg
yanby ORA-3120: unable to ff
aby unable to xx
y124gby ORA-3120: unable to gg
Simple Grep
[root@server ~]$ cat TextFile1.txt | grep "ORA-"
aby ORA-3120: unable to xx
sxyy ORA-3120: unable to aa
ytxy ORA-3120: unable to bb
y41y ORA-3120: unable to dd
yanby ORA-3120: unable to ff
y124gby ORA-3120: unable to gg
Put the results in a variable
[root@server ~]$ aa=$(cat TextFile1.txt | grep "ORA-")
[root@server ~]$ echo $aa
aby ORA-3120: unable to xx sxyy ORA-3120: unable to aa ytxy ORA-3120: unable to bb y41y ORA-3120: unable to dd yanby ORA-3120: unable to ff y124gby ORA-3120: unable to gg
想要的结果:
[root@server ~]$ echo $aa
\\n aby ORA-3120: 无法 xx \\n sxy ORA-3120: 无法 aa \\n ytxy ORA-3120: 无法 bb \\n y41y ORA-3120: 无法 dd \\n yanby ORA-3120: 无法 ff \\n y124gby ORA-3120: 无法 gg \\n
提前感谢您的帮助和提示
一个快速的解决方案是使用 awk:
aa=$(cat TextFile1.txt | grep "ORA-"| awk '{print [=10=] "\n"}')
有很多方法可以做到这一点。这里有两个:
管道 grep
的输出到 sed
:
aa=$(echo " \\n"; grep "ORA-" TextFile1.txt | sed 's/\(.*\)/ \\n/')
输出:
$ aa=$(echo " \\n"; grep "ORA-" TextFile1.txt | sed 's/\(.*\)/ \\n/')
$ echo $aa
\n aby ORA-3120: unable to xx \n sxyy ORA-3120: unable to aa \n ytxy ORA-3120: unable to bb \n y41y ORA-3120: unable to dd \n yanby ORA-3120: unable to ff \n y124gby ORA-3120: unable to gg \n
$
管道 grep
的输出到 awk:
aa=$(echo " \\n"; grep "ORA-" TextFile1.txt | awk '{print [=12=] " \\n"}')
输出:
$ aa=$(echo " \\n"; grep "ORA-" TextFile1.txt | awk '{print [=13=] " \\n"}')
$ echo $aa
\n aby ORA-3120: unable to xx \n sxyy ORA-3120: unable to aa \n ytxy ORA-3120: unable to bb \n y41y ORA-3120: unable to dd \n yanby ORA-3120: unable to ff \n y124gby ORA-3120: unable to gg \n
正如您在 echo "$aa"
中看到的(使用双引号避免 bash 吃掉换行符),每一行都已经以换行符结尾。
当您确定不需要双引号时,只需在每行末尾添加:
aa=$(cat TextFile1.txt | grep "ORA-" | sed 's/$/\\n/')
任何人都知道一种方法(在 RHEL7 和 AIX5 上工作)来制作一个在 /bin/bash 或 /bin/ksh 上运行的脚本,以使用 grep 在文本文件中搜索 1 个或多个字符串(或另一个函数),将结果放入一个变量中,但在该变量中每次出现的末尾都有一个字符串“\\n”?
例如:
TextFile1.txt
[root@server ~]$ cat TextFile1.txt
aby ORA-3120: unable to xx
sxyy unable to aa
sxyy ORA-3120: unable to aa
ytxy unable to bb
y41y unable to dd
yanby unable to ff
ytxy ORA-3120: unable to bb
y41y ORA-3120: unable to dd
y124gby unable to gg
yanby ORA-3120: unable to ff
aby unable to xx
y124gby ORA-3120: unable to gg
Simple Grep
[root@server ~]$ cat TextFile1.txt | grep "ORA-"
aby ORA-3120: unable to xx
sxyy ORA-3120: unable to aa
ytxy ORA-3120: unable to bb
y41y ORA-3120: unable to dd
yanby ORA-3120: unable to ff
y124gby ORA-3120: unable to gg
Put the results in a variable
[root@server ~]$ aa=$(cat TextFile1.txt | grep "ORA-")
[root@server ~]$ echo $aa
aby ORA-3120: unable to xx sxyy ORA-3120: unable to aa ytxy ORA-3120: unable to bb y41y ORA-3120: unable to dd yanby ORA-3120: unable to ff y124gby ORA-3120: unable to gg
想要的结果:
[root@server ~]$ echo $aa
\\n aby ORA-3120: 无法 xx \\n sxy ORA-3120: 无法 aa \\n ytxy ORA-3120: 无法 bb \\n y41y ORA-3120: 无法 dd \\n yanby ORA-3120: 无法 ff \\n y124gby ORA-3120: 无法 gg \\n
提前感谢您的帮助和提示
一个快速的解决方案是使用 awk:
aa=$(cat TextFile1.txt | grep "ORA-"| awk '{print [=10=] "\n"}')
有很多方法可以做到这一点。这里有两个:
管道 grep
的输出到 sed
:
aa=$(echo " \\n"; grep "ORA-" TextFile1.txt | sed 's/\(.*\)/ \\n/')
输出:
$ aa=$(echo " \\n"; grep "ORA-" TextFile1.txt | sed 's/\(.*\)/ \\n/')
$ echo $aa
\n aby ORA-3120: unable to xx \n sxyy ORA-3120: unable to aa \n ytxy ORA-3120: unable to bb \n y41y ORA-3120: unable to dd \n yanby ORA-3120: unable to ff \n y124gby ORA-3120: unable to gg \n
$
管道 grep
的输出到 awk:
aa=$(echo " \\n"; grep "ORA-" TextFile1.txt | awk '{print [=12=] " \\n"}')
输出:
$ aa=$(echo " \\n"; grep "ORA-" TextFile1.txt | awk '{print [=13=] " \\n"}')
$ echo $aa
\n aby ORA-3120: unable to xx \n sxyy ORA-3120: unable to aa \n ytxy ORA-3120: unable to bb \n y41y ORA-3120: unable to dd \n yanby ORA-3120: unable to ff \n y124gby ORA-3120: unable to gg \n
正如您在 echo "$aa"
中看到的(使用双引号避免 bash 吃掉换行符),每一行都已经以换行符结尾。
当您确定不需要双引号时,只需在每行末尾添加:
aa=$(cat TextFile1.txt | grep "ORA-" | sed 's/$/\\n/')