Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

GAWK error : replace newline with tab in WinXP ?
awk 'ORS=%NR%1?",":"\t"' %1.tsv > %1.csv

This line of code, adapted from http://www.ee.ucl.ac.uk/~hamed/misc...nelle.com/view/
currentview.html) and the physics are using the collected wisdom of
Fred on Everything (http://www.fredoneverything.net/
FOE_Frame_Column.htm).#The tests are run in Ut arcu. Nulla venenatis
egestas risus. Vestibulum nec ligula. Aenean adipiscing risus quis
libero vulputate rutrum. Proin sagittis orci non mi. Mauris magna.
Aliquam malesuada lectus vitae pede. Integer lacus risus, volutpat
sed, ultrices in, venenatis et, libero. Praesent euismod mauris id
turpis hendrerit egestas. Mauris tristique. Aliquam vel enim posuere
nisi vehicula laoreet. Integer tempus vestibulum odio. Donec et quam.
Maecenas lacinia nulla sed est. Donec mollis ligula sed tellus.
Phasellus quis eros a sem vulputate eleifend. Phasellus sagittis felis
id tellus. Nunc condimentum ultricies sem. ##Test 1:# How many Aggies
does it take to screw in a lightbulb? #Sed blandit, tellus non cursus
blandit, est justo sollicitudin lectus, vel mollis lectus magna ac
pede. Aliquam aliquam lorem sed erat. Quisque rutrum, enim sit amet
ornare varius, dui libero lacinia orci, non iaculis mi sapien non
urna. Sed massa ante, elementum ut, posuere quis, interdum eget,
ipsum. Duis blandit condimentum elit. Aliquam volutpat iaculis leo.
Suspendisse nibh nibh, dignissim cursus, elementum sed, convallis eu,
metus. Phasellus vel magna. Aenean nulla nisi, rhoncus non, egestas
non, commodo bibendum, pede. Aliquam justo. Aliquam sapien tortor,
placerat accumsan, auctor porta, rhoncus sed, tortor. Ut tempus.
Curabitur vitae lectus quis lectus venenatis posuere. Curabitur
venenatis. Donec elementum eros. Praesent eget ligula nec metus
molestie hendrerit. Sed at nisi.

Report this thread to moderator Post Follow-up to this message
Old Post
John Bartley K7AAY
03-26-08 12:00 AM


Re: GAWK error : replace newline with tab in WinXP ?
On Mar 25, 4:50=A0pm, John Bartley K7AAY <john.bart...@gmail.com> wrote:
> awk 'ORS=3D%NR%1?",":"\t"' %1.tsv > %1.csv
>
> This line of code, adapted fromhttp://www.ee.ucl.ac.uk/~hamed/misc/awk1lin=[/color
]
e.txt
> fails for me while running GNUWIN32 gawk 3.1.0 =A0

There is a bug in the example on this page.

> 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.
>
> Thank you kindly in advance.
>

Try this, which works for me on Linux:  awk 'ORS=3DNR%3?"\t":"\n"'

For Windows environment: the quoting has to be adapted.

Hermann

Report this thread to moderator Post Follow-up to this message
Old Post
Hermann Peifer
03-26-08 12:00 AM


