LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 04-09-2004, 10:05 AM   #1
Helene
LQ Newbie
 
Registered: Apr 2004
Posts: 19

Rep: Reputation: 0
Newbie playing with Awk and Grep


Hi,
I have a semicolon-separated text file, where I only want to list out the records with a particular field name. This is what I have so far:

grep "$field" $CATALOGUE | awk -F';' ' { print $1 } '

Further in the code, I print out the number of hits. This seems to work, but I'd like to have:
grep "$@" $CATALOGUE | awk -F';' ' { print $0 } '
so that I can print out the whole record(s). But then the counter wont work properly because it is counting all the instances of the particular field on the entire record.

What can I do different? Can anyone please help me with this? I'm so used to sql, and I wish I could say something like grep"$@" $CATALOGUE where "$@ IN $1 ...

- Helene
 
Old 04-11-2004, 01:50 AM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

I'm not completely sure I understand your question, but:

1. I believe you're saying you have a delimited file like this:

less MYFILE.TXT =>
car; porsche; expensive
car; chevy; cheap
pet; horse; expensive
pet; goldfish; cheap

2. Let's say you wanted to print out all the "expensive" items. You could do this:

gawk -F';' '/expensive/ { print $0 }' MYFILE.TXT =>
car; porsche; expensive
pet; horse; expensive

3. Let's say you wanted to count the "expensive" items:

gawk -F';' '/expensive/ { print $0 }' MYFILE.TXT | wc -l =>
2

4. The key point I'm trying to make is that you can *select* those lines you're
interested in with the "/regex/" section, then *process* the information in that line
(and, unfortunately, *only* that line) with the "{command}" section.

Hope that helps .. at least a little bit..

Your .. PSM
 
Old 04-14-2004, 08:38 PM   #3
Helene
LQ Newbie
 
Registered: Apr 2004
Posts: 19

Original Poster
Rep: Reputation: 0
Thank you for the answer. I guess I explained my problem a bit complicated, so let me try again! Assume I use the text file you showed:

car; porsche; expensive
car; chevy; cheap
pet; horse; expensive
pet; goldfish; cheap
expensive; cat; strange

In your example this:
gawk -F';' '/expensive/ { print $0 }' MYFILE.TXT | wc -l
will result in 3

But what if I only want to count the instances of "expensive" in the third field so that the result will be 2?

- Helene
 
Old 04-14-2004, 10:00 PM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
You can use a more complex regular expression (for example, "^expensive" will find words at the beginning of the line, i.e. in the first column) ...

... or you can try implementing this project in something other than "awk" (Perl or C would be good, for example).
 
Old 04-14-2004, 11:48 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Code:
[tink@diggn:~]$ cat awk_file.txt 
car; porsche; expensive
car; chevy; cheap
pet; horse; expensive
pet; goldfish; cheap
expensive; cat; strange
[tink@diggn:~]$ awk -F";" ' $3 ~ "expensive"  { count += 1}; END{ print count} ' awk_file.txt 
2
[tink@diggn:~]$

Cheers,
Tink

Last edited by Tinkster; 09-08-2005 at 02:26 AM.
 
Old 04-15-2004, 12:55 AM   #6
Helene
LQ Newbie
 
Registered: Apr 2004
Posts: 19

Original Poster
Rep: Reputation: 0
This was excellent. Thank you both
 
Old 04-15-2004, 02:08 AM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Pleasure ;)

awk rules! :)


Cheers,
Tink
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Extracting text with grep or awk? UrbanDruid Linux - Newbie 5 04-07-2005 02:57 PM
Bash script question (grep and awk) hamish Linux - Software 6 04-06-2005 03:14 PM
Can I use grep inside awk? Helene Programming 6 04-27-2004 12:58 AM
newbie needs help for grep and awk parker Programming 1 08-12-2003 04:24 AM
Running a shell command containing awk and grep within a C program Linh Programming 1 06-05-2003 06:51 PM


All times are GMT -5. The time now is 10:54 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration