The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
need help in Parsing a CSV file and generate a new output file vkr Shell Programming and Scripting 15 08-01-2008 08:33 AM
Finding & Moving Oldest File by Parsing/Sorting Date Info in File Names nikosey Shell Programming and Scripting 6 07-30-2008 10:46 PM
Parsing a file aol12123 Shell Programming and Scripting 11 03-25-2008 02:06 AM
Parsing a csv file chiru_h Shell Programming and Scripting 6 02-12-2008 09:33 AM
Parsing a Log file tbirenzweig Shell Programming and Scripting 4 06-20-2006 08:02 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-13-2008
brawnr brawnr is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 2
File Parsing Help

Hello,

I have a file which contains groups of fields. These groups are separated by a blank line, to form a logical record.

Each line consists of a field-value pair.

If want to find all records where field 'd' has a value of '4' and if it does, I want the value of field 'a' (from the same record).

Here's an example input file:
Quote:
a 1
b 2
c 3
d 4
a 2
b 1
d 6

b 3
a 6
c 2

c 2
d 4
a 9

c 3
d 4
a 3

d 4
a 6
I want to end up with a file containing:
Quote:
1
6
Import things to nore about the input file are a) the number of records varies, b) not all records contain a 'd' field, c) not all records contain a 'a' field, e) fields can be in any order within a record.

I think the answer will be to use AWK but I have very little knowledge of AWK and have only used it for very basic things.

The OS is Sun 5.8 by the way.

Help would be greatly appreciated.

Thanks.

  #2 (permalink)  
Old 10-13-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,365

Based on the input you supplied, the output should be:


Code:
1
9
3
6

This script will give you that:


Code:
awk '
/a/ { a = $2 }
/d/ { d = $2 }
/^$/ && d == 4 && a > 0 {
   print a 
   a = -1
   d = -1
}
END {
   if ( d == 4 && a > 0 ) print a
}
' "$FILE"

  #3 (permalink)  
Old 10-13-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 753
Unclear

Seems to be a big gap between your requirements and the output you expect. Based on what you have stated the output should look like cfajohnson's awk script...correct???
  #4 (permalink)  
Old 10-13-2008
brawnr brawnr is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 2
Sorry, you are correct, the output should have read:


Code:
 
1
9
3
6

Many thanks for your help cfajohnson.
  #5 (permalink)  
Old 10-13-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,915
With Perl:

Code:
perl -00 -ne'print $1,"\n"if/d 4/&&/a (.)/' filename

And another AWK approach:


Code:
awk '/d 4/ && /a / {
  for (i=1; i<=NF; i++)
    if ($i == "a") print $(i+1)
	}' RS= filename


Last edited by radoulov; 10-13-2008 at 04:03 PM.. Reason: refactored
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:13 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0