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.
Have a look at sed and awk. As with all questions for scripts you'll have to put in the work. Show us what you've got and where it's failing or where you encounter problems. That's when you encounter LQ users at their best. Ready made solutions are hardly ever offered here. Basically you loop through the output, line by line, filter out what you want using sed and/or awk and output to screen or an output file. If you're new to Bash I advice you to read some of the following: Bash Guide for Beginners Advanced Bash Scripting Guide Sed Awk
Have a look at sed and awk. As with all questions for scripts you'll have to put in the work. Show us what you've got and where it's failing or where you encounter problems. That's when you encounter LQ users at their best. Ready made solutions are hardly ever offered here. Basically you loop through the output, line by line, filter out what you want using sed and/or awk and output to screen or an output file. If you're new to Bash I advice you to read some of the following: Bash Guide for Beginners Advanced Bash Scripting Guide Sed Awk
Kind regards,
Eric
I do agree with you.
Here is what i have tried.
Quote:
awk 'NR>5' script_output > newfile
This assumes first 5 lines will be ignored.But as per the output its not only restricted to 5 it can be anything.
I need customize it considering "NAME" Keyword.
I guess by picking 2nd instance of "NAME" and taking content from then onwards will be sufficient(doubt:There is still better way of doing it)
Confused here.
You can use sed to delete from the first line up to the first occurrence of NAME (including that line). Since that's only a header I take it that it has no importance to you. Using your example text:
Code:
NAME STATE CPU(sec) CPU(%)
XXX -----r 55567 0.0
XXX -----r 55567 0.0
XXX -----r 55567 0.0
XXX -----r 55567 0.0
NAME STATE CPU(sec) CPU(%)
XXX -----r 55567 4.5
XXX -----r 55567 0.5
XXX -----r 55567 0.4
XXX -----r 55567 8.0
NAME STATE CPU(sec) CPU(%)
XXX -----r 55567 2.0
XXX -----r 55567 0.0
XXX -----r 55567 4.0
XXX -----r 55567 0.0
You can use sed to delete from the first line up to the first occurrence of NAME (including that line). Since that's only a header I take it that it has no importance to you. Using your example text:
Code:
NAME STATE CPU(sec) CPU(%)
XXX -----r 55567 0.0
XXX -----r 55567 0.0
XXX -----r 55567 0.0
XXX -----r 55567 0.0
NAME STATE CPU(sec) CPU(%)
XXX -----r 55567 4.5
XXX -----r 55567 0.5
XXX -----r 55567 0.4
XXX -----r 55567 8.0
NAME STATE CPU(sec) CPU(%)
XXX -----r 55567 2.0
XXX -----r 55567 0.0
XXX -----r 55567 4.0
XXX -----r 55567 0.0
I know sed has some buffers you can use to copy/keep the first line and insert it later on. For a bit of time I've been trying to figure out how exactly to use them in this case but haven't found it yet. Another solution you could use is this one.
Take the first command and redirect to a file:
Code:
sed -e '1,/NAME/d' yourfile >yourfile2
Then insert the line at the beginning of the file:
Code:
sed -i -'1i\
NAME STATE CPU(sec) CPU(%)' yourfile2
That will give you the desired result. I'm sure someone with more sed experience will kick in pretty soon, providing you a oneliner that does it all. If I find one in the meantime I'll post it.
will skip the 1st occurrence & delete through the 2nd occurrence, but since they are the same, this has the effect of reinserting the line.
If the discard block is always the 1st 5 lines, then
Code:
<script_output> | sed '1,5d'
should do the trick.
I tested both on your sample, & both work.
If the "NAME ..." lines are not all the same, i.e. the 1st cannot substitute for the 2nd, and the delete block's length is not fixed, then the far more cumbersome:
Code:
<script_output> | sed '2,/NAME/s,^NAME.*$,&\n&,' | sed '1,/NAME/d'
will do the trick.
If there is any any chance of leading spaces in the "NAME ..." lines use:
Code:
<script_output> | sed '2,/NAME/s,^.*NAME.*$,&\n&,' | sed '1,/NAME/d'
Again, I have tested both of these on your sample.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.