help-gnu-utils
[Top][All Lists]
Advanced

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

Re: GAWK error : replace newline with tab in WinXP ?


From: Rajan
Subject: Re: GAWK error : replace newline with tab in WinXP ?
Date: Wed, 26 Mar 2008 20:50:07 -0400



"Harlan Grove" <hrlngrv@gmail.com> wrote in message 649fac5b-0093-46f3-928c-550ae7736268@b64g2000hsa.googlegroups.com">news:649fac5b-0093-46f3-928c-550ae7736268@b64g2000hsa.googlegroups.com...
John Bartley K7AAY <john.bart...@gmail.com> wrote...
awk 'ORS=%NR%1?",":"\t"' %1.tsv > %1.csv

Aside from the typo (not your fault), NR%1 is an error. NR will only
evaluate to integer values, so NR%1 will ALWAYS evaluate to 0, so the
ternary expression NR%1?",":"\t" would ALWAYS evaluate to "\t".

This line of code, . . .
fails for me while running GNUWIN32 gawk 3.1.0  and I would
appreciate guidance from y'all in finding a fix.
...

CMD doesn't understand single quotes, and the only versions of gawk
that rebuild Unix shell-like command lines are 16-bit versions
compiled with DJGPP which may cause problems with long filenames.

Also, if you mean the following 1-liner from the linked article,

# concatenate every 5 lines of input, using a comma separator
# between fields
awk 'ORS=%NR%5?",":"\n"' file

then it's got a typo. It should be

awk 'ORS=NR%5?",":"\n"' file

but that still requires Unix shell-like quote processing.

I am trying to take a three line file and turn it into a one-line,
tab-separated file, so I can (later on) concatenate multiple files
into one CSV for import into Excel.

Do you mean you want a file containing

line1
line2
line3

to become

line1[tab]line2[tab]line3

and you're going to be doing this in a CMD batch file? If so, you
should use something like

awk "ORS=NR%3?tab:nl" tab=\t nl=\n %1.tsv > %1.csv

I think % has a special meaning in a batch file. Below should work.
gawk "ORS=NR%%3?tab:nl" tab=\t nl=\n %1.tsv > %1.csv
or
gawk  "ORS=NR%%3?"""\t""":"""\n"""" %1.tsv > %1.csv
or
gawk -v t="\t"  -v n="\n" "ORS=NR%%3 ? t: n" %1.tsv > %1.csv

reply via email to

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