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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-18-2012, 03:37 PM
|
#1
|
|
LQ Newbie
Registered: Aug 2005
Location: Victoria, British Columbia - Canada
Distribution: RHEL 5.5
Posts: 16
Rep:
|
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.
Last edited by inversecow; 01-18-2012 at 03:40 PM.
|
|
|
|
01-18-2012, 03:59 PM
|
#2
|
|
Member
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 113
Rep:
|
Does this happen only when you output to screen, or also when you redirect to a file?
|
|
|
|
01-18-2012, 04:01 PM
|
#3
|
|
LQ Newbie
Registered: Aug 2005
Location: Victoria, British Columbia - Canada
Distribution: RHEL 5.5
Posts: 16
Original Poster
Rep:
|
Quote:
Originally Posted by thesnow
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 ( ).
|
|
|
|
01-18-2012, 06:04 PM
|
#4
|
|
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
|
Quote:
Originally Posted by inversecow
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:
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
|
|
|
1 members found this post helpful.
|
01-18-2012, 06:25 PM
|
#5
|
|
LQ Newbie
Registered: Aug 2005
Location: Victoria, British Columbia - Canada
Distribution: RHEL 5.5
Posts: 16
Original Poster
Rep:
|
Quote:
Originally Posted by Nominal Animal
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:51 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|