Output in raws, not columns

Output in raws, not columns

Post by contrace.. » Wed, 11 Jan 2006 23:36:41



Look this file:

Solaris> cat file

WNT001   23
WNT002   51
WNT003     8
WNT004   44
WNT005   29

Can you tell me how output this file and get this output ?

WNT001  23, WNT002  51, WNT003  8, WNT004  44, WNT005  29.

Thanks.

 
 
 

Output in raws, not columns

Post by Michael Heimin » Thu, 12 Jan 2006 00:25:26



Quote:> Look this file:
> Solaris> cat file
> WNT001   23
> WNT002   51
> WNT003     8
> WNT004   44
> WNT005   29
> Can you tell me how output this file and get this output ?
> WNT001  23, WNT002  51, WNT003  8, WNT004  44, WNT005  29.

        awk 'BEGIN{ORS=","}{print}' infile

Use /usr/xpg4/bin/awk on Solaris.

--
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)

#bofh excuse 30: positron router malfunction

 
 
 

Output in raws, not columns

Post by Ed Morto » Thu, 12 Jan 2006 00:56:55




>>Look this file:

>>Solaris> cat file

>>WNT001   23
>>WNT002   51
>>WNT003     8
>>WNT004   44
>>WNT005   29

>>Can you tell me how output this file and get this output ?

>>WNT001  23, WNT002  51, WNT003  8, WNT004  44, WNT005  29.

>    awk 'BEGIN{ORS=","}{print}' infile

or even:

        awk -v ORS="," 1 file

but that's not exactly correct as it'll stick a comma at the end of the
line rather than a period, it won't put a space after each comma, and it
won't append a newline at the end of the line. What you really need is this:

        awk '{out=out sep $0;sep=", "}END{print out "."}' file

or this:

        awk '{printf "%s%s",sep,$0;sep=", "}END{print "."}' file

Quote:> Use /usr/xpg4/bin/awk on Solaris.

Those last 2 solutions above will work in any awk.

        Ed.

 
 
 

Output in raws, not columns

Post by j.. » Thu, 12 Jan 2006 01:11:02



> Look this file:

> Solaris> cat file

> WNT001   23
> WNT002   51
> WNT003     8
> WNT004   44
> WNT005   29

> Can you tell me how output this file and get this output ?

> WNT001  23, WNT002  51, WNT003  8, WNT004  44, WNT005  29.

Another possibility:

while read a b;do
  [ -n "$a" -a -n "$b" ] && printf "%s %s, " $a $b
done < file
echo

Joe
--
Gort, klatu barada nikto

 
 
 

Output in raws, not columns

Post by Michael Heimin » Thu, 12 Jan 2006 01:14:28





>>>Look this file:

>>>Solaris> cat file

>>>WNT001   23
>>>WNT002   51
>>>WNT003     8
>>>WNT004   44
>>>WNT005   29

>>>Can you tell me how output this file and get this output ?

>>>WNT001  23, WNT002  51, WNT003  8, WNT004  44, WNT005  29.

>>       awk 'BEGIN{ORS=","}{print}' infile
> or even:
>        awk -v ORS="," 1 file
> but that's not exactly correct as it'll stick a comma at the end of the
> line rather than a period, it won't put a space after each comma, and it
> won't append a newline at the end of the line. What you really need is this:

Left this as exercise for the OP, but it was expected that
somebody would come around spoon-feeding him, so why bother with
details?

Quote:>        awk '{out=out sep $0;sep=", "}END{print out "."}' file
> or this:
>        awk '{printf "%s%s",sep,$0;sep=", "}END{print "."}' file
>> Use /usr/xpg4/bin/awk on Solaris.
> Those last 2 solutions above will work in any awk.

Gives ",." at the end?

--
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)

#bofh excuse 300: Digital Manipulator exceeding velocity
parameters

 
 
 

Output in raws, not columns

Post by Chris F.A. Johnso » Thu, 12 Jan 2006 01:44:23



> Look this file:

> Solaris> cat file

> WNT001   23
> WNT002   51
> WNT003     8
> WNT004   44
> WNT005   29

> Can you tell me how output this file and get this output ?

> WNT001  23, WNT002  51, WNT003  8, WNT004  44, WNT005  29.

la=
while read a b
do
  [ -n "$la" ] && printf "%s  %d, " "$la" "$lb"
  la=$a
  lb=$b
done < file
printf "%s  %d.\n" "$la" "$lb"

--
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |          is released under the
   2005, Apress                 |     GNU General Public Licence

 
 
 

Output in raws, not columns

Post by Ed Morto » Thu, 12 Jan 2006 01:49:22





<snip>
>>>      awk 'BEGIN{ORS=","}{print}' infile
<snip>
>>but that's not exactly correct as it'll stick a comma at the end of the
>>line rather than a period, it won't put a space after each comma, and it
>>won't append a newline at the end of the line. What you really need is this:

> Left this as exercise for the OP, but it was expected that
> somebody would come around spoon-feeding him, so why bother with
> details?

If it had been a detail, I wouldn't have bothered, but it's actually a
completely different solution. It would be difficult for the OP to build
on what was originally posted to come up with the output he wanted as
he'd have had to figure out how to set ORS differently for the final
record than for all the others or buffer records and print the
preceeding one when processing the current one or do something else
needlesly complex.

<snip>

Quote:>>       awk '{printf "%s%s",sep,$0;sep=", "}END{print "."}' file
<snip>
> Gives ",." at the end?

No.

        Ed.

 
 
 

Output in raws, not columns

Post by j.. » Thu, 12 Jan 2006 02:26:58




> > Look this file:

