[SOLVED] BASH: Reformatting text output to join lines
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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 (
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
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
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:
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
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:
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.