I need to have a RegEx that finds a \n in the middle of a line as a start point, anything before is random, and replace after 15 digits and 49 alpha on the second line. I need to replace all that by blanks, but the second line needs to join with the first one.

Attempt

sed -r -e '{N;s/\n[[:digit:]]{15}[[:space:]]{49}//}'

Input

QC HOH 0H0 CA
:70:NOFX TRADE TR
100000100200621                                                 ADE RELATED WOOD                                                                                         PURCHASE

Screen shot of record

What needs to be removed is the linefeed after TRADE TR and bring the ADE RELATED to the TR so it spells TRADE.

Desired Output

QC H0H 0H0 CA
:70:NOFX TRADE TRADE RELATED WOOD                                                                                         PURCHASE

2 Answers

2
potong On Best Solutions

This might work for you (GNU sed):

sed -E 'N;s/\n[[:digit:]]{15}[[:space:]]{49}//;P;D' file

This opens up a two line window and amends the second of them if the substitute command matches. It always prints the first of the two lines and then removes it.

1
Ed Morton On

With GNU sed:

$ sed -Ez 's/\n[[:digit:]]{15}[[:space:]]{49}//' file
QC J0B 2Y0 CA
:70:NOFX TRADE TRADE RELATED WOOD                                                                                         PURCHASE