LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-26-2016, 12:59 PM   #1
bishop2001
Member
 
Registered: Apr 2015
Posts: 58

Rep: Reputation: Disabled
format output bash


Greetings
i have a file which contains the following

Something 01
average max min
123 215 0

Something 02
average max min
1 10 5

etc...

im hoping to change the output to look like:

Something 01
average
123
max
215
min
0

Something 02
average
1
max
10
min
5

help please
Thank again.
 
Old 01-26-2016, 01:03 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
what have you tried and where are you stuck. i would use awk.
 
Old 01-26-2016, 01:38 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
ditto for awk
 
Old 01-26-2016, 02:23 PM   #4
bishop2001
Member
 
Registered: Apr 2015
Posts: 58

Original Poster
Rep: Reputation: Disabled
this works but its pretty lame. more compact way?

awk '{print $1}' file > 1;awk '{print $2}' file >> 1;awk '{print $3}' file >> 1
 
Old 01-27-2016, 08:03 AM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
This solution is written in a way which is intended to be instructive.
It could be done with fewer keystrokes but this is most readable.

With this InFile ...
Code:
 
Something 01
average max min
123 215 0

Something 02
average max min
1 10 5

Something 19
average max min
5 12 2
... this awk ...
Code:
awk '{n=NR%4;
      if (n==1)  print $0;
      if (n==2)  print $0;
      if (n==3)  split($0,a);
      if (n==0) {split($0,b);
        for (j=1;j<=3;j++)
          print a[j]"\n"b[j]}}' $InFile >$OutFile
... produced this OutFile ...
Code:
 
Something 01
average
123
max
215
min
0

Something 02
average
1
max
10
min
5

Something 19
average
5
max
12
min
2
Daniel B. Martin
 
2 members found this post helpful.
Old 01-27-2016, 10:27 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Originally Posted by bishop2001 View Post
this works but its pretty lame. more compact way?

awk '{print $1}' file > 1;awk '{print $2}' file >> 1;awk '{print $3}' file >> 1
How does this deliver what you want??
Here is what I get running your suggestion:
Code:
$ awk '{print $1}' file > 1;awk '{print $2}' file >> 1;awk '{print $3}' file >> 1
$ cat 1
Something
average
123

Something
average
1

Something
average
5
01
max
215

02
max
10

19
max
12

min
0


min
5


min
2
The above to me does not look anything like your originally requested output.

So before I can deliver a solution I need to know which output you are after?

Also, are you really looking to make a file called '1'?
 
  


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
Putty using csv files in BASH to format file in script as output smks Linux - Newbie 7 12-22-2011 05:50 PM
[SOLVED] Bash; awk or sed output to variable: how keep newline at end of each output line porphyry5 Programming 3 06-10-2011 05:50 PM
Best video format for YouTube upload and can Recordmydesktop output that format? linus72 Linux - Software 6 12-21-2009 03:53 PM
Format bash output ceashton Programming 3 08-17-2007 12:16 PM
bash format "cat" output bendeco13 Linux - General 3 10-29-2004 06:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:35 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