Newbie Parsing Question...STUCK 
Author Message
 Newbie Parsing Question...STUCK

Hello Group.

New to AWK, moderate Unix....

I have a file of data that is currently in the format

string1
string2
string3
string4
...
etc.

each value is on a separate line, I need to convert that information into a
single space delimited file.so it looks like

string1 string2 string3 string4

Since I am very new to awk and have the proverbal gun to my head, I thought
I would look to the group for some help..

Thanks to all in advance.

Gary



Sun, 26 Dec 2004 20:57:04 GMT  
 Newbie Parsing Question...STUCK

awk '{ printf("%s ",$0) }; END { printf("\n") }' infile > outfile

should do the job if you really meant to concatenate all the lines,
without limit. However, if you want not more than 10 strings per line,
try something like this (all one line):

awk '{ printf("%s ",$0); n++; if((n%10)==0) printf("\n") }; END {
printf("\n") }' infile > outfile

Since these are untested, use them carefully.

Quote:

> Hello Group.

> New to AWK, moderate Unix....

> I have a file of data that is currently in the format

> string1
> string2
> string3
> string4
> ...
> etc.

> each value is on a separate line, I need to convert that information into a
> single space delimited file.so it looks like

> string1 string2 string3 string4

> Since I am very new to awk and have the proverbal gun to my head, I thought
> I would look to the group for some help..

> Thanks to all in advance.

> Gary



Sun, 26 Dec 2004 21:37:36 GMT  
 Newbie Parsing Question...STUCK
On Wed, 10 Jul 2002 08:57:04 -0400,
Quote:

>Hello Group.

>New to AWK, moderate Unix....

>I have a file of data that is currently in the format

>string1
>string2
>string3
>string4
>...
>etc.

>each value is on a separate line, I need to convert that information into a
>single space delimited file.so it looks like

>string1 string2 string3 string4

tr '\n' ' ' < file > newfile


Sun, 26 Dec 2004 22:18:42 GMT  
 Newbie Parsing Question...STUCK

Quote:

>On Wed, 10 Jul 2002 08:57:04 -0400,

>>Hello Group.

>>New to AWK, moderate Unix....

>>I have a file of data that is currently in the format

>>string1
>>string2
>>string3
>>string4
>>...
>>etc.

>>each value is on a separate line, I need to convert that information into a
>>single space delimited file.so it looks like

>>string1 string2 string3 string4

>tr '\n' ' ' < file > newfile

Doesn't compile in this ng.  Sorry, Charlie...


Mon, 27 Dec 2004 02:34:31 GMT  
 Newbie Parsing Question...STUCK

Quote:


...
>>tr '\n' ' ' < file > newfile

>Doesn't compile in this ng.  Sorry, Charlie...

Quite right! Bill clearly erred by failing to mention awk 1 ORS = " " file >
newfile before mentioning the clearly superior tr command. One must always
provide an on-topic awk fig leaf before mentioning a better, non-awk
approach to solving any particular problem in order to minimize the number
of Kenny's responses.


Mon, 27 Dec 2004 04:08:35 GMT  
 Newbie Parsing Question...STUCK

Quote:



>...
>>>tr '\n' ' ' < file > newfile

>>Doesn't compile in this ng.  Sorry, Charlie...

>Quite right! Bill clearly erred by failing to mention awk 1 ORS = " " file >
>newfile before mentioning the clearly superior tr command. One must always
>provide an on-topic awk fig leaf before mentioning a better, non-awk
>approach to solving any particular problem in order to minimize the number
>of Kenny's responses.

Your point is well-taken, of course, but I always wonder about this for the
following reason:

        Imagine the kind of loopy, non-linear responses you would get for
        posting an AWK solution in a Perl NG.  Although I don't want to
        go down the road of debating this at too much length, the fact is
        that for most relatively simple things (e.g., those not involving
        networking - and select()) that are within AWK's natural solution
        space, the AWK solution will be shorter, more elegant, and easier
        to understand than the equivalent Perl solution.

        Yet, you will never get a Perl nut to admit this.

My point is that I am really satirizing the nuts in the Perl and C groups
when I post my little diatribes here.



Mon, 27 Dec 2004 05:13:28 GMT  
 Newbie Parsing Question...STUCK

Quote:


> >On Wed, 10 Jul 2002 08:57:04 -0400,

> >>Hello Group.

> >>New to AWK, moderate Unix....

> >>I have a file of data that is currently in the format

> >>string1
> >>string2
> >>string3
> >>string4
> >>...
> >>etc.

> >>each value is on a separate line, I need to convert that information
into a
> >>single space delimited file.so it looks like

> >>string1 string2 string3 string4

> >tr '\n' ' ' < file > newfile

> Doesn't compile in this ng.  Sorry, Charlie...

On the other hand, why promote UUOA?  (Or in your case, UUOT.)  Of course,
you could provide an answer that DOES compile, like these ;)

