arjun9916
arjun9916

Reputation: 91

Multiple awk to print in the same line

I have 5 files

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?

Upvotes: 3

Views: 5306

Answers (3)

user12859859
user12859859

Reputation:

pipe through 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

Upvotes: 0

anubhava
anubhava

Reputation: 785196

You don't need multiple awk. You can actually combine them in single 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
  • NR>FNR will print a space for 2nd file onwards

Upvotes: 2

Arnab Nandy
Arnab Nandy

Reputation: 6702

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 '\n' ' '

Upvotes: 1

Related Questions