Bash/AWK - Transposing, indexing, newbie at if/else.
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 think the issue is either within the else statement or that awk's if/else function doesn't automatically repeat (i.e. go back to the 'if' part of the function).
I figured the code would work like this (pseudocode)
Code:
sequence=1
read each record
if $4 = sequence
append $2+"," to output
timestamp = $3
else
sequence++
append $3+"\n" to output
append $2+"," to output
seq+1; has no effect, seq++; would be the correct awk statement (seq += 1; would also work). Also, the assignment to FS is repeated for every record as well, you should put BEGIN as the pattern before the action to indicate it only happens once. Or you can set it using the -F option, as in: awk -F, -v seq=1 '{...}'
I don't really understand what you are trying to do, but there is an inconsistency between your code and psuedocode:
Each record represents a counter representing a metric (hda, hdb, memory, etc.)
The cookedvalue column represents a value pertaining to the record's metric.
The output should print out all the cookedvalues for all metrics that have the same sequence all on the same line.
The output file is then going to be parsed by another program that a co-worker is using that plots the resulting data by indexing the timestamp.
To do this:
Code:
1. AWK reads a record.
1a. Check to see if the sequence field is 1.
1b. If it is,
print the cookedvalue of that record followed by a comma;
remember the record's timestamp.
1c. (This way, every time the record is 1, it will print cookedvalue after cookedvalue)
1d. If it isn't,
then print the timestamp at the end of the current line;
create a new line;
print the current record's cookedvalue;
increase the sequence field checker by 1.
1e. go back to 1a.
It seems that my logic on step 1d is faulty. It loops the "print timestamp + newline, print cookedvalue" section.
I sort of see how you did that; besides the header issue, it looks like you flipped my if/else logic.
Not flipped so much as combined the common parts:
Code:
if $4 = sequence
append $2+"," to output
timestamp = $3
else
sequence++
append timestamp+"\n" to output # $3 was written, but timestamp was meant
append $2+"," to output
===> # can put timestamp assignment in else branch without causing trouble
if $4 = sequence
append $2+"," to output
timestamp = $3
else
sequence++
append timestamp+"\n" to output
append $2+"," to output
timestamp = $3 # harmless
===> # note that blue parts are common:
if $4 = sequence
;
else
sequence++
append timestamp+"\n" to output
append $2+"," to output
timestamp = $3
Quote:
Originally Posted by grail
We all seemed to have missed the sequence number at the beginning (not sure if important??):
Hmm, true. It's not in the DESIRED OUTPUT of OP. I would guess it's actually the "sequence" from the header line that should be removed since:
Quote:
The output file is then going to be parsed by another program that a co-worker is using that plots the resulting data by indexing the timestamp.
True, so my question to the OP is .... Is the header incorrect, ie the sequence should be removed? Currently we have 7 items in the header and only 6 in the data lines or recommended output.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.