I like:
    BEGIN {ORS=" "} 1
myself, if you can stance an extra space at the end.  By the way, the "tr"
solution does this if the file ends with a newline (as many text files tend
to do).

If you can't, try
    BEGIN {ORS = ""} NR != 1 {$0 = " " $0} 1

'1' is a pattern that invokes the default action, '{print}'.  When no
arguments are passed to 'print', $0 is used as the default argument.
Therefore '1' is equivalent to '{print $0}'

    - Dan



Mon, 27 Dec 2004 09:51:40 GMT  
 Newbie Parsing Question...STUCK
...

Quote:
>If you can't, try
>    BEGIN {ORS = ""} NR != 1 {$0 = " " $0} 1

>'1' is a pattern that invokes the default action, '{print}'.  When no
>arguments are passed to 'print', $0 is used as the default argument.
>Therefore '1' is equivalent to '{print $0}'

I like pointless answers just as much as any regular here, but this does too
much work with the unnecessary pattern evaluation and concatenation.

BEGIN { ORS = ""; getline; print } { print "", $0 }



Mon, 27 Dec 2004 13:11:02 GMT  
 Newbie Parsing Question...STUCK

Quote:

> ...
> >If you can't, try
> >    BEGIN {ORS = ""} NR != 1 {$0 = " " $0} 1

> >'1' is a pattern that invokes the default action, '{print}'.  When no
> >arguments are passed to 'print', $0 is used as the default argument.
> >Therefore '1' is equivalent to '{print $0}'

> I like pointless answers just as much as any regular here, but this does
too
> much work with the unnecessary pattern evaluation and concatenation.

> BEGIN { ORS = ""; getline; print } { print "", $0 }

Harlan -

You answer is, of course, better.  I'm not surprised that when you put your
mind to it, you were able to out-do me with a correct solution.

Quote:
> I like pointless answers...

I am surprised by this remark.  You are not usually so condescending to
anyone but Kenny.  I didn't think my answer was pointless!

    (1) The OP did ask in an awk group, so my post answered his question,
where as most previous posters hadn't; they suggested non-awk alternatives
or criticized the solution space.

    (2) My code-only first solution is equivalent to your command-line
solution (I realize it was posted hours after, but I could not see your
solution at work, I had to wait until I got home.)  Command-line stuff
borders on being shell stuff (which in this case had already been
crititized), so I shy away from it sometimes so I can post just source code.

    (3) Your initial solution, and the other posted awk solution, always add
a space at the end of the string, which could be wrong (but perhaps
acceptable) in the case of a text file without an ending newline.  The OP
did ask for space-delimited, not space-separated or space-terminated,
output, and no one had yet addressed the ambiguity.

I hope that explains why I bothered posting.

    - Dan



Mon, 27 Dec 2004 21:35:17 GMT  
 Newbie Parsing Question...STUCK
look at
getline > varname

Quote:
> Hello Group.

> New to AWK, moderate Unix....

> I have a file of data that is currently in the format

> string1
> string2
> string3
> string4
> ...
> etc.

> each value is on a separate line, I need to convert that information into
a
> single space delimited file.so it looks like

> string1 string2 string3 string4

> Since I am very new to awk and have the proverbal gun to my head, I
thought
> I would look to the group for some help..

> Thanks to all in advance.

> Gary



Wed, 29 Dec 2004 10:39:05 GMT  
 Newbie Parsing Question...STUCK

Quote:
> look at
> getline > varname


> > Hello Group.

> > New to AWK, moderate Unix....

> > I have a file of data that is currently in the format

> > string1
> > string2
> > string3
> > string4
> > ...
> > etc.

> > each value is on a separate line, I need to convert that information
into
> a
> > single space delimited file.so it looks like

> > string1 string2 string3 string4

> > Since I am very new to awk and have the proverbal gun to my head, I
> thought
> > I would look to the group for some help..

> > Thanks to all in advance.

> > Gary

The correct syntax for getline is:

    getline varname < filename

NOT as above

HTH
--
Peter S Tillier
"Who needs perl when you can write dc and sokoban in sed?"
peter{dot}tillier<at>btinternet[dot]com
To reply direct to me please use the above address
not the "Reply To" which activates a spam trap.



Wed, 29 Dec 2004 11:01:59 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Newbie's question: PARSE

2. Rexx Newbie Parse Question

3. Newbie PARSE question

4. Newbie ARG (or PARSE) question

5. Newbie parsing question

6. Parsing Python question (newbie)

7. newbie question - parsing

8. python parsing question from a python newbie (though coding for 10 years)

9. newbie question - code in comments cause parsing failures ?

10. From Hickory Sticks to Joy Sticks

11. newbie stuck on generating lists of structures

12. Newbie stuck on recursion

 

 
Powered by phpBB® Forum Software