LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > LinuxQuestions.org > LinuxQuestions.org Member Intro
User Name
Password
LinuxQuestions.org Member Intro New to LinuxQuestions.org? Been a long time member but never made a post? Introduce yourself here.

Notices


Reply
  Search this Thread
Old 07-09-2017, 05:20 AM   #1
1QAZ2WSX
LQ Newbie
 
Registered: Jul 2017
Posts: 9

Rep: Reputation: Disabled
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
 
Old 07-09-2017, 08:40 AM   #2
linustalman
LQ Guru
 
Registered: Mar 2010
Location: Ireland
Distribution: Debian 12 Bookworm
Posts: 5,715

Rep: Reputation: 479Reputation: 479Reputation: 479Reputation: 479Reputation: 479
Smile

Greetings.

* Note that this section is only for introductions. *
 
Old 07-09-2017, 12:27 PM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by 1QAZ2WSX View Post
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.

Last edited by TB0ne; 07-09-2017 at 12:29 PM.
 
Old 07-10-2017, 07:03 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Hi 1QAZ2WSX and welcome to LQ!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
tar and deleting source files side by side devUnix Linux - Server 4 10-05-2013 02:15 AM
Comparing lines of code side by side punchy71 Programming 7 12-13-2011 04:10 PM
[SOLVED] grep ??? searching a file and outputting 5 lines either side... redhatuser1 Linux - Newbie 2 09-29-2010 03:45 AM
diff file to side-by-side comparison saawan Linux - Software 4 03-01-2010 09:00 PM
"catting" files in one file side by side, not among each other indyn00b Programming 5 05-10-2007 01:05 AM

LinuxQuestions.org > Forums > LinuxQuestions.org > LinuxQuestions.org Member Intro

All times are GMT -5. The time now is 07:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration