Printing filename 
Author Message
 Printing filename

I have an executable awk script that doesn't want to print the filename
variable (which is passed to it in the command line) in the BEGIN statement.
If I put it anywhere else in the code it prints it correctly... I've even
tried reformatting my code :-).

--
#!/bin/gawk

BEGIN {FS = "\""; print FILENAME}

...
---

doesn't work with
BEGIN { FS="\""
               print FILENAME}
either.

This is going to be ridiculously easy I know, but it's been bugging me for
ages!

While I'm here, I currently have an if statement looking at 4 regexp's, is
there any way to shorten this or make it any more efficient, within one
regexp perhaps? It just looks... messy... at the moment.

--
if (x ~ /^.+\.htm$/ || x ~ /^.+\.html$/ || x ~ /^.+\.asp$/ || x ~
/^.+\.php$/)
{
    code...

Quote:
}

--

Thanks in advance,

Paul

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system ( http://www.*-*-*.com/ ).
Version: 6.0.467 / Virus Database: 266 - Release Date: 01/04/2003



Mon, 03 Oct 2005 23:45:51 GMT  
 Printing filename

Quote:

>I have an executable awk script that doesn't want to print the filename
>variable (which is passed to it in the command line) in the BEGIN statement.
>If I put it anywhere else in the code it prints it correctly... I've even
>tried reformatting my code :-).

>--
>#!/bin/gawk

>BEGIN {FS = "\""; print FILENAME}

>...
>---

>doesn't work with
>BEGIN { FS="\""
>               print FILENAME}
>either.

>This is going to be ridiculously easy I know, but it's been bugging me for
>ages!

RTFM

       FILENAME    The name of the current  input  file.   If  no
                   files  are  specified on the command line, the
                   value of FILENAME is "-".   However,        FILENAME
                   is  undefined  inside  the BEGIN block (unless
                   set by getline).

Chuck Demas

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

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



Tue, 04 Oct 2005 01:19:47 GMT  
 Printing filename
[snip]

Quote:
>        FILENAME    The name of the current  input  file.   If  no
>    files  are  specified on the command line, the
>    value of FILENAME is "-".   However, FILENAME
>    is  undefined  inside  the BEGIN block (unless
>    set by getline).

Thanks,

(suitably shamed) Paul

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.467 / Virus Database: 266 - Release Date: 01/04/2003



Tue, 04 Oct 2005 01:31:39 GMT  
 Printing filename


[snip]

Quote:

>While I'm here, I currently have an if statement looking at 4 regexp's, is
>there any way to shorten this or make it any more efficient, within one
>regexp perhaps? It just looks... messy... at the moment.

>--
>if (x ~ /^.+\.htm$/ || x ~ /^.+\.html$/ || x ~ /^.+\.asp$/ || x ~
>/^.+\.php$/)
>{
>    code...
>}
>--

I think /^.+anything$?/ is equivalent to /.anything$/
so you can shorten /^.+\.htm$/ to /.\.htm$/

and
x ~ /^.+\.htm$/ || x ~ /^.+\.html$/
is equivalent to
x ~ /.\.html?$/
where ? means zero or one of the preceding item (l)

finally, move the ORs into the regular expression and group things
using ()s

{
   x=tolower($0)
   if (x ~ /.\.(html?|asp|php)$/)
   {
     print x,"matches"
   }

Quote:
}

Hope this helps
--
Alan Linton


Tue, 04 Oct 2005 03:35:40 GMT  
 Printing filename

X [snip]
X >        FILENAME    The name of the current  input  file.   If  no
X >    files  are  specified on the command line, the
X >    value of FILENAME is "-".   However, FILENAME
X >    is  undefined  inside  the BEGIN block (unless
X >    set by getline).
X
X Thanks,
X
X (suitably shamed) Paul
X
X
X ---
X Outgoing mail is certified Virus Free.
X Checked by AVG anti-virus system (http://www.grisoft.com).
X Version: 6.0.467 / Virus Database: 266 - Release Date: 01/04/2003
X
X

If you want to get to the command line arguments, then look at the
ARGV[] array and the ARGC variables.  These are available in the BEGIN{}
block.

                                        Bob Harris



Tue, 04 Oct 2005 05:27:37 GMT  
 Printing filename
[snip]

Quote:
> I think /^.+anything$?/ is equivalent to /.anything$/
> so you can shorten /^.+\.htm$/ to /.\.htm$/

> and
> x ~ /^.+\.htm$/ || x ~ /^.+\.html$/
> is equivalent to
> x ~ /.\.html?$/
> where ? means zero or one of the preceding item (l)

> finally, move the ORs into the regular expression and group things
> using ()s

> {
>    x=tolower($0)
>    if (x ~ /.\.(html?|asp|php)$/)
>    {
>      print x,"matches"
>    }
> }

> Hope this helps

Excellent, thanks,

Regards,

Paul

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.467 / Virus Database: 266 - Release Date: 01/04/2003



Tue, 04 Oct 2005 05:41:26 GMT  
 Printing filename
On Thu, 17 Apr 2003 16:45:51 +0100, Paul
Quote:

> I have an executable awk script that doesn't want to print the filename
> variable (which is passed to it in the command line) in the BEGIN statement.
> If I put it anywhere else in the code it prints it correctly... I've even
> tried reformatting my code :-).

> --
> #!/bin/gawk

> BEGIN {FS = "\""; print FILENAME}

s/FILENAME/ARGV[1]/


Tue, 04 Oct 2005 10:41:57 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. printing filenames

2. Printing Report To Variable DOS Filenames

3. How to Specify Variable FileName(!FileName)In Report

4. Using variable filename(!FILENAME)

5. Long Filenames okay for physical filenames???

6. REQUIRES filename (NEEDS filename)

7. DOS 8.3 filenames (how to remove long filenames header)

8. Library os.path.isfile(filename) - long filenames error.

9. Adding PC Filename Extensions to Macintosh Filenames

10. PRINT PREVIEWER FREEWARE - FormView - Views HP PCL Print Output / Prints PCL to ANY WINDOWS PRINTER

11. Printing to a Pre-Printed Form

12. How to print the Total of pages to Print

 

 
Powered by phpBB® Forum Software