Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: tvc
Need to parse a file in this way:
The file is a multi-line flatfile, containing whitespace separated keywords, ex.one two three four
five six seven eight
nine ten two elevenNow, I want to know all lines containing a given keyword, ex. "two".
I can do a simple GREP on "two", but this will give:one two three four
nine ten two elevenAnd, I'm not needing to have that feedback, since I would like to have only the lines back from the query, where the second column, matches.
So, I need only to have :
one two three four
How do I do that ?
I am supposing there is only ONE line matching a given keyword in a given column. So, there are no lines like:one two three four
nine two two eleven

One way is to use awk to check if field 2 matches string "two":
#!/bin/ksh var="two" awk '{ if(match($2,v1) > 0) print $0 }' v1=${var} datafile.txt

both syntaxes work, BUT there is difference. match() match words that contain the text. eg if you want to only get the word "two" and 2nd field is , say for example "twoeyes", it will match as well. so take note

I was thinking about that, but didn't do tests that way yet. Often, it would need to exactly match, not just contain that word. Good you stress it, because the "match" function is new to me.
Thanks

tvc:
awk has a number of built-in functions, match being just one:
http://www.math.utah.edu/docs/info/...
The above link speaks of POSIX compliant versions of awk - such as GNU gawk. The older Unix versions may not support all of the functions discussed.

The AWk functions are great ... but they compromise compatibilty with other *nix platforms ... no ?

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |