LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 03-30-2006, 10:00 PM   #1
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Rep: Reputation: 15
Yet another scripting question


Hi again friends


Now I'm trying to grep a file, get the line directly below the matching content, then awk this to print the result of the 3rd word on the second line multiplied by 100, divided by the 4th word on the 1st line...

The textfile looks something like this:

Quote:
Number of File: 93
Successful conversion: 83

I tried this without success:

Quote:
grep -i "number of files" -A 1 example2.txt | awk '{print ((line2 $3) * 100 / (line1 $4))}'
I know I asked a lot of questions already and really tried this one on my own, but I'm really stuck and feel awful not being able to figure it out...

Could someone please show this ignorant scripter-wannabee the path to enlightenment once more?
 
Old 03-30-2006, 11:47 PM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Use a regular expression to select the line you want, and assign the field you want into a variable.
Perform the calculation and print out the result in the END block.

Remember, a sed or awk program is executed once per line, one line at a time, so on the first line in your example, $3="File:" and $4=93
On the second line, $3=83 and $4 is blank.
Each time, your print command is carried out, or would be if there weren't an error on the first line.

- - -

I would recommend downloading the Gawk source, doing a "./configure && make pdf".
This will generate the documentation. It may depend on the version you use, but I got two separate documents: "Gawk" and "Effective Awk Programming" from installing the documentationb from my .src.rpm.
The second may have come from a separate package.

Last edited by jschiwal; 03-30-2006 at 11:58 PM.
 
Old 03-30-2006, 11:49 PM   #3
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Original Poster
Rep: Reputation: 15
Thanks Jschiwal, but can you provide a rough example as well?

Thanks!
 
Old 04-01-2006, 03:28 AM   #4
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You could try using the form:
$m ~ /pattern/ { print $n }

m is the field number that contains the pattern to test, such as /File:/.
n is the field number that contains the number.

So in your example, you would have two similar lines that look for different patterns and print out the field you want in the action part of each line.

An alternative is to assign the field to a variable in the action part of the line. This will allow you to retain the values you extracted. Then you can use these variables in an equation in the END block.

Since your example file doesn't contain a number of records, awk may not be the best program for this.
It works best at processing a stream of records coming standard input.

You could use sed, for example, to extract the information.
NUMFILES=$(sed -n '/Number of Files/s/^.*: \([[:digit:]][[:digit:]]*\)/\1/p' example.txt )
CONVERSIONS=$(sed -n '/Conversions/s/^.*: \([[:digit:]][[:digit:]]*\)/\1/p' example.txt )
RESULT=$(( CONVERSIONS*100/NUMFILES ))

This may work better in a sed script where you can use the Q (quit) command to keep from reading the rest of the file. For very large files, this can save a lot of time.

I checked the info file for gawk, and it is the Effective Awk Programming manual I mentioned in an earlier post.

Last edited by jschiwal; 04-01-2006 at 05:08 AM.
 
Old 04-01-2006, 12:30 PM   #5
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Original Poster
Rep: Reputation: 15
Jschiwal, thanks for going to the trouble of helping me out. Linux users really are a dime a dozen!

Thanks friend.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
scripting question kpachopoulos Programming 1 03-03-2006 01:03 PM
Question about scripting RitzContent Programming 18 11-23-2004 10:34 AM
Scripting Question Darklight451 Linux - Newbie 4 11-12-2004 11:14 AM
scripting question gearoid Programming 3 05-26-2004 02:43 PM
Scripting question aikitortoise Linux - Newbie 3 11-14-2002 05:18 PM


All times are GMT -5. The time now is 12:00 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration