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?
From stackoverflow
-
perl -pe 'chomp if (++$i % 4);'Lars Wirzenius : This is beautifully short. It seems to lack the final newline in the output, though.kmkaplan : That must be because the number of lines in your file is not a multiple of four. -
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 ABCDEFYou want output like this:
abcdefghijklmnopqrstuvwx yz0123456789ABCDEFThe 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 -
An easier way to do it with awk would be:
awk '{ printf $0 } (NR % 4 == 0) { print }' filenameAlthough 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 }' filenamedmckee : 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.David Dean : I've added a more complicated example that fixes that problem. Still relatively simple.
0 comments:
Post a Comment