grep, awk, typeset in a shell script..

For e.g I have a file named "relation" which has three coloums i.e

JOHN MARY JACK
PETE ALISIA JONNY
TONY JACKIE VICTOR

If I do
grep -w 'JOHN' relation | awk '{print""$1" is husband of "$2" & father of "$3""}'

It gives out
JOHN i husband of MARY & father of JACK (which is desired output for me)

I put this thing into script but it stops working. All I am doing is to change the input in Upper case & using grep, awk after that.

#!/usr/bin/ksh
name=$1
print "$name"
echo "$name" | tr '[a-z]' '[A-Z]' >$NAME
print "$NAME"
grep -w "$NAME" relation |awk '{print""$1" is husband of "$2" & father of "$3""}' >$out
print "$out"
exit 0

If I run it like ./scriptname john
it gives output
john
JOHN

Can anyone plz help me to fix it..

I had given an example.

This line is incorrect. Insted of assigning the result to the variable NAME, you are redirecting the output of the command to a file, whose name is stored in the variable NAME (which I presume is currently undefined).

echo "$name" | tr '[a-z]' '[A-Z]' >$NAME

I think this will give the result you expect:

name=$(echo "$name" | tr '[a-z]' '[A-Z]')
# or this, but I prefer the previous syntax
name=`echo "$name" | tr '[a-z]' '[A-Z]'`

Similarly with $out, you need to make the same change.

nawk '{print toupper($1)" is husband of"toupper($2)" & father of "toupper($3)}' filename

Thanks for the reply but this thing won't work since I am converting lowercase to Uppercase so that the grep can work b'coz in the file everything is in uppercase. I m doing something wrong in grep statement not in awk one.

What does your code look like now?

Have you made the same change to the grep line as I recommended for the tr line?

Will try that when I go to office tommorow..Thanks for the help.:slight_smile:

Nothing here is ksh-specific, so I changed it to use /bin/sh

#!/bin/sh
name=$1
print "$name"
nawk -v name="$1" '$1 == toupper(name){print""$1" is husband of "$2" & father of "$3""}'
exit 0

I think you are expecting the redirections (>$out etc) to assign the output to a variable, but it doesn't do that. It sends the output to a file whose name is $out

Like Annihilannic tried to explain, the syntax for assigning the output from a command to a variable is var=`commands` or equivalently var=$(commands) ... but unless you need the variable for something else than just printing it immediately, using variables at all is unnecessary.

Thanks Annihilannic everything works now. :b: