For Programmers: Free Programming Magazines  


Home > Archive > AWK > May 2006 > Joining Lines









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 Joining Lines
jefferson.cliff@gmail.com

2006-05-30, 6:57 pm

Is there a way with awk to join multiple lines in a file. I have a file
with several thousand lines. Each line has to be tied to the line
under:

Top
Bottom
Left
Right

I need to take all the lines in this file and join the first and
second: third and forth: etc. Can that be done in awk?

Thanks
Cliff

Chris F.A. Johnson

2006-05-30, 6:57 pm

On 2006-05-30, jefferson.cliff@gmail.com wrote:
> Is there a way with awk to join multiple lines in a file. I have a file
> with several thousand lines. Each line has to be tied to the line
> under:
>
> Top
> Bottom
> Left
> Right
>
> I need to take all the lines in this file and join the first and
> second: third and forth: etc. Can that be done in awk?


awk ' NR % 2 { printf "%s", $0; next }
{ print }'

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
jefferson.cliff@gmail.com

2006-05-30, 6:57 pm

Thanks for the quick reply, but I am getting a syntax error. I am kind
of new to using awk :
cat cal1.attr | awk ' NR % 2 { printf "%s", $0; next } {print }'
awk: syntax error near line 1
awk: bailing out near line 1
cat: write error: Broken pipe

Where did I go wrong?

Jürgen Kahrs

2006-05-30, 6:57 pm

jefferson.cliff@gmail.com wrote:
> Thanks for the quick reply, but I am getting a syntax error. I am kind
> of new to using awk :
> cat cal1.attr | awk ' NR % 2 { printf "%s", $0; next } {print }'
> awk: syntax error near line 1
> awk: bailing out near line 1
> cat: write error: Broken pipe
>
> Where did I go wrong?


You use nawk on Solaris. Use gawk or mawk or xpg awk.

gawk ' NR % 2 { printf "%s", $0; next } {print }' call.attr
jefferson.cliff@gmail.com

2006-05-30, 6:57 pm

Thanks I am still trying to learn awk. This command worked great. I
really appreciate the quick response and great answer.
Cliff

Kenny McCormack

2006-05-30, 6:57 pm

In article <1149016889.850225.11120@y43g2000cwc.googlegroups.com>,
jefferson.cliff@gmail.com <jefferson.cliff@gmail.com> wrote:
>Thanks I am still trying to learn awk. This command worked great. I
>really appreciate the quick response and great answer.
>Cliff
>


You might want to try the cannonical (sp?) answer:

ORS=FNR%2?"":"\n"

Yes, that's the whole program. Hope I got the details right!

Juergen Kahrs

2006-05-31, 3:57 am

Kenny McCormack wrote:

>
>
> You might want to try the cannonical (sp?) answer:
>
> ORS=FNR%2?"":"\n"
>
> Yes, that's the whole program. Hope I got the details right!


It is always impressive to see how short AWK
scripts can be. But he is new to AWK and I
think we should first help him on basics of
usage.
William James

2006-05-31, 6:57 pm

Juergen Kahrs wrote:
> Kenny McCormack wrote:
>
>
> It is always impressive to see how short AWK
> scripts can be. But he is new to AWK and I
> think we should first help him on basics of
> usage.


Input:

Top
Bottom
Left
Right

Output:

Bottom
Right

Try this:

gawk '{ORS=ORS?"":"\n"}8' junk

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com