Using awk to read a specific line and a specific field on that line.

Say the input was as follows:

Brat 20 x 1000 32rf
Pour 15 p 1621 05pr
Dart 10 z 1111 22xx

My program prompts for an input, what I want is to use the input to locate a specific field. Like if I type in, "Pou" then it would return "Pour" and just "Pour"

I currently have this line but it is not giving an output.

                        
var=`grep -c $search filename`
awk  'NR==$var{print $1}' filename

I am using the count for the line number and trying to get the first field. Not working though.
Any suggestions, changes or thoughts?

GNU grep:

pat='Pou'
grep -o "${pat}[^ ]*" INPUTFILE  

what if I wanted to get the first two fields which are both strings and my search only consists of the first field?

I'd use awk, but that's a matter of personal preference. This is the simplest form which will find your search string anywhere on the record:

awk -v what=$search '
    { if( index(  $1, what ) )   # record contains string
        print $1, $2;                # print any part of the record desired
    }
'

If you need to search for a regular expression, then you could use match() instead of index.

awk -vpat="$pat" '$0 ~ pat {print $1, $2}' INPUTFILE

You can change pat (for example $0 ~ "^" pat "[A-Za-z]*" and output as you like.

Thanks, that worked perfectly

---------- Post updated 07-31-11 at 12:23 PM ---------- Previous update was 07-30-11 at 10:59 PM ----------

I have a new problem regarding reading a specific line and a specific field. But now I want to replace the first two fields. I tried this because it looked reasonable:

sed -i "s/$searchName\ $2/$name\ $lname/g" database.txt

John Wall 21 xf gdm

user input to change name: Peter Pan

output becomes

Peter PanWall

I got rid of John but I couldn't get rid of Wall.

This works for me assuming that searchName == John, $2 == Wall. If you've invoked your script with just John on the command line, $2 will be null, then you'll end up with the results that you posted. The small script I wrote to test your code includes some error checking that might be beneficial in your case so I've posted it as an example.


#!/usr/bin/env ksh

if [[ $# != 4 ]]
then
   echo "error: missing parms. expected:  name lastname newname new-lastname"
   exit 1
fi
searchName=$1
# $2 is not assigned and used directly later 
name=$3
lname=$4

sed  "s/$searchName\ $2/$name\ $lname/g" test.data

I would recommend assigning $2 to a named variable; while it is fine the way it is, it makes your script easier to read if it were something like this:

sed  "s/$searchName\ $searchLastname/$name\ $lname/g" test.data

I also don't believe that you need to escape the spaces. This should work:

sed  "s/$searchName $2/$name $lname/g" test.data

and again makes the code easier to read.

---------- Post updated at 13:13 ---------- Previous update was at 13:10 ----------

Thought I would also note that I dropped the -i from my testing because the version of sed that I prefer does not implement it. It should work just fine if you prefer to use that option.