LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-22-2010, 11:45 PM   #1
robertmarkbram
Member
 
Registered: May 2003
Location: Melbourne, Australia
Distribution: Cygwin, Windows XP
Posts: 69

Rep: Reputation: 15
awk command line: blank line record sep, new line field sep


Hi All,

I have been trying to understand from the online awk manuals how to phrase this command line, but would greatly appreciate some help please.

I have a file (temp.txt) with these contents (one field per line, blank line separates records):

Code:
1st record, 1st field cat
1st record, 2nd field dog
1st record, 3rd field duck
1st record, 4th field cow

2nd record, 1st field car
2nd record, 2nd field boat
2nd record, 3rd field plane
2nd record, 4th field train

3rd record, 1st field plate
3rd record, 2nd field cup
3rd record, 3rd field fork
3rd record, 4th field knife
I want to find the record with the value "knife" for e.g. and print out the first field. This is what I have come up with so far:

Code:
awk -F=/ "/knife/ {print \$2}" RS="" "temp.txt"
I understand I need to change the field separator to a newline and record separator to a blank line, but I am getting really confused about how.

Also, does awk support the notion of saying "search for the value in the first field" etc?

Thank you for any assistance!

Last edited by robertmarkbram; 01-23-2010 at 12:37 AM. Reason: Use code instead of courier new
 
Old 01-23-2010, 12:18 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
$ awk -vRS= -vFS="\n" '/knife/{print $2}' file
3rd record, 2nd field cup
 
Old 01-23-2010, 12:35 AM   #3
robertmarkbram
Member
 
Registered: May 2003
Location: Melbourne, Australia
Distribution: Cygwin, Windows XP
Posts: 69

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ghostdog74 View Post
Code:
$ awk -vRS= -vFS="\n" '/knife/{print $2}' file
3rd record, 2nd field cup
Nice, thank you so much - the only other thing I would like to achieve is to narrow the search down, either to say "search in field x" or at least to say "must match whole field" (^searchTerm$ .. ?).

Thank you again!
 
Old 01-25-2010, 01:22 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well ghostdog74 probably has a cleverer solution but M2C:

awk -vRS= -vFS="\n" -v output="$1.*field $2$" '$0 ~ output { print $1 }' $3

and to your script you pass field number ($1), item ($2) and filename ($3)
 
Old 02-21-2010, 05:25 AM   #5
robertmarkbram
Member
 
Registered: May 2003
Location: Melbourne, Australia
Distribution: Cygwin, Windows XP
Posts: 69

Original Poster
Rep: Reputation: 15
In a script:
Code:
awk -vRS= -vFS="\n" "{if (\\$5 == \"$1\") print \\$1}" temp.txt
$1 is "knife"..
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace a field for a whole line in the same file, with awk. amwink Programming 12 11-13-2009 06:51 AM
LXer: SEP Sesam 3.6 Does Hyper-V and Command Line LXer Syndicated Linux News 0 10-03-2009 12:02 AM
need to delete a line if a field of that line matches using awf in bash scripting accesskarthi Linux - Newbie 8 06-29-2009 03:15 AM
how to replace line of file with another line using awk in shell script amit_pansuria Programming 3 03-29-2009 09:43 AM
grab the line below a blank line and the line above the next blank line awk or perl? Pantomime Linux - General 7 06-26-2008 08:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration