Computing.Net > Forums > Unix > file parsing

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

file parsing

Reply to Message Icon

Name: tvc
Date: November 7, 2009 at 10:35:06 Pacific
OS: unix
CPU/Ram: n/a
Product: Sony Crash bash (psx)
Subcategory: Software Problems
Comment:

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 eleven

Now, 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 eleven

And, 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



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: November 7, 2009 at 20:45:49 Pacific
Reply:

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


0

Response Number 2
Name: ghostdog
Date: November 8, 2009 at 05:51:36 Pacific
Reply:

v="two"
awk -v var="$v" '$2==var' file

GNU win32 packages | Gawk


0

Response Number 3
Name: tvc
Date: November 8, 2009 at 06:01:57 Pacific
Reply:

Cool, both syntaxes work perfectly!
Thanks!


0

Response Number 4
Name: ghostdog
Date: November 8, 2009 at 18:28:11 Pacific
Reply:

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

GNU win32 packages | Gawk


0

Response Number 5
Name: tvc
Date: November 9, 2009 at 02:41:06 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: nails
Date: November 9, 2009 at 09:51:28 Pacific
Reply:

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.


0

Response Number 7
Name: tvc
Date: November 11, 2009 at 03:55:42 Pacific
Reply:

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


0

Response Number 8
Name: ghostdog
Date: November 12, 2009 at 00:23:10 Pacific
Reply:

like ?

GNU win32 packages | Gawk


0

Response Number 9
Name: tvc
Date: November 12, 2009 at 14:00:51 Pacific
Reply:

The big three : Sun, IBM and HP (in no specific order)


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: file parsing

Simple text file parsing? www.computing.net/answers/unix/simple-text-file-parsing/5920.html

Shell script for Text file parsing www.computing.net/answers/unix/shell-script-for-text-file-parsing/4388.html

Parsing file to store array variabl www.computing.net/answers/unix/parsing-file-to-store-array-variabl/8254.html