Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data selection 1

Status
Not open for further replies.

alan147

Technical User
Nov 15, 2002
128
GB
Good evening

I need to extract some data from a file, each entry comprises three rows of data and I need to be able to select fields from each of the three rows. The 4th field in each of the three rows has a different three character code. The fields are comma seperated.

The file looks similar to this:

xxxxxxx,xxxxxx,xxxxx,aaa,x,x,x,x,x,x,x,x,x,x
xxxxxxx,xxxxxx,xxxxx,bbb,,x,x,x,x,x,x,x,x,x,x,x,x
xxxxxxx,xxxxxx,xxxxx,ccc,,,,x,,x,,,,x,,x,,,x,
xxxxxxx,xxxxxx,xxxxx,aaa,x,x,x,x,x,x,x,x,x,x
xxxxxxx,xxxxxx,xxxxx,bbb,,x,x,x,x,x,x,x,x,x,x,x,x
xxxxxxx,xxxxxx,xxxxx,ccc,,,,x,,x,,,,x,,x,,,x,
xxxxxxx,xxxxxx,xxxxx,aaa,x,x,x,x,x,x,x,x,x,x
xxxxxxx,xxxxxx,xxxxx,bbb,,x,x,x,x,x,x,x,x,x,x,x,x
xxxxxxx,xxxxxx,xxxxx,ccc,,,,x,,x,,,,x,,x,,,x,

If I wanted to select, for example, field 10 and 12 from row 1 those with aaa, and field 16 from row 2 (bbb) and field 8 and 11 from row three (ccc) is there a way with a script I can do this?

Thanks

Alan

 
A starting point:
Code:
awk -F, '
$4=="aaa"{print $10,$12}
$4=="bbb"{print $16}
$4=="ccc"{print $8,$11}
' /path/to/input > output

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks, I'll try that in the morning.

Alan
 
Thanks very much, it does what I want. If a field is blank how can I ignore it?

Alan
 
In the awk man page have a look at the if (expression) statement.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top