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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AWK: a better way to extract only fields that match??

Status
Not open for further replies.

cptk

Technical User
Mar 18, 2003
305
US
Input Data:
abc 12-25-05 gifts bear horse
xyz new 03-05-64 house 01-01-99 frogs

I want to extract only those fields that have a "-" in them, but I also want them to display on the same line for each record.

example of desired output:

12-25-05
03-05-64 01-01-99

I found this to work:
awk '{for(i=1;i<=NF;++i)if($i !~/-/)$i=""; print}'

...but, because I'm setting non-matching fields to null (i.e. - ""), I'm getting additional spaces in my final output.

How can I either ...
1.)trim the white space using the above AWK cmd, or
2.)do a positive search (i.e. - search for "-", not use the "!") and only print the field(s) that match on 1 line per record. Example:
awk '{for(i=1;i<=NF;++i)if($i ~/-/) print $i}'
This cmd give me the correct fields, but it displays one field per row, which is what I don't want - I want to have all matching fields on same line per record!




 
awk '{for(i=1;i<=NF;++i) if($i ~ /-/) printf("%s%s", $i,(i<NF) ? OFS : "\n")}' file

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
vgersh99 -

It's not working for me ...

Since I'm not totally familiar with ALL the syntax of the printf at this point (especially the part "(i<NF) ? OFS : "\n"" [should it be $i ???]), I guess I should mention how I'm driving data to this cmd:

> cat somefile | grep "somevalue" | awk ...

Could you elaborate on the printf section please??
 
sorry - I should've tested a bit more:

awk '/somevalue/{for(i=1;i<=NF;++i) if($i ~ /-/) printf("%s%s", $i, OFS); print ""}' somefile

NOTE: you can get rid of UUOC and UUOG and do it all in awk.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
...that's working better, but some follow-up questions.

1.) Yes, I'm taking your advice and dropping the use of the cat and grep cmds (what's UUOC & UUOG mean?) and instead using the awk search / / ...

2.) the 2nd %s format field, "OFS" - which I thought it stood for "Output Field Separator" - I don't understand how this works (but it does) ...

3.) Since I switched over from using the cat & grep way, I'm getting some records in my ouput stating "...has too many fields". Is this a limitation of awk and that maybe I should try gawk instead? Postnote: used POSIX version of awk, no more "...too many fields" error.

...thanks for your help and hopefully you can elaborate on my 1st two questions.
 
1. UUOC - Useless Use Of Cat
UUOG - Useless Use of Grep
uuoc1
uuoc2

2. your inputFS can be different from your outputFS - OFS is a variable to change it. I guess I don't understand what you "don't understand" - it seems you described the usage correctly.

3. yes, it's a limitation of older awks - gawk either has a higher limit OR none at all.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I initially was having trouble because I didn't realize that "OFS" was a variable in of its' self.

Just one last Q:

At the end of the cmd, why does print"" work as opposed to just print ?
 
'print' is equevalent to 'print $0'

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
yeah, but print"" does what?

...just trying to understand!!!
 
print "" is equivalent to printf("\n")

equavalent vlad

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top