Solved

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

Posted on 2004-10-03
14
1,265 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

839 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