Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Grep on a specific field and print results. NO AWK!!

Posted on 2004-10-03
14
Medium Priority
?
1,302 Views
Last Modified: 2012-06-27
Hi,

I'm trying to use grep to search for a string in a table. However I need to limit the search to a specific field as I dont want any results from the first field if it contains the string.  Then I need to print a few of the fields from the lines that contain my string in the field I need to search on.  Here's an example of my data:

Toteve     Sam      Denver
Denver    John     Aspen
Ellis        Joe       Witchita
Toms       Mary     Denver

I want to search column 3 for Denver, but not pick up the John Denver line as I'm only searching column 3.  It's easy in awk, but I cant use awk... Do I use Grep....I dont see how I can use Grep.

Thanks,

Sam

0
Comment
Question by:elwayisgod
  • 4
  • 4
  • 3
  • +1
14 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 12214444
If Denver is the last word and what follows is a newline character you can do a


grep 'Denver$' filename
0
 
LVL 38

Expert Comment

by:yuzh
ID: 12214445
Is is a fix length table, you can use grep, eg the 3rd start from 11th position, you do:

grep ^..........Aspen filename

it will match "Denver    John     Aspen"

It is a lot easier to use awk to do the job.
0
 
LVL 11

Expert Comment

by:avizit
ID: 12214449
make that

grep ' Denver$' filename

ie a space before the D
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:elwayisgod
ID: 12214510
It's variable length fields, thus cant do the position solution. Tried that. I also have to pass the variable in because I ask what State they want to search on. Here is my code and the datafile called data.5:  

#!/bin/ksh

echo "What State are you looking for /n"
read state

grep ' state$ data.5 > file5
cat file5


Dunny      John      303-555-1212      Utah
Knox      Bill      402-498-7890      Nebraska
Jones      Mary      908-456-1243      Montana
Smith      Barb      319-456-2390      Utah
Utah      Hank      301-494-1009      Wyoming
0
 
LVL 48

Expert Comment

by:Tintin
ID: 12214511
elwayisgod.

You already had these answers in the other question you posted.  Were they not suitable?

Also, please explain why you *can't* use awk.  Is that because you don't want to, or because you've been told you can't use it?


avizit, I assume you amended your grep to add a space in case there are any cities that end in Denver (highly unlikely)?  Of course, it won't work if the data is tab separated.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 12214518
elwayisgod.

You already had these answers in the other question you posted.  Were they not suitable?

Also, please explain why you *can't* use awk.  Is that because you don't want to, or because you've been told you can't use it?


avizit, I assume you amended your grep to add a space in case there are any cities that end in Denver (highly unlikely)?  Of course, it won't work if the data is tab separated.
0
 
LVL 11

Expert Comment

by:avizit
ID: 12214559
You are right Tintin , Thanks for pointing that out .. what would be the walk around btw ..I checked that grep doesn't understand '\s' or '\t' even
0
 

Author Comment

by:elwayisgod
ID: 12214560
It is in 'awk' now. See below. i need to convert it to non awk. Just following orders.  We have to get it to non awk for personal preference reasons. I have never done it in non awk. I got side tracked on the other question and closed it as it was getting too confusing. I'm trying to stay focused here as to not break any rules :)  I'm just not understanding how to convert the following to non awk. If you look at my try above, you can see I'm not even close.

AWK Code:

#!/bin/ksh

nawk '$4 == "Utah" { print $4, $2, $1, $3 }'  data.5

Data:

Dunny    John    303-555-1212    Utah
Knox    Bill    402-498-7890    Nebraska
Jones    Mary    908-456-1243    Montana
Smith    Barb    319-456-2390    Utah
Utah    Hank    301-494-1009    Wyoming


Output:

broncos:/Users/stoteve/ex1.awk
Utah John Dunny 303-555-1212
Utah Barb Smith 319-456-2390
0
 
LVL 38

Expert Comment

by:yuzh
ID: 12215031
Life is tough without awk, you need to re-invent the wheel:

assume your data look like:
Knox    Bill    402-498-7890    Nebraska

and you want to search the 2nd col

Here's an example script:
#!/bin/ksh
FILE=/path-to/datafile
echo "please enter a name: \c"
read AA

#search the file and locate the record

exec 0<$FILE
while read SNAME NAME PH STATE
do
    if [ "$NAME" = "$AA" ] ; then
        # found it
        echo "$STATE $SNAME  $NAME $PH \n"
    fi
done
exit

#End of script.

PS: you can modify the script to make it "ignore case"


0
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 12215287
It certainly helps to see the bigger picture.  In your awk example, you can convert it into

#!/bin/ksh
grep "Utah$" data.5 | while read last first phone state
do
  echo "$state $first $last $phone"
done

Using grep will be quicker than using a while loop to read in every line in the data file.

0
 
LVL 38

Expert Comment

by:yuzh
ID: 12215396
"grep + while read " will speed up a bit, you still need the IF statement to print the record.
0
 

Author Comment

by:elwayisgod
ID: 12217045
Hi,

When "Utah$" is used. What does it mean? I understand everything but the signifigance of the $ after the search criteria.   This works.

Thanks,

Sam
0
 
LVL 11

Expert Comment

by:avizit
ID: 12217072
the $ in a regular expresion means the end of line ..

so utah$ matches only those lines which have utah in the end.
0
 

Author Comment

by:elwayisgod
ID: 12217307
workded perfectly
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses
Course of the Month8 days, 7 hours left to enroll

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question