Home > Archive > AWK > December 2004 > Is it possible to get the working directory in Windows gawk?
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 |
Is it possible to get the working directory in Windows gawk?
|
|
| Rufus V. Smith 2004-12-03, 8:55 pm |
| (I am currently using gawk 3.1.0 under Windows XP)
I have a compiler that outputs error messages using relative file names
I am trying to filter the stderr output so the file names are absolute path
names
rather than relative, so I can use the Visual Studio "find next error"
facilities (and
I am not in the directory of the files being compiled that generated the
message -
lot of directory changes and nmakes in the build process)
I have no problems piping the error output through a filter program, but the
problem is, the filter program needs to know the working directory to be
able
to prepend it to the file names.
Under Windows, I can get the current directory in the variable %cd%, for
example
echo %cd%
returns the same as
cd
(or in *nix/*nux)
pwd
I tried to pipe output using a command like this (dir/b is only an example):
dir /b | gawk -f c:\rvsawk\addpath.awk curpath=%cd%
The problem is the \ directory separators are interpreted as special
characters,
of course. If I typed it in manually, I would use a double \\ so the string
would
get the backslash.
Is there an internal variable or a way to access this from awk? "cd"
doesn't come
up as a regular environment string with the set command so I couldn't use
ENVIRON["CD"]
to get it, though other variables, like:
ENVIRON["HOMEPATH"]
work okay.
Anyone know of a way?
Rufus
| |
| Ted Davis 2004-12-03, 8:55 pm |
| On Fri, 3 Dec 2004 16:15:25 -0500, "Rufus V. Smith"
<nospam@nospam.com> wrote:
>dir /b | gawk -f c:\rvsawk\addpath.awk curpath=%cd%
I find this confusing - I find that / workes better than \ in the
script name. It is unclear what "curpath=%cd%" is supposed to
accomplish - "-vcurpath=%cd%" would be clear.
In any case,
dir /b /s
returns fully qualified filespecs (but includes any subdirectories).
Backslashes are not an issue in strings read from the input.
You can use DIR without /b, and extract the directory name from the
header and the file names from the body of the report.
BEGIN{
"CD" | getline
BaseDir = $0
close( "CD")
}
puts the output of the CD command into $0 as input andthen into
BaseDir while preserving the backslashes.
T.E.D. (tdavis@gearbox.maem.umr.edu)
SPAM filter: Messages to this address *must* contain "T.E.D."
somewhere in the body or they will be automatically rejected.
| |
| William James 2004-12-04, 3:55 am |
| Make a batch file containing:
set here=%cd%
dir /b | awk95 -f addpath.awk
You awk program should have:
BEGIN { curpath=ENVIRON["here"] }
| |
| Ted Davis 2004-12-07, 3:59 am |
| On Fri, 3 Dec 2004 16:15:25 -0500, "Rufus V. Smith"
<nospam@nospam.com> wrote:
>dir /b | gawk -f c:\rvsawk\addpath.awk curpath=%cd%
I find this confusing - I find that / workes better than \ in the
script name. It is unclear what "curpath=%cd%" is supposed to
accomplish - "-vcurpath=%cd%" would be clear.
In any case,
dir /b /s
returns fully qualified filespecs (but includes any subdirectories).
Backslashes are not an issue in strings read from the input.
You can use DIR without /b, and extract the directory name from the
header and the file names from the body of the report.
BEGIN{
"CD" | getline
BaseDir = $0
close( "CD")
}
puts the output of the CD command into $0 as input andthen into
BaseDir while preserving the backslashes.
T.E.D. (tdavis@gearbox.maem.umr.edu)
SPAM filter: Messages to this address *must* contain "T.E.D."
somewhere in the body or they will be automatically rejected.
| |
| William James 2004-12-07, 3:59 am |
| Make a batch file containing:
set here=%cd%
dir /b | awk95 -f addpath.awk
You awk program should have:
BEGIN { curpath=ENVIRON["here"] }
| |
| Rufus V. Smith 2004-12-07, 3:59 am |
|
"Rufus V. Smith" <nospam@nospam.com> wrote in message
news:1102108503.u8/1dXablCG7XJCeEG6V8A@teranews...
> (I am currently using gawk 3.1.0 under Windows XP)
>
> I have a compiler that outputs error messages using relative file names
>
> I am trying to filter the stderr output so the file names are absolute
path
> names
> rather than relative, so I can use the Visual Studio "find next error"
> facilities..
<snip>
I finally came up with the following combination:
NMAKE LINE:
----
$(MAKE) /$(MAKEFLAGS) $(LIBDIR)\$(*B).l 2>&1 | gawk -f
/rvsawk/addpath.awk xx=%cd% yy=zz
----
This pipes stderr and stdout through the single stdout pipe into the gawk
filter.
The %cd% does return the current directory
GAWK SCRIPT:
----
BEGIN {
cd=substr(ARGV[1],4,999) "\\";
#print "cd = " cd
}
substr($0,1,1)=="\"" { $1 = "\"" cd substr($1,2,999)
}
{print $0}
----
In order that awk doesn't process the %cd% backslashes as escape
characters, I pull the cd string out of argv[1].
Interestingly, I have to put that additional filler : yy=zz at the end of
the line, otherwise the end of the line gets truncated and I get
%cd
as the directory name.
Rufus
| |
| Rufus V. Smith 2004-12-07, 3:59 am |
|
"Rufus V. Smith" <nospam@nospam.com> wrote in message
news:1102352600. 81f323c92e6bbce4e9ae0cda3d5f6bba@teranew
s...
>
> "Rufus V. Smith" <nospam@nospam.com> wrote in message
> news:1102108503.u8/1dXablCG7XJCeEG6V8A@teranews...
> path
>
> <snip>
>
> I finally came up with the following combination:
>
> NMAKE LINE:
>
> ----
> $(MAKE) /$(MAKEFLAGS) $(LIBDIR)\$(*B).l 2>&1 | gawk -f
> /rvsawk/addpath.awk xx=%cd% yy=zz
> ----
>
> This pipes stderr and stdout through the single stdout pipe into the gawk
> filter.
> The %cd% does return the current directory
>
>
> GAWK SCRIPT:
> ----
> BEGIN {
> cd=substr(ARGV[1],4,999) "\\";
> #print "cd = " cd
> }
> substr($0,1,1)=="\"" { $1 = "\"" cd substr($1,2,999)
> }
> {print $0}
> ----
>
> In order that awk doesn't process the %cd% backslashes as escape
> characters, I pull the cd string out of argv[1].
>
> Interestingly, I have to put that additional filler : yy=zz at the end of
> the line, otherwise the end of the line gets truncated and I get
> %cd
> as the directory name.
>
>
> Rufus
A new wrinkle, it turns out the error code returned is the code of the
filter program,
not the compiler generating the output, which is inconvenient, though it
makes sense.
So now I have to parse the output for an "NMAKE error code" line, extract
the
error code and exit the awk script with the same error code value if
I want the build to terminate.
Anyone know a better way (in windows) to propagate the error status past
the filter program? For example, can the filter program get the status from
the prior program?
I've been trying to avoid turning this into a batch file call.
Rufus
Gee, now that I say this, there may be an %errorstatus% or something like
that
I can append to that command line. Stay tuned...
| |
| Rufus V. Smith 2004-12-07, 3:59 am |
|
"Rufus V. Smith" <nospam@nospam.com> wrote in message
news:1102377899. d4fb40926008a690728feed58e160fb2@teranew
s...
>
> Gee, now that I say this, there may be an %errorstatus% or something like
> that
> I can append to that command line. Stay tuned...
>
Yes, there is an %errorlevel%
No, it's 0 when the filter is run.
ah, well. sticking to parsing.
| |
| William James 2004-12-07, 3:59 am |
| > cd=substr(ARGV[1],4,999) "\\";
cd=substr(ARGV[1],4) "\\"
| |
| Rufus V. Smith 2004-12-07, 3:56 pm |
|
"William James" <w_a_x_man@yahoo.com> wrote in message
news:1102383463.618562.303080@c13g2000cwb.googlegroups.com...
>
> cd=substr(ARGV[1],4) "\\"
>
Thanks!! Mine's a throwback to more ancient languages...
Rufus
|
|
|
|
|