LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Newbie playing with Awk and Grep (https://www.linuxquestions.org/questions/programming-9/newbie-playing-with-awk-and-grep-168132/)

Helene 04-09-2004 10:05 AM

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 ...:confused:

- Helene

paulsm4 04-11-2004 01:50 AM

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

Helene 04-14-2004 08:38 PM

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

paulsm4 04-14-2004 10:00 PM

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).

Tinkster 04-14-2004 11:48 PM

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

Helene 04-15-2004 12:55 AM

This was excellent. Thank you both :)

Tinkster 04-15-2004 02:08 AM

Pleasure ;)

awk rules! :)


Cheers,
Tink


All times are GMT -5. The time now is 08:09 AM.