Re: GAWK error : replace newline with tab in WinXP ?
On Mar 26, 2:50 am, John Bartley K7AAY <john.bart...@gmail.com> wrote:
> awk 'ORS=%NR%1?",":"\t"' %1.tsv > %1.csv
>
> This line of code, adapted fromhttp://www.ee.ucl.ac.uk/~hamed/misc/awk1lin
e.txt
> fails for me while running GNUWIN32 gawk 3.1.0  and I would appreciate
> guidance from y'all in finding a fix.
>
> 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.
>
> Thank you kindly in advance.
>
> The TSV files look something like this:
>
> XY_12345_Acme_Blinder_Coughdrop-Optimal
> 9
> This test will run Lorem ipsum dolor sit amet, consectetuer adipiscing
> elit. Fusce a diam. Etiam sapien pede, placerat quis, malesuada in,
> eleifend quis, velit. Praesent imperdiet cursus odio. Praesent vel
> lorem. Pellentesque dignissim, felis non euismod rhoncus, felis pede
> condimentum lacus, ac accumsan mi leo sit amet lacus. Donec porta nunc
> ut sapien. Morbi pede. Nulla tincidunt nisi mollis augue. Praesent
> dapibus cursus tellus. Sed accumsan dui sed metus. Morbi lacus. Nunc
> placerat arcu tincidunt turpis. Proin tristique ullamcorper velit.
> Proin id justo egestas erat interdum volutpat. Sed sodales massa sit
> amet orci. Duis hendrerit volutpat velit. Aenean vel libero.
> Suspendisse potenti.##Test will run the following sequence:#1. Run in
> circles#2. Scream#3. And shout###The following are is a more in-depth
> look at what each of Praesent tincidunt egestas nulla. Nullam non mi.
> Ut tellus ipsum, molestie fringilla, eleifend non, pulvinar vel,
> sapien. Nullam et mi nec neque tempus iaculis. Sed quam. Aenean orci
> nulla, sodales et, pulvinar sodales, sagittis vel, turpis. Aliquam
> massa quam, porttitor vitae, rhoncus ac, volutpat at, sem. Cras sed
> lorem. Mauris faucibus erat non neque. Nunc bibendum mattis mi.
> Quisque elementum diam at lectus. (http://www.jerrypournelle.com/view/
> currentview.html) and the physics are using the collected wisdom of
> Fred on Everything (http://www.fredoneverything.net/
> FOE_Frame_Column.htm).#The tests are run in Ut arcu. Nulla venenatis
> egestas risus. Vestibulum nec ligula. Aenean adipiscing risus quis
> libero vulputate rutrum. Proin sagittis orci non mi. Mauris magna.
> Aliquam malesuada lectus vitae pede. Integer lacus risus, volutpat
> sed, ultrices in, venenatis et, libero. Praesent euismod mauris id
> turpis hendrerit egestas. Mauris tristique. Aliquam vel enim posuere
> nisi vehicula laoreet. Integer tempus vestibulum odio. Donec et quam.
> Maecenas lacinia nulla sed est. Donec mollis ligula sed tellus.
> Phasellus quis eros a sem vulputate eleifend. Phasellus sagittis felis
> id tellus. Nunc condimentum ultricies sem. ##Test 1:# How many Aggies
> does it take to screw in a lightbulb? #Sed blandit, tellus non cursus
> blandit, est justo sollicitudin lectus, vel mollis lectus magna ac
> pede. Aliquam aliquam lorem sed erat. Quisque rutrum, enim sit amet
> ornare varius, dui libero lacinia orci, non iaculis mi sapien non
> urna. Sed massa ante, elementum ut, posuere quis, interdum eget,
> ipsum. Duis blandit condimentum elit. Aliquam volutpat iaculis leo.
> Suspendisse nibh nibh, dignissim cursus, elementum sed, convallis eu,
> metus. Phasellus vel magna. Aenean nulla nisi, rhoncus non, egestas
> non, commodo bibendum, pede. Aliquam justo. Aliquam sapien tortor,
> placerat accumsan, auctor porta, rhoncus sed, tortor. Ut tempus.
> Curabitur vitae lectus quis lectus venenatis posuere. Curabitur
> venenatis. Donec elementum eros. Praesent eget ligula nec metus
> molestie hendrerit. Sed at nisi.

When I tried your script on Windows XP using GNU Awk 3.1.3, I found
this error:

gawk: cmd. line:1: 'ORS=1?,:\t'
gawk: cmd. line:1: ^ invalid char ''' in expression

If that is the problem you encountered, you have to escape the double-
quote mark in a double-quoted string using the "^" (double-quote caret
double-quote) sequence.  (It's as awkward as it sounds.)

Try:

gawk "ORS=1?"^"","^"":"^""\t"^""" %1.tsv > %1.csv

Regards,

--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</
a>

Report this thread to moderator Post Follow-up to this message
Old Post
Kam-Hung Soh
03-26-08 02:59 AM


Re: GAWK error : replace newline with tab in WinXP ?
On Mar 26, 1:11 pm, Kam-Hung Soh <kamhung....@gmail.com> wrote:
> On Mar 26, 2:50 am, John Bartley K7AAY <john.bart...@gmail.com> wrote:
>
>
> 
> 
> 
> 
> 
> 
>
> When I tried your script on Windows XP using GNU Awk 3.1.3, I found
> this error:
>
> gawk: cmd. line:1: 'ORS=1?,:\t'
> gawk: cmd. line:1: ^ invalid char ''' in expression
>
> If that is the problem you encountered, you have to escape the double-
> quote mark in a double-quoted string using the "^" (double-quote caret
> double-quote) sequence.  (It's as awkward as it sounds.)
>
> Try:
>
> gawk "ORS=1?"^"","^"":"^""\t"^""" %1.tsv > %1.csv
>
> Regards,
>
> --
> Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</
> a>

Correction: use three double-quotes to escape a double-quote.

gawk "ORS=1?""",""":"""\t"""" %1.tsv > %1.csv

--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</
a>

Report this thread to moderator Post Follow-up to this message
Old Post
Kam-Hung Soh
03-26-08 08:58 AM


Re: GAWK error : replace newline with tab in WinXP ?
On Mar 25, 8:30=A0pm, Kam-Hung Soh <kamhung....@gmail.com> wrote:
> On Mar 26, 1:11 pm, Kam-Hung Soh <kamhung....@gmail.com> wrote:
>
>
> 
> 
> 
1line.txt 
te 
> 
- 
> 
> 
> 
 
 
 
 
> 
> 
> 
<snip>

> Correction: use three double-quotes to escape a double-quote.
>
> gawk "ORS=3D1?""",""":"""\t"""" %1.tsv > %1.csv
>
> --
> Kam-Hung Soh <a href=3D"http://kamhungsoh.com/blog">Software Salariman</a>=[/color
]


I appreciate the suggestion, but that led to a new problem, four gawk
error messages:

gawk: cmd. line:1: ORS=3D1?,:\t
gawk: cmd. line:1:             ^ parse error

(The caret lines up with the comma on the first line.)


gawk: cmd. line:1: ORD=3D1?,:\t
gawk: cmd. line:1:              ^ backslash not last character

(The caret lines up with the backstroke on the third
line)

I'll take another look at this later today.
Again, thank you; appreciate the effort.

Report this thread to moderator Post Follow-up to this message
Old Post
John Bartley K7AAY
03-27-08 12:03 AM


Re: GAWK error : replace newline with tab in WinXP ?
John Bartley K7AAY wrote:
> On Mar 25, 8:30 pm, Kam-Hung Soh <kamhung....@gmail.com> wrote: 
> <snip>
> 
>
> I appreciate the suggestion, but that led to a new problem, four gawk
> error messages:
>
> gawk: cmd. line:1: ORS=1?,:\t
> gawk: cmd. line:1:             ^ parse error
>
>                (The caret lines up with the comma on the first line.)
>
>
> gawk: cmd. line:1: ORD=1?,:\t
> gawk: cmd. line:1:              ^ backslash not last character
>
>                 (The caret lines up with the backstroke on the third
> line)
>
> I'll take another look at this later today.
> Again, thank you; appreciate the effort.

To avoid quoting problems alltogether, you should place the code into a
file, say: script.awk and run the command like this:

gawk -f script.awk infile > outfile

And if you really want to change 3 lines into 1 line with tab-delimited
fields, then this code will do it:

$ cat script.awk
ORS = NR%3 ? "\t" : "\n"
$ cat data
1
2
3
4
5
6
7
8
9
$ awk -f script.awk data
1       2       3
4       5       6
7       8       9

Report this thread to moderator Post Follow-up to this message
Old Post
Hermann Peifer
03-27-08 12:03 AM


Re: GAWK error : replace newline with tab in WinXP ?
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

Report this thread to moderator Post Follow-up to this message
Old Post
Harlan Grove
03-27-08 12:03 AM


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

"Harlan Grove" <hrlngrv@gmail.com> wrote in message
news:649fac5b-0093-46f3-928c-550ae7736268@b64g2000hsa.googlegroups.com...
> John Bartley K7AAY <john.bart...@gmail.com> wrote... 
>
> 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".
> 
> ...
>
> 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.
> 
>
> 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


Report this thread to moderator Post Follow-up to this message
Old Post
Rajan
03-27-08 03:07 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 03:45 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.