Solved

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

Posted on 2004-10-03
14
1,269 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
Independent Software Vendors: 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!

 

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 125 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

680 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