LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 03-24-2011, 12:10 AM   #1
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Rep: Reputation: 37
little help for scripting.


We are running some script to generate below values however we don't need the first level output as it always shows cpu % as 0.0.

(We dont need following part:
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
)

script output:

Quote:
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
How would we accomplish this.

Last edited by pinga123; 03-24-2011 at 01:49 AM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-24-2011, 01:30 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello,

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
 
1 members found this post helpful.
Old 03-24-2011, 01:36 AM   #3
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
Quote:
Originally Posted by EricTRA View Post
Hello,

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.
 
Old 03-24-2011, 01:54 AM   #4
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello,

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
the following command:
Code:
sed -e '1,/NAME/d'
gives me this as output:
Code:
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
If you need the header you can easily reinsert it using sed.

Kind regards,

Eric
 
1 members found this post helpful.
Old 03-24-2011, 02:26 AM   #5
pinga123
Member
 
Registered: Sep 2009
Posts: 684

Original Poster
Blog Entries: 2

Rep: Reputation: 37
Quote:
Originally Posted by EricTRA View Post
Hello,

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
the following command:
Code:
sed -e '1,/NAME/d'
gives me this as output:
Code:
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
If you need the header you can easily reinsert it using sed.

Kind regards,

Eric
Work well but how do i reinsert it?
 
Old 03-24-2011, 03:00 AM   #6
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello,

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.

Kind regards,

Eric
 
2 members found this post helpful.
Old 03-24-2011, 07:11 AM   #7
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
If the "NAME ..." lines are always the same, then
Code:
<script_output>  | sed '2,/NAME/d'
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.
 
1 members found this post helpful.
Old 03-24-2011, 09:05 AM   #8
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hi,

Thanks Rick for enlightening me. Never new it would be that simple as to start from line 2.

Kind regards,

Eric
 
Old 03-24-2011, 11:48 AM   #9
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 03-24-2011, 09:05 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Code:
awk '/NAME/{a++}a>1' file
 
Old 03-24-2011, 09:37 PM   #11
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Code:
sed '1,/NAME/ {1d;/NAME/ ! d}' file
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Scripting the Linux desktop, Part 2: Scripting Nautilus LXer Syndicated Linux News 0 02-17-2011 04:02 AM
Firefox Scripting Add-on (Scripting HTML / Javascript inside Firefox) linuxbeatswindows Programming 1 09-18-2009 10:09 PM
scripting phsythax Linux - General 2 06-04-2008 07:22 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
scripting help please deepsix Programming 10 09-08-2005 07:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration