nawk question

nawk question

Post by wh.. » Sat, 08 May 1993 02:49:24



Greetings to all-
        Here's a sample of my csh script that uses nawk to search a datafile.
Everything works fine but I want the part '$5 ~ search' to match the beginning
of the internal variable 'search', kinda like '$5 ~ ^search'  but it doesnt
like that.  what I ended up doing was changing that part to
                ' $5 ~ /^'"$opt1"'/ '
        That way worked fine using the csh variable.  I would rather use the
internal nawk variable (it would seem a bit cleaner).  any other suggestions
would be greatly appreciated.

if ($opt1 == "-M" && $?opt2 == 1) then
  nawk -F: 'BEGIN {found = 0 ; \
            search = toupper(search1)} \
$5 ~ search {found = 1 ; printf ("%-29s%-16s%-7s%-10s%-11s%-7s\n",$1,$2,$6) \

Quote:} END {if (found == 0) print "No Matches"}' search1=$opt2 $phonefile

  goto bottom
endif

BTW - I tried several other ways.  like '/^search/', but then it takes the
variable search literally.  how can I tell it that its a variable inside the
'//' 's ?  Is there a metacharacter that I overlooked?

                                                -Chris

 
 
 

nawk question

Post by Henk J. Sn » Sat, 08 May 1993 22:44:16



>>   Greetings to all-
>>           Here's a sample of my csh script that uses nawk to search a datafile.
>>   Everything works fine but I want the part '$5 ~ search' to match the beginning
>>   of the internal variable 'search', kinda like '$5 ~ ^search'  but it doesnt
>>   like that.  what I ended up doing was changing that part to
>>                   ' $5 ~ /^'"$opt1"'/ '
>> A   That way worked fine using the csh variable.  I would rather use the
>>   internal nawk variable (it would seem a bit cleaner).  any other suggestions
>>   would be greatly appreciated.

>>   if ($opt1 == "-M" && $?opt2 == 1) then
>>     nawk -F: 'BEGIN {found = 0 ; \
>>               search = toupper(search1)} \
>>   $5 ~ search {found = 1 ; printf ("%-29s%-16s%-7s%-10s%-11s%-7s\n",$1,$2,$6) \
>>   } END {if (found == 0) print "No Matches"}' search1=$opt2 $phonefile
>>     goto bottom
>>   endif

Chris,

Try this:

if ($opt1 == "-M" && $?opt2 == 1)
then
        nawk -F: '
        BEGIN {found = 0
                search = sprintf"^%s", toupper(search1))
        }
        $5 ~ search {
                found = 1
                printf ("%-29s%-16s%-7s%-10s%-11s%-7s\n",$1,$2,$6\)

        }
        END {
                if (found == 0)
                         print "No Matches"
        }' search1=$opt2 $phonefile

Have fun,
--
Henk Snel
Network & System Administrator
Information Technology
Room BG-S64
Larenseweg 50 - 1200 BD Hilversum
The Netherlands

 
 
 

1. two simple nawk questions, Sunos 4.1.2 and nawk

I am trying to nawk through a file and change in the 20th field the value
of the date from mo/da/year, as in 02/30/1993, to mo/ld/year, as in
02/28/1993( where da is any particular day, even wrong ones, and ld is the
correct last day of the month), and the two problems I can't solve are,
that I can't get the shell wrapper to work like I would like without having
to specify a file on the command line, and I am having trouble finding a
way to print out the line when I am done, using ranges, like $1-$19.

I would like to do:

1)
#! /bin/nawk -f # Call the nawk script without any parameters
BEGIN { FS = "|";
       OFS = "|" }
{ while ( getline <"Data.file" > 0 )

...rest of script...

How can I run this program without having to use:

mysys: ~ $ datefix Data.file

In otherwords, just by typing the script name( without Data.file).

2)
How do I specify a range for a print statement:

print $1-$19,date,$21-$37

Right now it seems I have to have a line like:

print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14...goes on and on till
I wanna scream!

I am sorry if these are obvious questions or FAQ's, but nothing about them
appears in the O'Rielly book, or anywhere else.  I guess the creaters of
nawk expect you to write filters like:

mysys: ~ $ datefix -F\| -f20 < Data.file > Data.file.fixed

but I want to do it my way.
--



{...}!cs.utexas!cactus.org!cheselka (512)452-9412v ( 24hours answer. mach.)

2. LOCAL/WWW: First mirror of the LUG list

3. NAWK -- Question

4. Traffic Shaping Limitations?

5. lex & nawk : Questions

6. DIFF3 Experiences

7. nawk question, split string

8. PPP problems with 2.0.27?

9. nawk question

10. A Simple nawk Question.

11. NAWK question

12. A Novice^2 nawk question

13. nawk question