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

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

elwayisgodAsked:
Who is Participating?
 
TintinConnect With a Mentor Commented:
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
 
avizitCommented:
If Denver is the last word and what follows is a newline character you can do a


grep 'Denver$' filename
0
 
yuzhCommented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
avizitCommented:
make that

grep ' Denver$' filename

ie a space before the D
0
 
elwayisgodAuthor Commented:
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
 
TintinCommented:
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
 
TintinCommented:
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
 
avizitCommented:
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
 
elwayisgodAuthor Commented:
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
 
yuzhCommented:
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
 
yuzhCommented:
"grep + while read " will speed up a bit, you still need the IF statement to print the record.
0
 
elwayisgodAuthor Commented:
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
 
avizitCommented:
the $ in a regular expresion means the end of line ..

so utah$ matches only those lines which have utah in the end.
0
 
elwayisgodAuthor Commented:
workded perfectly
0
All Courses

From novice to tech pro — start learning today.