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.
t da hr stat k iop q x y p
200.0000 990720 0 34 2 0 0.000 28.000 -65.500 973.100
200.2500 990720 600 44 1 0 0.982 30.500 -66.000 977.200
.....
.....
The number after "nit" in each "track" line defines how many lines of numbers are in each track. So Track 2 above is cut off and actually has 4 more lines since nit = 6 for that one. There are 1000+ tracks in each file.
What I need to do is delete any track for which nit <= 4.
I was thinking that the easiest way to do this would be to write a loop that reads nit for each line starting with track and then either skip or delete nit+3 lines (to account for the blank lines in the files separating each track and the original track line itself) depending on the value of nit.
I'm a complete novice at awk. My main programming experience is with C++/C# but unfortunately I am confined to a linux box at the moment.
Is this a good way to go about this task? Should I be looking into perl or python? I have no idea if they would be better suited to this task. I also dont have any clue on how I would go about deleting an entire line so even if someone could clear that up for me it would be much appreciated.
/Track/{ split($0,array)
# Read the value of nit
for (i=1; i<=NF; i++){
if (array[i] == "nit" )
nit = array[i+2]
}
# Print the track if nit > 4
if ( nit > 4 ){
i = 1
while ( i <= nit+4 ){
print
getline
i++
}
print
}
}
I assume there are two blank lines between each track, otherwise you have to modify the while statement.
Thanks, Ghostdog! I didn't know you can use \1 in gensub. Also the RS="Track" is cool!
yes, you can. Its already documented. pls check the link i gave the OP for the GNU manual. Note, gensub does not address greediness and its a GNU extension.
yes, you can. Its already documented. pls check the link i gave the OP for the GNU manual. Note, gensub does not address greediness and its a GNU extension.
I always have the GNU awk manual at hand for reference, but I did not find that. Can you point me to the paragraph?
Edit: Found it. Just in the gensub paragraph:
Quote:
gensub provides an additional feature that is not available in sub or gsub: the ability to specify components of a regexp in the replacement text. This is done by using parentheses in the regexp to mark the components and then specifying ‘\N’ in the replacement text, where N is a digit from 1 to 9.
So... this feature is available for gensub only, not for sub or gsub. Good to know!
Last edited by colucix; 06-08-2009 at 01:26 PM.
Reason: I got answer by myself....
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.