简体   繁体   English

多个awk在同一行打印

[英]Multiple awk to print in the same line

I have 5 files我有5个文件

a.txt
b.txt
c.txt 
d.txt 
e.txt

Pattern used使用的图案

awk 'NR==21 {print $1}' a.txt; awk 'NR==21 {print $1}' b.txt; awk 'NR==21 {print $1}' c.txt; awk 'NR==21 {print $1}' d.txt; awk 'NR==21 {print $1}' e.txt;

Output输出

a
b
c
d
e

But I need it to be但我需要它

a b c d e

Can someone please help me?有人可以帮帮我吗?

You don't need multiple awk.你不需要多个 awk。 You can actually combine them in single awk:您实际上可以将它们组合在单个 awk 中:

awk FNR==21 {if (NR>FNR) printf OFS; printf $1}' {a,b,c,d,e}.txt
a b c d e
  • FNR==21 will run this block for line #21 in each input file FNR==21将在每个输入文件中为第 21 行运行此块
  • NR>FNR will print a space for 2nd file onwards NR>FNR将为第二个文件打印一个空格

Try this尝试这个

awk 'NR==21 {print $1}' a.txt; awk 'NR==21 {print $1}' b.txt; awk 'NR==21 {print $1}' c.txt; awk 'NR==21 {print $1}' d.txt; awk 'NR==21 {print $1}' e.txt; |tr '\n' ' '

Just add an tr command只需添加一个tr命令

tr '\n' ' '

通过 xargs 管道

awk 'NR==21 {print $1}' a.txt; awk 'NR==21 {print $1}' b.txt; awk 'NR==21 {print $1}' c.txt; awk 'NR==21 {print $1}' d.txt; awk 'NR==21 {print $1}' e.txt | xargs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM