Solved

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

Posted on 2004-10-03
14
1,246 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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now