How to get FILENAME with foreach & tr pipe 
Author Message
 How to get FILENAME with foreach & tr pipe

How can I get nawk to find the filename of a file that is passed to it
through a pipe?

Before I had to add the tr command to my awk program, I put the foreach
variable name after my nawk command string, and it would print the name
of each file as it processed it. Now I have to put the variable name
after the first tr command to get this to work. I get the text I want
out of my part file, but nawk doesn't know what the name of the file
is it is processing. Is there another way to do this? Can I assign the
filename to it's own variable name and then use that in the print
command? how?

Here is my command string:
==========================
foreach prt (*.prt*)
foreach? tr "\343" "\012" < $prt | tr "\342" "\012" | tr "\0" "\012" |
sed 's/SAME AS DES/junk/' | nawk   '/DES/ {c_disc = NR+2};(NR == c_disc)
{descrpt = substr($0,2)};END {print (FILENAME,"\n"descrpt,"\n""#")} '
foreach? end

Which gives the following output:
=================================
-                         <-- Not printing the file name. :-(
NUT,WELD,1/2-13THD,4 PJTN
#
--
+-----------------------------------------------------------------+
| David A. Haigh                                                  |
| Alcatel USA                                                     |
| 1400 McDowell Blvd, Petaluma, CA 94954                          |
| phone:  (707)7927-007   <--- Yes double "O" seven!              |
| fax:    (707)792-7059                                           |

+-----------------------------------------------------------------+
 The opinions represented here are not necessarily those of Alactel



Sat, 16 Aug 2003 05:44:15 GMT  
 How to get FILENAME with foreach & tr pipe

Quote:
> How can I get nawk to find the filename of a file that is passed to it
> through a pipe?

> Before I had to add the tr command to my awk program, I put the foreach
> variable name after my nawk command string, and it would print the name
> of each file as it processed it. Now I have to put the variable name
> after the first tr command to get this to work. I get the text I want
> out of my part file, but nawk doesn't know what the name of the file
> is it is processing. Is there another way to do this? Can I assign the
> filename to it's own variable name and then use that in the print
> command? how?

> Here is my command string:
> ==========================
> foreach prt (*.prt*)
> foreach? tr "\343" "\012" < $prt | tr "\342" "\012" | tr "\0" "\012" |
> sed 's/SAME AS DES/junk/' | nawk   '/DES/ {c_disc = NR+2};(NR == c_disc)
> {descrpt = substr($0,2)};END {print (FILENAME,"\n"descrpt,"\n""#")} '
> foreach? end

> Which gives the following output:
> =================================
> -                         <-- Not printing the file name. :-(
> NUT,WELD,1/2-13THD,4 PJTN
> #
> --

The 'FILENAME' variable is set to the name of the current input file, that
is, the one being implicitly processed in the 'main' block. If you do not
specify an input file on the command-line, the value of 'FILENAME' defaults
to '-', representing 'stdin'. So, your program actually *is* printing the
filename; you just didn't know it :)

You will find that if you place a 'print FILENAME;' statement in your
program's 'main' block, a '-' will be printed for each record processed. It
should also be printed, once, in any such statement in the 'END' block (as
it is in your code).

Now, you *can* pass variable names with assigned values on the command-line.
For example:

     awk -vtheFilename="myFile" '...'

Thus, you could pass the name of the file from which input is being piped in
this manner. The variable will be available in all blocks, which, of course,
includes the 'BEGIN' block.

So, if the above approach is adopted, your 'print' statement would be:

    print theFilename ...

rather than:

    print FILENAME ...

I hope this helps.



Sat, 16 Aug 2003 07:25:11 GMT  
 How to get FILENAME with foreach & tr pipe

Quote:

> Now, you *can* pass variable names with assigned values on the command-line.
> For example:

>      awk -vtheFilename="myFile" '...'

Anthony:
I guess I don't understand. I tried the following:
==================================================
finland 151% foreach prt (i2*.prt*)
foreach? tr "\343" "\012" < $prt | tr "\342" "\012" | tr "\0" "\012" |
sed 's/SAME AS DES/junk/' | nawk -vthefilename="$prt"  '/DES/ {c_disc =
NR+2};(NR == c_disc) {description = substr($0,2)};END {print
(thefilename,"\n"description,"\n""#")} '
foreach? end

Which gave me this:
===================
                          <-- nothing :-(
NUT,WELD,1/2-13THD,4 PJTN
#

Then I tried this:
==================
tr "\343" "\012" < i2-bf_2012.prt| tr "\342" "\012" | tr "\0" "\012" |
sed 's/SAME AS DES/junk/' | nawk -vthefilename="i2-bf_2012.prt" '/DES/
{c_disc = NR+2};(NR == c_disc) {description = substr($0,2)};END {print
(thefilename,"\n"description,"\n""#")} '

Which gave me the same thing:
=============================
                             <-- nothing :-(
NUT,WELD,1/2-13THD,4 PJTN
#

I finally tried this:
=====================
finland 156% foreach prt (i2*.prt*)
foreach? set filename=$prt
foreach? tr "\343" "\012" < $prt | tr "\342" "\012" | tr "\0" "\012" |
sed 's/SAME AS DES/junk/' | nawk -vthefilename="$filename"  '/DES/
{c_disc = NR+2};(NR == c_disc) {description = substr($0,2)};END {print
(thefilename,"\n"description,"\n""#")} '
foreach? end

Which once again gave me this:
==============================
                           <-- nothing :-(
NUT,WELD,1/2-13THD,4 PJTN
#

I'm still looking through the O'Reilly book trying to figure this
out. I frustrating to get this far and not know enough to finish.
--
+-----------------------------------------------------------------+
| David A. Haigh                                                  |
| Alcatel USA                                                     |
| 1400 McDowell Blvd, Petaluma, CA 94954                          |
| phone:  (707)7927-007   <--- Yes double "O" seven!              |
| fax:    (707)792-7059                                           |

+-----------------------------------------------------------------+
 The opinions represented here are not necessarily those of Alactel



Sat, 16 Aug 2003 08:17:24 GMT  
 How to get FILENAME with foreach & tr pipe
By the way, my old script, which worked on the old file format, was
pretty simple. Here is it is.

foreach prt (*.prt*)
nawk   '/ DES/ {c_disc = NR+3};\
(NR == c_disc) && ($3 !~ "NULL") {description = substr($0,6)};\
END {print (FILENAME,"\n"description,"\n""#")} ' $prt

Which gave me this:
===================
158-0602-010.prt.2   <-- The filename :-)
NUT,CAP,6-32,ST,ZNPL,LW CRWN
#
158-0903-007.prt.2
NUT,CLIP,1/4-20,.125-.156THK PNL,CRES
#

--
+-----------------------------------------------------------------+
| David A. Haigh                                                  |
| Alcatel USA                                                     |
| 1400 McDowell Blvd, Petaluma, CA 94954                          |
| phone:  (707)7927-007   <--- Yes double "O" seven!              |
| fax:    (707)792-7059                                           |

+-----------------------------------------------------------------+
 The opinions represented here are not necessarily those of Alactel



Sat, 16 Aug 2003 08:24:37 GMT  
 How to get FILENAME with foreach & tr pipe

Quote:


> > Now, you *can* pass variable names with assigned values on the command-line.
> > For example:

> >      awk -vtheFilename="myFile" '...'

> Anthony:
> I guess I don't understand. I tried the following:
> ==================================================
> finland 151% foreach prt (i2*.prt*)
> foreach? tr "\343" "\012" < $prt | tr "\342" "\012" | tr "\0" "\012" |
> sed 's/SAME AS DES/junk/' | nawk -vthefilename="$prt"  '/DES/ {c_disc =

Did you try to set the thefilename env variable without quoting $prt?


Sat, 16 Aug 2003 08:54:23 GMT  
 How to get FILENAME with foreach & tr pipe


Quote:
>How can I get nawk to find the filename of a file that is passed to it
>through a pipe?

Basicly, you cannot.  Awk sees the piped input as if it were
from standard input.

Quote:
>Before I had to add the tr command to my awk program, I put the foreach
>variable name after my nawk command string, and it would print the name
>of each file as it processed it. Now I have to put the variable name
>after the first tr command to get this to work. I get the text I want
A
>out of my part file, but nawk doesn't know what the name of the file
>is it is processing. Is there another way to do this? Can I assign the
>filename to it's own variable name and then use that in the print
>command? how?

>Here is my command string:
>==========================
>foreach prt (*.prt*)
>foreach? tr "\343" "\012" < $prt | tr "\342" "\012" | tr "\0" "\012" |
>sed 's/SAME AS DES/junk/' | nawk   '/DES/ {c_disc = NR+2};(NR == c_disc)
>{descrpt = substr($0,2)};END {print (FILENAME,"\n"descrpt,"\n""#")} '
>foreach? end

>Which gives the following output:
>=================================
>-                         <-- Not printing the file name. :-(
>NUT,WELD,1/2-13THD,4 PJTN

that's because to awk the input looks like standard input and as
such FILENAME is never assigned.

So instead, pass the file name as a value, something like this:

foreach prt (*.prt*)
foreach? tr "\343" "\012" < $prt | tr "\342" "\012" | tr "\0" "\012" |
sed 's/SAME AS DES/junk/' |
nawk   -v myfile="$prt" '/DES/ {c_disc = NR+2};(NR == c_disc)
{descrpt = substr($0,2)};END {print (myfile,"\n"descrpt,"\n""#")} '
foreach? end

Chuck Demas

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sat, 16 Aug 2003 07:40:15 GMT  
 How to get FILENAME with foreach & tr pipe

Quote:
> By the way, my old script, which worked on the old file format, was
> pretty simple. Here is it is.

> foreach prt (*.prt*)
> nawk   '/ DES/ {c_disc = NR+3};\
> (NR == c_disc) && ($3 !~ "NULL") {description = substr($0,6)};\
> END {print (FILENAME,"\n"description,"\n""#")} ' $prt

> Which gave me this:
> ===================
> 158-0602-010.prt.2   <-- The filename :-)
> NUT,CAP,6-32,ST,ZNPL,LW CRWN
> #
> 158-0903-007.prt.2
> NUT,CLIP,1/4-20,.125-.156THK PNL,CRES
> #

David,

Your old script received the filename as the last parameter on the
command-line, which is what 'awk' normally expects will be, a filename. It
is this value that is placed in 'FILENAME'.

Your newer script pipes a stream into 'awk', via 'stdin', and does *not*
provide a filename as an explicit command-line parameter. The value of
'FILENAME' when this is done, is '-'.

To explicitly pass a variable/value to the 'awk' script, and in your case,
the name of a file in the variable 'theFilename', you do as follows:

      awk -vtheFilename="..."        OR     awk -v theFilename="..."

Within your 'awk' script you will be able to access the variable,
'theFilename', which will contain the value you assigned on the
command-line.

This is a simple method for your 'awk' script to gain access to the name of
the input file since it cannot obtain it from the piped input stream.

Is this any clearer ?



Sat, 16 Aug 2003 12:08:13 GMT  
 How to get FILENAME with foreach & tr pipe

Quote:

> How can I get nawk to find the filename of a file that is passed to it
> through a pipe?

You can't. Or rather you'll have to pass it to awk explicitly.
Or sometimes you can call the pipe from within awk.

Quote:
> Before I had to add the tr command to my awk program, I put the foreach
> variable name after my nawk command string, and it would print the name
> of each file as it processed it. Now I have to put the variable name
> after the first tr command to get this to work. I get the text I want
> out of my part file, but nawk doesn't know what the name of the file
> is it is processing. Is there another way to do this? Can I assign the
> filename to it's own variable name and then use that in the print
> command? how?

> Here is my command string:
> ==========================
> foreach prt (*.prt*)
> foreach? tr "\343" "\012" < $prt | tr "\342" "\012" | tr "\0" "\012" |
> sed 's/SAME AS DES/junk/' | nawk   '/DES/ {c_disc = NR+2};(NR == c_disc)
> {descrpt = substr($0,2)};END {print (FILENAME,"\n"descrpt,"\n""#")} '
> foreach? end

awk -v FILE=$prt '...'

and use FILE instead of FILENAME there.

By the way, a single tr is sufficient (tr '\343\342\0' '\012\012\012'
or simpler tr '\343\342\0' '[\n*]'). Also the sed part could be done
with awk, so how about

...
tr '\343\342\0' '[\n*]' <$prt |
 nawk -v FILE=$prt '/DES/ && !/SAME AS DES/{
      getline; getline; descrpt=substr($0,2) }
 END {print (FILE,"\n"descrpt,"\n""#")} '
...

Incidentally, unless you expect there to be several DES lines of
which all but the last should be discarded, you could do simply

...
tr '\343\342\0' '[\n*]' <$prt |
 nawk -v FILE=$prt '/DES/ && !/SAME AS DES/{ getline; getline
 print FILE,"\n"substr($0,2),"\n""#"; exit} '
...

--
Tapani Tarvainen



Sat, 16 Aug 2003 17:11:57 GMT  
 How to get FILENAME with foreach & tr pipe

Quote:

> foreach? tr "\343\342\0" "\012" < $prt | sed 's/SAME AS DES/junk/' |

Warning: that tr command is not portable (are you sure it works
even in your machine?). In most tr's it would only convert \343.
Instead, use

tr "\343\342\0" "\012\012\012"

or

tr "\343\342\0" "[\012*]"

--
Tapani Tarvainen



Sun, 17 Aug 2003 17:24:55 GMT  
 How to get FILENAME with foreach & tr pipe

Quote:

> > tr "\343\342\0" "\012" < $prt

> Warning: that tr command is not portable (are you sure it works
> even in your machine?). In most tr's it would only convert \343.
> Instead, use

> tr "\343\342\0" "[\012*]"

Wow! your right, it works as I wrote it on my SGI machine, but when
I tried it on my SUN I got junk. Another thing about the Solaris is
that /usr/bin/tr will not find and replace the null character. I had
to use:

 /usr/xpg4/bin/tr "\343\342\000" "[\012*]"

--
+-----------------------------------------------------------------+
| David A. Haigh                                                  |
| Alcatel USA                                                     |
| 1400 McDowell Blvd, Petaluma, CA 94954                          |
| phone:  (707)7927-007   <--- Yes double "O" seven!              |
| fax:    (707)792-7059                                           |

+-----------------------------------------------------------------+
 The opinions represented here are not necessarily those of Alactel



Mon, 18 Aug 2003 08:35:12 GMT  
 How to get FILENAME with foreach & tr pipe

Quote:

> > > tr "\343\342\0" "\012" < $prt

> > Warning: that tr command is not portable
> Wow! your right, it works as I wrote it on my SGI machine, but when
> I tried it on my SUN I got junk. Another thing about the Solaris is
> that /usr/bin/tr will not find and replace the null character. I had
> to use:

>  /usr/xpg4/bin/tr "\343\342\000" "[\012*]"

Indeed in SunOS there are old (pre-POSIX) versions of various commands
in /usr/bin and /bin and newer ones in /usr/xpg4/bin, notably both
awk and tr.

By the way, it should not be necessary to use three zeroes for null in
a POSIXy tr:

/usr/xpg4/bin/tr '\343\342\0' '[\012*]'

should work just fine (and works in a Sun I just tried it in).
Not that the extra zeroes hurt anything, but if you find it
doesn't work with just one, it probably means your shell is doing
the backslash interpretation instead of tr (that should not happen
in a POSIX-compliant shell but...), and it may cause you surprises
if you modify it later.  To avoid that, use single quotes - no
shell should mess with backslashes then. In general single quotes
should be used unless you *want* the shell to do something to the
contents (well, double quotes are also convenient around strings
that contains single quotes).

--
Tapani Tarvainen



Mon, 18 Aug 2003 14:34:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Template for getting filenames and options in OS/2

2. MS Fortran PS getting filenames from command line

3. getting filename and ext of URL

4. Getting Unicode filenames in Windows 2000

5. getting full filename under NT

6. Getting "Short" filenames inWindows

7. Getting filenames into a listbox

8. getting filename from UNIX file descriptor

9. getting line number and filename at runtime

10. Getting list of filenames from a directory TCL+Win2000+Apache

11. Getting filename of currently executing TCL script

12. Getting Filenames

 

 
Powered by phpBB® Forum Software