How to prevent awk from reading escape sequences in AWK strings

  • Follow


Hi
I have the following problem. I have build an installer for windows,
which uses an awk script in the process. The installer just edits
(text replace)  awk-script with the the proper path of a certain file.
e.g.

I have a variable in that awk script that holds path information:
(Installer outputs the right hand side)

LogFormat          =  "'C:\Program Files\Logs\System Log %Y-%m-
%d.txt'"

The idea would be that the installer would output a proper path to the
awk script and the scipt would be then run. The problem is that the
installer outputs only a single backslash (as normal windows programs
display a path) . However single backslashes need to be replaced with
a double backslash since this information goes directly to a awk
variable.

I would need a series of awk commands that can turn the contents of
the above variable into

"'C:\\Program Files\\Logs\\System Log %Y-%m-%d.txt'"

with random paths e.g. replace backslashes with double backslashes. I
tryed this with gsup but i was not able to make it work.
0
Reply juhanay (3) 11/11/2008 3:05:39 PM

juhanay@gmail.com wrote:
> Hi
> I have the following problem. I have build an installer for windows,
> which uses an awk script in the process. The installer just edits
> (text replace)  awk-script with the the proper path of a certain file.
> e.g.
> 
> I have a variable in that awk script that holds path information:
> (Installer outputs the right hand side)
> 
> LogFormat          =  "'C:\Program Files\Logs\System Log %Y-%m-
> %d.txt'"
> 
> The idea would be that the installer would output a proper path to the
> awk script and the scipt would be then run. The problem is that the
> installer outputs only a single backslash (as normal windows programs
> display a path) . However single backslashes need to be replaced with
> a double backslash since this information goes directly to a awk
> variable.
> 
> I would need a series of awk commands that can turn the contents of
> the above variable into
> 
> "'C:\\Program Files\\Logs\\System Log %Y-%m-%d.txt'"
> 
> with random paths e.g. replace backslashes with double backslashes. I
> tryed this with gsup but i was not able to make it work.


   awk '{gsub(/\\/,"\\\\")}1' infile >outfile

Make sure (by introducing a proper condition in front of the action)
that no unintended backslashes in your input file will be replaced.

Janis
0
Reply Janis 11/11/2008 5:34:34 PM


In article <gfcfnb$d8b$1@svr7.m-online.net>,
Janis Papanagnou  <janis_papanagnou@hotmail.com> wrote:
....
>   awk '{gsub(/\\/,"\\\\")}1' infile >outfile

Small improvement (a little clearer - and it works for doubling anything):

{gsub(/\\/,"&&")}
1

0
Reply gazelle 11/11/2008 5:45:22 PM

On 11 marras, 19:45, gaze...@shell.xmission.com (Kenny McCormack)
wrote:
> In article <gfcfnb$d8...@svr7.m-online.net>,
> Janis Papanagnou =A0<janis_papanag...@hotmail.com> wrote:
> ...
>
> > =A0 awk '{gsub(/\\/,"\\\\")}1' infile >outfile
>
> Small improvement (a little clearer - and it works for doubling anything)=
:
>
> {gsub(/\\/,"&&")}
> 1

Thanks for the replys. I the above method works for 100% and I will
use it, if there is no other way. I was looking for a way that would
allow sigle quotes like in Perl. E.g. No data is altered in single
quotes.  Another possibility would be that the installer would output
the string "C:\Program Files\Logs\System Log %Y-%m-
%d.txt" to awk-source code and then I would use gsub somehow to modify
that string at runtime.
0
Reply juhanay 11/17/2008 2:06:13 PM

juhanay@gmail.com wrote:
> On 11 marras, 19:45, gaze...@shell.xmission.com (Kenny McCormack)
> wrote:
> 
>>In article <gfcfnb$d8...@svr7.m-online.net>,
>>Janis Papanagnou  <janis_papanag...@hotmail.com> wrote:
>>...
>>
>>
>>>  awk '{gsub(/\\/,"\\\\")}1' infile >outfile
>>
>>Small improvement (a little clearer - and it works for doubling anything):
>>
>>{gsub(/\\/,"&&")}
>>1
> 
> 
> Thanks for the replys. I the above method works for 100% and I will
> use it, if there is no other way. I was looking for a way that would
> allow sigle quotes like in Perl.

That's an easy answer; you're using awk and there is no single quote
syntax construct as in perl or shell or...; because a backslash is a
special escape character.

> E.g. No data is altered in single
> quotes.  Another possibility would be that the installer would output
> the string "C:\Program Files\Logs\System Log %Y-%m-
> %d.txt" to awk-source code and then I would use gsub somehow to modify
> that string at runtime.

There are two inherent "problems" here...

1. Backslash is an _escape character_ that needs special handling.
    This got popular from Unix world where you can find it everywhere;
    in the shell, in the C language, in awk, and many other Unix tools.

2. The WinDOS platform uses a _common escape character_ - which had
    been in use long before Bill left his garage! - as a delimiter for
    the very common path components; with that historic background this
    is a severe misdesign.


Janis
0
Reply Janis 11/17/2008 2:50:17 PM

4 Replies
273 Views

(page loaded in 0.053 seconds)

Similiar Articles:













7/25/2012 9:40:45 PM


Reply: