bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gawk] feature request: expanding escape sequences


From: Aharon Robbins
Subject: Re: [bug-gawk] feature request: expanding escape sequences
Date: Sun, 06 Jul 2014 07:13:07 -0400
User-agent: Heirloom mailx 12.5 6/20/10

Hello Ed.

This is something that can be done fairly easily as a function written
in awk.  In particular, I'd consider using the four-argument version of
split(), which gives you the pieces and the separators, using a regex
that matches escape sequences. You could then convert the strings into
their corresponding values fairly easily, and rejoin all the parts into
the result string.

Another alternative would be to write an extension function in C to
do this.

I don't see this as general purpose enough to require adding another
built-in function to gawk.

Thanks,

Arnold

> Date: Sat, 05 Jul 2014 09:38:17 -0500
> From: Ed Morton <address@hidden>
> To: address@hidden
> Subject: [bug-gawk] feature request: expanding escape sequences
>
> Guys - I just tried to do something conceptually trivial and discovered there 
> is 
> literally NO good way to do it in awk so I was wondering if a gawk function 
> could be provided to do it?
>
> I just want to be able to expand escape sequences in text read 
> from an input file. For example, let's say I have:
>
>      $ cat file
>      a\tb
>      c\td
>
> and I want to output:
>
>      a<tab>b
>      c<tab>d
>
> where "<tab>" is a literal control character.
>
> The most concise solution I've come up with is 
> to invoke the shell to parse the file, e.g.
>
>      $ awk '{ system("printf \047" $0 "\n\047") }' file
>      a       b
>      c       d
>
> but that has some caveats, not least of which is that if the input file 
> contains 
> text like '$(ls)' then that command will be executed!
>
> The alternatives seem to be writing text to manually parse each line to 
> convert 
> every escape-char to it's literal character, or using a script to read the 
> input 
> file to generate another script and then execute that.
>
> I'm not looking for an `eval` function, just something that will convert 
> escape 
> sequences to their equivalent characters so I can do something like this if 
> `file` contains lines of formatting text:
>
>      awk '{ printf expandEscapes($0), "whatever" }' file
>
> and expandEscapes() will just return it's argument as a string with all 
> escape 
> chars expanded (actual function name up for grabs of course).
>
> What do you think?
>
>       Ed.
>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]