bug-gawk
[Top][All Lists]
Advanced

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

Re: [bug-gawk] Is there a way to print a character in the escaped form?


From: david kerns
Subject: Re: [bug-gawk] Is there a way to print a character in the escaped form?
Date: Tue, 22 Jan 2019 14:44:43 -0700

On Tue, Jan 22, 2019 at 2:18 PM Peng Yu <address@hidden> wrote:

> > this worked for me: (I entered 2 lines after the command)
> > $ awk '{gsub("\t", "\\t", $0); print }'
> > a       a
> > a\ta
> > a       b       c
> > a\tb\tc
>
> I'd expect something more like %q in Go Printf() beatifically print
> the control characters. It can not be easily done in awk?
>
> --
> Regards,
> Peng
>

easy is a relative term ...

$ cat /tmp/bp.awk
function beatifically_print(str)
{
  gsub("\a", "\\a", str);
  gsub("\b", "\\b", str);
  gsub("\n", "\\n", str);
  gsub("\r", "\\r", str);
  gsub("\t", "\\t", str);
  gsub("\v", "\\v", str);
  print str;
}
BEGIN {
  str = sprintf("%1.9f this line (%d) uses the escape chars awk knows \a \b
\r \t \v \n", 2 * atan2(1,0), 1);
  beatifically_print(str);
  print str;
}
$ awk -f /tmp/bp.awk
3.141592654 this line (1) uses the escape chars awk knows \a \b \r \t \v \n
         54 this line (1) uses the escape chars awk knows


$


reply via email to

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