For Programmers: Free Programming Magazines  


Home > Archive > AWK > January 2006 > gawk record and field









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 gawk record and field
Mr_Bill

2006-01-17, 6:56 pm

I have a file, it while acutually be a variable, that will look
like this

field field field field
field field field
field field field
field field field
field field field field field

I need to process each record.

How do I control gawk as it moves through records?
Record 1 will process different than record 2 thru (last -1)
The last record will process differently.

The variable might have 3 records, it might have 50 records.

Hmmm.. I don't think I'm expressing this very well.

I going to turn $myvariable into an html table.
The first record is a header <TABLE><TABLEHEADER>$0</TABLEHEADER>
The middle records will be <TR><TD>$1</TD><TD>$2</TD><TD>$3</TD></TR>
The last record will be <TR><TD>$1</TD><TD>$2</TD><TD>$3</TD></TR></TABLE>

So my question is (hope I'm asking this right) how do I
iterate my way through $myvariable 1 record at a time
and how do I know what record I'm on?

GNU awk 3.1.4
GNU bash, version 3.00.16(1)-release (i586-suse-linux)

Thanks,

Mr_Bill


Ed Morton

2006-01-17, 6:56 pm



Mr_Bill wrote:
> I have a file, it while acutually be a variable, that will look
> like this
>
> field field field field
> field field field
> field field field
> field field field
> field field field field field
>
> I need to process each record.
>
> How do I control gawk as it moves through records?
> Record 1 will process different than record 2 thru (last -1)
> The last record will process differently.
>
> The variable might have 3 records, it might have 50 records.
>
> Hmmm.. I don't think I'm expressing this very well.
>
> I going to turn $myvariable into an html table.
> The first record is a header <TABLE><TABLEHEADER>$0</TABLEHEADER>
> The middle records will be <TR><TD>$1</TD><TD>$2</TD><TD>$3</TD></TR>
> The last record will be

<TR><TD>$1</TD><TD>$2</TD><TD>$3</TD></TR></TABLE>
>
> So my question is (hope I'm asking this right) how do I
> iterate my way through $myvariable 1 record at a time
> and how do I know what record I'm on?
>
> GNU awk 3.1.4
> GNU bash, version 3.00.16(1)-release (i586-suse-linux)
>


"NR" tells you the record number. try this:

echo "$myvariable" | awk '
NR==1 { printf "<TABLE><TABLEHEADER>%s</TABLEHEADER>",$0; next }
{ printf "\n<TR><TD>%s</TD><TD>%s</TD><TD>%s</TD></TR>",$1,$2,$3 }
END { print "\n</TABLE>" }
'

Regards,

Ed.

Mr_Bill

2006-01-18, 3:55 am

On Tue, 17 Jan 2006 09:02:17 -0600, Ed Morton wrote:

> "NR" tells you the record number. try this:
>
> echo "$myvariable" | awk '
> NR==1 { printf "<TABLE><TABLEHEADER>%s</TABLEHEADER>",$0; next }
> { printf "\n<TR><TD>%s</TD><TD>%s</TD><TD>%s</TD></TR>",$1,$2,$3 }
> END { print "\n</TABLE>" }
> '


Thanks, Ed.
That opened my eyes to a lot. Mainly, how poorly
I understood and expressed the problem. . . .

From record NR==2 to record NR==last,
$3 is the name of a PDF file (a runway approach plate).
I need to cp that file to a new directory (images).
$2 will be a link to the file.
{ printf "\n<TR><TD>%s</TD><TD><A HREF="/images/%s">%s</A></TD>",$1,$3,$2 }
Note $2,$3 reversal ^ ^

So.... Can I even do something like cp $3 /images/$3 inside gawk?

I was thinking I would have to do some kind of for statement.
for [i in $myvariable, i++] <--- theoretical
transer=$( echo $myvariable | gawk 'NR==$i{ printf, $3}'
cp the-path-to-images/$transfer the-new-path/$tranfer

Is there a better way to do that within awk?

Thanks again,
Mr_Bill






Ed Morton

2006-01-18, 7:55 am

Mr_Bill wrote:
> On Tue, 17 Jan 2006 09:02:17 -0600, Ed Morton wrote:
>
>
>
>
> Thanks, Ed.
> That opened my eyes to a lot. Mainly, how poorly
> I understood and expressed the problem. . . .
>
> From record NR==2 to record NR==last,
> $3 is the name of a PDF file (a runway approach plate).
> I need to cp that file to a new directory (images).
> $2 will be a link to the file.
> { printf "\n<TR><TD>%s</TD><TD><A HREF="/images/%s">%s</A></TD>",$1,$3,$2 }
> Note $2,$3 reversal ^ ^
>
> So.... Can I even do something like cp $3 /images/$3 inside gawk?


Yes, using system("cp "$3" /images/"$3) or print "cp "$3" /images/"$3 |
"/bin/sh" but that may not be the best way to do what you want.

> I was thinking I would have to do some kind of for statement.
> for [i in $myvariable, i++] <--- theoretical
> transer=$( echo $myvariable | gawk 'NR==$i{ printf, $3}'
> cp the-path-to-images/$transfer the-new-path/$tranfer


Yes, you can do something like that in shell. For information on how to
pass the values of shell variables to awk, see question 24 in the
comp.unix.shell FAQ (http://home.comcast.net/~j.p.h/cus-faq-2.html#24)

> Is there a better way to do that within awk?


No, copying files is best done in the shell (or whatever OS you're
using), but I'm not sure how that requirement fits in with your original
post about wrapping fields in HTML tags. Best advice I can give you is,
given what you know now, post your COMPLETE question with sample input
and desired output to comp.unix.shell (if you're using UNIX).

Ed.
Jay C. James

2006-01-19, 6:57 pm


"Mr_Bill" <No_thanks@null_mail.org> wrote in message
news:pan.2006.01.18.08.15.21.458704@null_mail.org...
> On Tue, 17 Jan 2006 09:02:17 -0600, Ed Morton wrote:
>
"\n<TR><TD>%s</TD><TD>%s</TD><TD>%s</TD></TR>",$1,$2,$3 }[color=darkred]
>
> Thanks, Ed.
> That opened my eyes to a lot. Mainly, how poorly
> I understood and expressed the problem. . . .



Ed continuously opens our eyes to a lot of things :)


Mr_Bill

2006-01-20, 3:55 am

On Thu, 19 Jan 2006 10:18:33 -0800, Jay C. James wrote:

> Ed continuously opens our eyes to a lot of things :)


He and about 5 other unix/linux gurus have opened mine so wide,
I'am about to go blind!

Mr_Bill


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com