LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   format output bash (https://www.linuxquestions.org/questions/programming-9/format-output-bash-4175567951/)

bishop2001 01-26-2016 12:59 PM

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.

schneidz 01-26-2016 01:03 PM

what have you tried and where are you stuck. i would use awk.

grail 01-26-2016 01:38 PM

ditto for awk

bishop2001 01-26-2016 02:23 PM

this works but its pretty lame. more compact way?

awk '{print $1}' file > 1;awk '{print $2}' file >> 1;awk '{print $3}' file >> 1

danielbmartin 01-27-2016 08:03 AM

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

grail 01-27-2016 10:27 AM

Quote:

Originally Posted by bishop2001 (Post 5487670)
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'?


All times are GMT -5. The time now is 05:26 PM.