LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH: Reformatting text output to join lines (https://www.linuxquestions.org/questions/programming-9/bash-reformatting-text-output-to-join-lines-924488/)

inversecow 01-18-2012 03:37 PM

BASH: Reformatting text output to join lines
 
Hello all,

I am working on a script to collect I/O metrics and am having trouble reformatting output from the iostat command.
Specifically, some "normally single line" rows of data are being placed as double line results (OUTPUT 1).
I expect this has to do with output formatting rules within the utility itself, but have been unable to "unify" the two lines back into one.

What are some recommended solutions to obtain a desirable result (EG: RESULT 1)?
I would like to use tools suitable to include in a command pipeline if possible (EG: sed, tr, awk, etc).
The spacing is less important as I plan to cut the results up with awk and convert to CSV.

# OUTPUT 1
Code:

[20:42:45:user@hostname:~]
$ /usr/bin/iostat -d -x
...
cciss/c0d0        1.44    22.00  3.19  6.78  273.78  190.25    46.53    0.36  36.42  3.82  3.81
cciss/c0d0p1
0.00    0.00  0.00  0.00    0.03    0.00    18.12    0.00  27.78  26.17  0.01
cciss/c0d0p2
0.55    7.65  1.08  1.54  103.03    69.58    66.01    0.16  61.01  4.80  1.26
cciss/c0d0p3
0.88    14.35  2.11  5.24  170.72  120.67    39.62    0.20  27.68  4.22  3.10
...

# RESULT 1
Code:

[20:42:45:user@hostname:~]
$ /usr/bin/iostat -d -x | $EXTRA_COMMANDS
...
cciss/c0d0        1.44    22.00  3.19  6.78  273.78  190.25    46.53    0.36  36.42  3.82  3.81
cciss/c0d0p1      0.00    0.00  0.00  0.00    0.03    0.00    18.12    0.00  27.78  26.17  0.01
cciss/c0d0p2      0.55    7.65  1.08  1.54  103.03    69.58    66.01    0.16  61.01  4.80  1.26
cciss/c0d0p3      0.88    14.35  2.11  5.24  170.72  120.67    39.62    0.20  27.68  4.22  3.10
...


Thanks for your time.

thesnow 01-18-2012 03:59 PM

Does this happen only when you output to screen, or also when you redirect to a file?

inversecow 01-18-2012 04:01 PM

Quote:

Originally Posted by thesnow (Post 4578272)
Does this happen only when you output to screen, or also when you redirect to a file?

Hello thesnow,

Thanks for the above reply.
Indeed this is a problem which occurs in both scenarios (screen && file).
I am writing to a file with a simple STDOUT redirection (
Code:

command > file.txt
).

Nominal Animal 01-18-2012 06:04 PM

Quote:

Originally Posted by inversecow (Post 4578243)
Specifically, some "normally single line" rows of data are being placed as double line results (OUTPUT 1).

It seems iostat is splitting the line because the device name is too long. The splits occur at the start of the line, just after the device name, and not later on in the line.

Quote:

Originally Posted by inversecow (Post 4578243)
What are some recommended solutions to obtain a desirable result (EG: RESULT 1)?

Pipe through
Code:

awk '{ if (NF>1) printf("%s\n", $0); else printf("%s\t", $0); }'
Quote:

Originally Posted by inversecow (Post 4578243)
The spacing is less important as I plan to cut the results up with awk and convert to CSV.

In that case, you can use a "pre-rule" to merge those lines, so your awk script will see correct records:
Code:

awk '(NF==1) { getline temp; $0 = $0 "\t" temp; }
    { # your actual rule goes here
      printf("\"%s\",%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12);
    }'

Note that before running the iostat and the awk script, you should probably set LANG=C and LC_ALL=C in the environment, to make sure the output does not depend on locale. For example, in my default locale, the decimal point is , . If you use Bash, start it with
Code:

#!/bin/bash
export LANG=C LC_ALL=C

To set it for just the iostat call, use
Code:

env LANG=C LC_ALL=C iostat -d -x

inversecow 01-18-2012 06:25 PM

Quote:

Originally Posted by Nominal Animal (Post 4578345)
It seems iostat is splitting the line because the device name is too long. The splits occur at the start of the line, just after the device name, and not later on in the line.


Pipe through
Code:

awk '{ if (NF>1) printf("%s\n", $0); else printf("%s\t", $0); }'
In that case, you can use a "pre-rule" to merge those lines, so your awk script will see correct records:
Code:

awk '(NF==1) { getline temp; $0 = $0 "\t" temp; }
    { # your actual rule goes here
      printf("\"%s\",%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12);
    }'

Note that before running the iostat and the awk script, you should probably set LANG=C and LC_ALL=C in the environment, to make sure the output does not depend on locale. For example, in my default locale, the decimal point is , . If you use Bash, start it with
Code:

#!/bin/bash
export LANG=C LC_ALL=C

To set it for just the iostat call, use
Code:

env LANG=C LC_ALL=C iostat -d -x

Hello Nominal Animal,

Thanks for the in-depth response.
This fixed my issue beautifully! :-D


All times are GMT -5. The time now is 02:29 AM.