英文:
How to print a string and all the n lines of that string bash script
问题
大家好,你能帮我一下吗?我正在尝试获得以下输出,基本上我有两个输入文件,我们只需要两个输入文件中共同的:names,以及它们下面的.name/of/file行。
到目前为止,我已经尝试过:
awk '/:name1/{flag=1} flag;/ /{flag=0}' /path/of/input/file1
输出:
:name1
./name/of/file [logfile] [ error in file coming since Day : 1 ]
但它只打印第一行。
Input file1:
:name1
./name/of/file [logfile] [ error in file coming since Day : 1 ]
./name/of/file [logfile] [ error in file coming since Day : 1 ]
:name2
./name/of/file [logfile] [ error in file coming since Day : 1 ]
:name3
./name/of/file [logfile] [ error in file coming since Day : 24 ]
./name/of/file [logfile] [ error in file coming since Day : 24 ]
:name4
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
:name5
./name/of/file [logfile] [ error in file coming since Day : 6 ]
./name/of/file [logfile] [ error in file coming since Day : 6 ]
Input file2:
:name1
:name3
:name4
Required Outputfile:
:name1
./name/of/file [logfile] [ error in file coming since Day : 1 ]
./name/of/file [logfile] [ error in file coming since Day : 1 ]
:name3
./name/of/file [logfile] [ error in file coming since Day : 24 ]
./name/of/file [logfile] [ error in file coming since Day : 24 ]
:name4
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
英文:
Hi All can you please help me I am trying to get the following output so basically I have 2 input files and we need only the common :names from both the input files along with there the lines below them the .name/of/file lines.
Till now I have tried:
awk '/:name1/{flag=1} flag;/ /{flag=0}' /path/of/input/file1
Output:
:name1
./name/of/file [logfile] [ error in file coming since Day : 1 ]
But its only printing the first line.
Input file1:
:name1
./name/of/file [logfile] [ error in file coming since Day : 1 ]
./name/of/file [logfile] [ error in file coming since Day : 1 ]
:name2
./name/of/file [logfile] [ error in file coming since Day : 1 ]
:name3
./name/of/file [logfile] [ error in file coming since Day : 24 ]
./name/of/file [logfile] [ error in file coming since Day : 24 ]
:name4
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
:name5
./name/of/file [logfile] [ error in file coming since Day : 6 ]
./name/of/file [logfile] [ error in file coming since Day : 6 ]
Input file2:
:name1
:name3
:name4
Required Outputfile:
:name1
./name/of/file [logfile] [ error in file coming since Day : 1 ]
./name/of/file [logfile] [ error in file coming since Day : 1 ]
:name3
./name/of/file [logfile] [ error in file coming since Day : 24 ]
./name/of/file [logfile] [ error in file coming since Day : 24 ]
:name4
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
答案1
得分: 2
一个 awk
方法:
FNR==NR { a[$1]; next }
NF<=1 { flag=0 }
$1 in a { flag=1 }
flag
英文:
One awk
approach:
awk '
FNR==NR { a[$1]; next }
NF<=1 { flag=0 } # clear flag if zero or one non-white-space fields is present in the current line
$1 in a { flag=1 } # set flag if 1st field is an index in the a[] array
flag # if flag == 1 then print current line to stdout
' file2 file1
This generates:
:name1
./name/of/file [logfile] [ error in file coming since Day : 1 ]
./name/of/file [logfile] [ error in file coming since Day : 1 ]
:name3
./name/of/file [logfile] [ error in file coming since Day : 24 ]
./name/of/file [logfile] [ error in file coming since Day : 24 ]
:name4
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
答案2
得分: 2
输出也可以缩进:
a[$1] { $1 = $1; print }' RS= FS='\n' OFS='\n\t' file{2,1}
英文:
Output can also be made indented:
awk 'NR == FNR { for (i = 1; i <= NF; ++i) a[$i] = 1; next }
a[$1] { $1 = $1; print }' RS= FS='\n' OFS='\n\t' file{2,1}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论