0

I want, the selected lines of file to be print in output file side by side separated by space. Here what I have did so far,

for file in SAC*

do

awk 'FNR==2 {print $4}' $file >>exp 

awk 'FNR==3 {print $4}' $file >>exp

awk 'FNR==4 {print $4}' $file >>exp

awk 'FNR==5 {print $4}' $file >>exp

awk 'FNR==7 {print $4}' $file >>exp

awk 'FNR==8 {print $4}' $file >>exp

awk 'FNR==24 {print $0}' $file >>exp 

done

My output is:

XV

AMPY

BHZ

2012-08-15T08:00:00

2013-12-31T23:59:59

I want output should be

XV AMPY BHZ 2012-08-15T08:00:00  2013-12-31T23:59:59
3
  • 3
    show the input file contents Commented Jul 10, 2017 at 9:01
  • pipe the result to tr -d '\n' Commented Jul 10, 2017 at 9:07
  • Modify all print $4 to printf "%s ",$4 in your awk command Commented Jul 10, 2017 at 9:20

1 Answer 1

1

First the test data (only 9 rows, tho):

$ cat file
1 2 3 14
1 2 3 24
1 2 3 34
1 2 3 44
1 2 3 54
1 2 3 64
1 2 3 74
1 2 3 84
1 2 3 94

Then the awk. No need for that for loop in shell, awk can handle multiple files:

$ awk '
BEGIN { 
    ORS=" "
    a[2];a[3];a[4];a[5];a[7];a[8]  # list of records for which $4 should be outputed
}
FNR in a { print $4 }              # output the $4s
FNR==9 { printf "%s\n",$0 }        # replace 9 with 24
' file file # ...                  # the files you want to process (SAC*)
24 34 44 54 74 84 1 2 3 94
24 34 44 54 74 84 1 2 3 94
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.