views:

133

answers:

3

I have a huge textfile, approx 400.000 lines 80 charachters wide on liux.

Need to "unfold" the file, merging four lines into one ending up having 1/4 of the lines, each line 80*4 charachters long.

any suggestions?

+10  A: 
perl -pe 'chomp if (++$i % 4);'
kmkaplan
This is beautifully short. It seems to lack the final newline in the output, though.
Lars Wirzenius
That must be because the number of lines in your file is not a multiple of four.
kmkaplan
+2  A: 

I hope I understood your question correctly. You have an input line like this (except your lines are longer):

abcdef
ghijkl
mnopqr
stuvwx
yz0123
456789
ABCDEF

You want output like this:

abcdefghijklmnopqrstuvwx
yz0123456789ABCDEF

The following awk program should do it:

{ line = line $0 }
(NR % 4) == 0 { print line; line = "" }
END { if (line != "") print line }

Run it like this:

awk -f merge.awk data.txt
Lars Wirzenius
+2  A: 

An easier way to do it with awk would be:

awk '{ printf $0 } (NR % 4 == 0) { print }' filename

Although if you wanted to protect against ending up without a trailing newline it gets a little more complicated:

awk '{ printf $0 } (NR % 4 == 0) { print } END { if (NR % 4 != 0) print }' filename
David Dean
Cute, though I suspect shares the "sometime lacks a trailing newline" problem with the perl solution suggested by kmkaplan. None-the-less, props for clarity and terseness.
dmckee
I've added a more complicated example that fixes that problem. Still relatively simple.
David Dean