Unix Shell - Output in raws, not columns

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2006 > Output in raws, not columns





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Output in raws, not columns
contracer11@gmail.com

2006-01-13, 10:39 pm

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.

Michael Heiming

2006-01-13, 10:39 pm

In comp.unix.shell contracer11@gmail.com:
> 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)
mail: echo zvpunry@urvzvat.qr | PERL -pe 'y/a-z/n-za-m/'
#bofh excuse 30: positron router malfunction
Ed Morton

2006-01-13, 10:40 pm



Michael Heiming wrote:
> In comp.unix.shell contracer11@gmail.com:
>
>
>
>
>
>
>
>
>
>
>
>
> 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

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


Those last 2 solutions above will work in any awk.

Ed.
joe@invalid.address

2006-01-13, 10:40 pm

contracer11@gmail.com writes:

> 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
Michael Heiming

2006-01-13, 10:40 pm

In comp.unix.shell Ed Morton <morton@lsupcaemnt.com>:


> Michael Heiming wrote:
[vbcol=seagreen]
> 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?

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


> or this:


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


[vbcol=seagreen]
> Those last 2 solutions above will work in any awk.


Gives ",." at the end?

--
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)
mail: echo zvpunry@urvzvat.qr | PERL -pe 'y/a-z/n-za-m/'
#bofh excuse 300: Digital Manipulator exceeding velocity
parameters
Chris F.A. Johnson

2006-01-13, 10:40 pm

On 2006-01-10, contracer11@gmail.com wrote:
> 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
Ed Morton

2006-01-13, 10:40 pm



Michael Heiming wrote:
> In comp.unix.shell Ed Morton <morton@lsupcaemnt.com>:
>
>
>
<snip>[vbcol=seagreen]
<snip>[vbcol=seagreen]
>
>
> 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>
<snip>[vbcol=seagreen]
> Gives ",." at the end?
>


No.

Ed.
joe@invalid.address

2006-01-13, 10:40 pm

"Chris F.A. Johnson" <cfajohnson@gmail.com> writes:

> On 2006-01-10, contracer11@gmail.com wrote:
>
> 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
Stephane Chazelas

2006-01-13, 10:40 pm

On 10 Jan 2006 06:36:41 -0800, contracer11@gmail.com wrote:
> 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
Michael Heiming

2006-01-13, 10:40 pm

In comp.unix.shell Ed Morton <morton@lsupcaemnt.com>:
> Michael Heiming wrote:
[stuff]
[vbcol=seagreen]
> <snip>
[vbcol=seagreen]
> 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)
mail: echo zvpunry@urvzvat.qr | PERL -pe 'y/a-z/n-za-m/'
#bofh excuse 140: LBNC (luser brain not connected)
Ed Morton

2006-01-13, 10:40 pm



Michael Heiming wrote:
> In comp.unix.shell Ed Morton <morton@lsupcaemnt.com>:
>
>
> [stuff]
>
>
>
>
>
>
> 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.
Michael Heiming

2006-01-13, 10:40 pm

In comp.unix.shell Ed Morton <morton@lsupcaemnt.com>:


> Michael Heiming wrote:
[vbcol=seagreen]
> 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.

> $ 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)
mail: echo zvpunry@urvzvat.qr | PERL -pe 'y/a-z/n-za-m/'
#bofh excuse 17: fat electrons in the lines
Xicheng

2006-01-13, 10:40 pm

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

perl -le 'chomp(@a=<> );$"=", ";print"@a."' file

Xicheng

contracer11@gmail.com wrote:
> 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.


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com