Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If a line has a length less than a number, append to its previous line

Tags:

bash

I have a file that looks like this:

ABCDEFGH
ABCDEFGH
ABC
ABCDEFGH
ABCDEFGH
ABCD
ABCDEFGH

Most of the lines have a fixed length of 8. But there are some lines in between that have a length less than 8. I need a simple line of code that appends each of those short lines to its previous line.

I have tried the following code but it takes lots of memory when working with large files.

cat FILENAME | awk 'BEGIN{OFS=FS="\t"}{print length($1), $1}' | tr 
'\n' '\t' | sed 's/8/\n/g' | awk 'BEGIN{OFS="";FS="\t"}{print $2, $4}'

The output I expect:

ABCDEFGH
ABCDEFGHABC
ABCDEFGH
ABCDEFGHABCD
ABCDEFGH
like image 299
grapefruit Avatar asked Feb 09 '19 02:02

grapefruit


1 Answers

If perl is your option, please try:

perl -0777 -pe 's/(\n)(.{1,7})$/\2/mg' filename
  • -0777 option tells perl to slurp all lines.
  • The pattern (\n)(.{1,7}) matches to a line with length less than 8, assigning \1 to a newline and \2 to the string.
  • The replacement \2 does not contain the preceding newline and is appended to the previous line.
like image 135
tshiono Avatar answered Sep 19 '22 12:09

tshiono