如何在bash脚本中打印一个字符串以及该字符串的所有n行。

huangapple go评论65阅读模式
英文:

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 &#39;
FNR==NR { a[$1]; next }
NF&lt;=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
&#39; 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 &#39;NR == FNR { for (i = 1; i &lt;= NF; ++i) a[$i] = 1; next }
    a[$1] { $1 = $1; print }&#39; RS= FS=&#39;\n&#39; OFS=&#39;\n\t&#39; file{2,1}

huangapple
  • 本文由 发表于 2023年2月10日 02:48:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定