Multiple awk to print on one line

I have 5 files

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

      

Template 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 anyone help me?

+3


source to share


2 answers


You don't need multiple awk. You can combine them in one 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 every input file
  • NR>FNR

    will print a space for the second file forward
+2


source


try it

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 the command tr

tr '\n' ' '

      

+1


source







All Articles