Unix Shell - Awk: seperating records

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2005 > Awk: seperating records





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Awk: seperating records
ronancarty@gmail.com

2005-11-30, 8:48 pm

hi all,

i have a script reading from a file with an awk command! I can get it
to read the amount of records it finds and to display them all in a
variable!!
is there a way i can access say the nth value in the nth record!!
by using $n i can only get the values from the first record that awk
finds!!

cheers for any help!!
ro001

------------------------------------
IFS=:
num_recs
echo "Please Enter the Item you wish to Search for: "
read search
MYNUM=`grep "$search" MYBOOK | wc -l`
MYRECS=`awk "/$search/ "' { print "Record #" NR " (" $1 ")"} ' MYBOOK`


echo "There are $MYNUM records with the value \"$search\" in it!"
echo $MYRECS
set $MYRECS

echo Name: "$1"
---------------------------------

Ed Morton

2005-11-30, 8:48 pm

ronancarty@gmail.com wrote:
> hi all,
>
> i have a script reading from a file with an awk command! I can get it
> to read the amount of records it finds and to display them all in a
> variable!!
> is there a way i can access say the nth value in the nth record!!


Yes:

awk 'NR==10{print $10}'

will access the 10th value (field $10) in the 10th record (NR==10).

> by using $n i can only get the values from the first record that awk
> finds!!


I have no idea what you mean by that.

> cheers for any help!!
> ro001
>
> ------------------------------------
> IFS=:
> num_recs
> echo "Please Enter the Item you wish to Search for: "
> read search
> MYNUM=`grep "$search" MYBOOK | wc -l`
> MYRECS=`awk "/$search/ "' { print "Record #" NR " (" $1 ")"} ' MYBOOK`


No, the correct syntax for the above line is:

MYRECS=`awk -v search="$search" '$0 ~ search{printf "Record #%d
(%s)\n",NR,$1}' MYBOOK`

The way you have it right now is likely to fail dramatically with
obscure error messages given various input string values.

> echo "There are $MYNUM records with the value \"$search\" in it!"
> echo $MYRECS
> set $MYRECS
>
> echo Name: "$1"
> ---------------------------------


Does the above script behave in some way that you don't like and, if so,
what is the problem with it?

Ed.
ronancarty@gmail.com

2005-11-30, 8:48 pm

> by using $n i can only get the values from the first record that awk
> finds!!


i meant by that, if i use say echo $1 and tried to access the second
record that awk found, it would not work. i tried loops etc but still
wudnt work for me!!


Thank you very much for letting me know, it was actually working, but
obviously would have given me trouble in future tasks.


[vbcol=seagreen]
>Does the above script behave in some way that you don't like and, if so,
>what is the problem with it?


No, it was fine for me until i tried to access an nth element of an nth
row!! but hopefully it will work for me now!!

cheers and thank you for your time & knowledge!!

Ed Morton

2005-11-30, 8:48 pm

ronancarty@gmail.com wrote:

>
>
> i meant by that, if i use say echo $1 and tried to access the second
> record that awk found, it would not work. i tried loops etc but still
> wudnt work for me!!


This would work:

MYRECS=`awk -v search="$search" '$0 ~ search{printf "Record #%d
(%s)\n",NR,$1}' MYBOOK`

echo "$MYRECS" | awk 'NR==2{print $1}'

assuming you want to print the first field of the second record and for
some reason don't want to do it all in a single awk command.

Ed.
ronancarty@gmail.com

2005-11-30, 8:48 pm

hi thanks very much again!!

I wudnt mind doing it with two commands!! Once i get it working i
suppose!!

They over all goal for me is too search for a value in my file, display
the records that i find, then allow me to choose the record that i
actually want to edit if there are multiple records containing the
value i enter as $search!!

So i decided to try this using the awk method to get the record before
i edit it!!!

cheers again!! i really appreciate your advice!!

Ed Morton

2005-11-30, 8:48 pm

ronancarty@gmail.com wrote:
> hi thanks very much again!!
>
> I wudnt mind doing it with two commands!! Once i get it working i
> suppose!!
>
> They over all goal for me is too search for a value in my file, display
> the records that i find, then allow me to choose the record that i
> actually want to edit if there are multiple records containing the
> value i enter as $search!!


So, something like this:

echo "enter a search pattern: \c"
read -r search
MYRECS=`awk -v search="$search" '$0 ~ search{printf "Record #%d
(%s)\n",NR,$1}' MYBOOK`
echo "$MYRECS"
echo "enter a record number to edit: \c"
read -r recnr
THISREC=`echo "MYRECS" | awk -v recnr="$recnr" 'NR==recnr{print;exit}'`
echo "$THISREC"

would get the specific record for you.

> So i decided to try this using the awk method to get the record before
> i edit it!!!
>
> cheers again!! i really appreciate your advice!!
>


Then you'll love this: please read http://cfaj.freeshell.org/google
before posting again.

Regards,

Ed.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com