1

I'm currently writing a bash script for parsing a simple text file. I now have the problem that there are a few occurences of new lines, where none should be. Let me give you an example

Q1bcd    3 - 4 ---   ---  ---     Ba     Klasse hat Aufgab..
8 a     3    ---        ---     Mr
6 b      3 - 4 ---   ---  ---     Bu     Unterricht im
Klassenverband
8 a     4    ---        ---     Ke
7 a      4    De   Bi   r24    La

The 'Klassenverband' string is in a new line. I don't want that. To remove the new line character I tried different commands with sed and awk:

awk '/^\([a-zA-Z]\{4\}\)/{printf "%s ",$0;next} 1' file
sed '/^\([a-zA-Z]\{4\}\)/{N;s/\n//;}' file

What I've come up with so far, is that all the neccecary strings can be extracted with the following Regex

^\([a-zA-Z]\{4\}\)

I am expecting an output like this

Q1bcd    3 - 4 ---   ---  ---     Ba     Klasse hat Aufgab..
8 a     3    ---        ---     Mr
6 b      3 - 4 ---   ---  ---     Bu     Unterricht im Klassenverband
8 a     4    ---        ---     Ke
7 a      4    De   Bi   r24    La

Edit: This worked for me. But the text file needs to be a unix text file no DOS file

sed '$!N; s/\n\([[:alpha:]]\{4,\}\)/ \1/; P;D' file
  • can you add expected output? – Sundeep Sep 16 '16 at 14:49
1

How about

sed '$!N; s/\n\([[:alpha:]]\{4,\}\)/ \1/; P;D' file
Q1bcd    3 - 4 ---   ---  ---     Ba     Klasse hat Aufgab..
8 a     3    ---        ---     Mr
6 b      3 - 4 ---   ---  ---     Bu     Unterricht im Klassenverband
8 a     4    ---        ---     Ke
7 a      4    De   Bi   r24    La
1

With perl, assuming the input file is small enough to fit in memory:

perl -0777 -pe 's/\n([a-zA-Z]{4})/ $1/g' file.txt
0

This is clumsy; there may be a better way

awk '{ c=""; if (NR!=1 && !/^[a-zA-Z]{4}/) { c="\n" }; printf("%s%s",c,$0) } END { print "" }' 

Basically, for each line we print it out without the LF character. Then we can prefix the next line with LF, if we don't want to merge this line.

Finally this will mean the last line is miss the final LF, so we add that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.