> > Solaris> cat file

> > WNT001   23
> > WNT002   51
> > WNT003     8
> > WNT004   44
> > WNT005   29

> > Can you tell me how output this file and get this output ?

> > WNT001  23, WNT002  51, WNT003  8, WNT004  44, WNT005  29.

> la=
> while read a b
> do
>   [ -n "$la" ] && printf "%s  %d, " "$la" "$lb"
>   la=$a
>   lb=$b
> done < file
> printf "%s  %d.\n" "$la" "$lb"

If there's a blank line at the end of the file this will have a
trailing 0 in the output.

Joe
--
Gort, klatu barada nikto

 
 
 

Output in raws, not columns

Post by Stephane Chazela » Thu, 12 Jan 2006 02:46:23



Quote:> Look this file:

> Solaris> cat file

> WNT001   23
> WNT002   51
> WNT003     8
> WNT004   44
> WNT005   29

> Can you tell me how output this file and get this output ?

> WNT001  23, WNT002  51, WNT003  8, WNT004  44, WNT005  29.

[...]

awk '
   NF != 2 {next}
   { s = $1 "  " $2 }
   more == 1 { s = ", " s }
   { printf "%s", s; more = 1 }
   END { if (more) print "." }' file

--
Stephane

 
 
 

Output in raws, not columns

Post by Michael Heimin » Thu, 12 Jan 2006 03:19:18



[stuff]

Quote:>>>       awk '{printf "%s%s",sep,$0;sep=", "}END{print "."}' file
> <snip>
>> Gives ",." at the end?

> No.

With gawk it works, but got ",." at the end on solaris with the
awk in $PATH.

--
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)

#bofh excuse 140: LBNC (luser brain not connected)

 
 
 

Output in raws, not columns

Post by Ed Morto » Thu, 12 Jan 2006 04:51:15






> [stuff]

>>>>      awk '{printf "%s%s",sep,$0;sep=", "}END{print "."}' file

>><snip>

>>>Gives ",." at the end?

>>No.

> With gawk it works, but got ",." at the end on solaris with the
> awk in $PATH.

I'm sorry, but that's just not possible unless your awk is severely
broken (and I don't mean in the normal way that old awk is broken) or
you have an extra blank line at the end of your input file (but then
gawk would've produced the same error) or some wierd control characters.

This is on Solaris:

$ uname -a
SunOS ... 5.8 Generic_108528-29 sun4u sparc SUNW,Ultra-Enterprise
$ cat file
WNT001   23
WNT002   51
WNT003     8
WNT004   44
WNT005   29
$ gawk '{printf "%s%s",sep,$0;sep=", "}END{print "."}' file
WNT001   23, WNT002   51, WNT003     8, WNT004   44, WNT005   29.
$ nawk '{printf "%s%s",sep,$0;sep=", "}END{print "."}' file
WNT001   23, WNT002   51, WNT003     8, WNT004   44, WNT005   29.
$ /usr/xpg4/bin/awk '{printf "%s%s",sep,$0;sep=", "}END{print "."}' file
WNT001   23, WNT002   51, WNT003     8, WNT004   44, WNT005   29.
$ /usr/bin/awk '{printf "%s%s",sep,$0;sep=", "}END{print "."}' file
WNT001   23, WNT002   51, WNT003     8, WNT004   44, WNT005   29.

Regards,

        Ed.

 
 
 

Output in raws, not columns

Post by Michael Heimin » Thu, 12 Jan 2006 05:20:22







>> [stuff]

>>>>>      awk '{printf "%s%s",sep,$0;sep=", "}END{print "."}' file

>>><snip>

>>>>Gives ",." at the end?

>>>No.

>> With gawk it works, but got ",." at the end on solaris with the
>> awk in $PATH.

> I'm sorry, but that's just not possible unless your awk is severely
> broken (and I don't mean in the normal way that old awk is broken) or
> you have an extra blank line at the end of your input file (but then
> gawk would've produced the same error) or some wierd control characters.

Might well be, run on different systems.

Quote:> $ uname -a
> SunOS ... 5.8 Generic_108528-29 sun4u sparc SUNW,Ultra-Enterprise

Used 5.10.

[..]

--
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)

#bofh excuse 17: fat electrons in the lines

 
 
 

Output in raws, not columns

Post by Xichen » Thu, 12 Jan 2006 07:41:41


If your file is not a very big one, you can try the following perl
one-liner:


Xicheng


> Look this file:

> Solaris> cat file

> WNT001   23
> WNT002   51
> WNT003     8
> WNT004   44
> WNT005   29

> Can you tell me how output this file and get this output ?

> WNT001  23, WNT002  51, WNT003  8, WNT004  44, WNT005  29.

> Thanks.

 
 
 

1. 4.3.2 Man command outputs 81 column output?

When I do a "man <something>", the output sometimes has 81 columns of
output, putting the last character of the last word on a line by
itself. Anyone seen this and figured out how to fix it?

Please reply to newsgroup. My address is forwarded to /dev/null.
--

----------------------------------
AOS/VS, AIX, Win95, FreeBSD, WinNT:
I'm caught in a twisty little maze of operating systems, all different.

2. netscape problem

3. Plug-ins not not seen by Gimp

4. NEC 4x4 supported?

5. meaning of the second column of output in ll?

6. ALR / Gateway 9200 info?

7. Output scroll out in two columns?

8. SOMEONE SHOOT IT! <G> J/k

9. inod-sz column in sar -v output.

10. How to separate adjacent columns in ps(1b) output?

11. Separate adjacent columns in ps(1b) output

12. what algorothm does ls use for sizing output row/column format?