Question

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?

Was it helpful?

Solution

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

OTHER TIPS

perl -pe 'chomp if (++$i % 4);'

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top