LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   LinuxQuestions.org Member Intro (https://www.linuxquestions.org/questions/linuxquestions-org-member-intro-24/)
-   -   shell script to print selected lines of files side by side in output file. (https://www.linuxquestions.org/questions/linuxquestions-org-member-intro-24/shell-script-to-print-selected-lines-of-files-side-by-side-in-output-file-4175609503/)

1QAZ2WSX 07-09-2017 05:20 AM

shell script to print selected lines of files side by side in output file.
 
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

linustalman 07-09-2017 08:40 AM

Greetings.

* Note that this section is only for introductions. *

TB0ne 07-09-2017 12:27 PM

Quote:

Originally Posted by 1QAZ2WSX (Post 5732723)
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

Great; so look at your other post:
http://www.linuxquestions.org/questi...pt-4175609000/

...where you were told to use CODE tags. And I'd recommend you do a quick Google search for using sed to join all lines together, or re-write your script to assign each awk to its own variable, and print them all at once, instead of appending everything to one line.

rtmistler 07-10-2017 07:03 AM

Hi 1QAZ2WSX and welcome to LQ! :)


All times are GMT -5. The time now is 06:51 PM.