0

Trying to combine multiple lines if it doesn't have an end of message string. Would also like to remove the prefix on all but the first line.

Input:

b929e3e1-29db this is a long message that
abc074df-48a1 is continued onto multiple
dfd4c683-ab48 lines#EOM
ff513a72-570d this is a short message#EOM
980d10aa-bbed another short message#EOM

Output:

b929e3e1-29db this is a long message that is continued onto multiple lines
ff513a72-570d this is a short message
980d10aa-bbed another short message

Ideally using common tools available via command line. (ex: awk, sed)

  • 2
    What did you try? – Satō Katsura Sep 11 '16 at 16:33
  • Perl script treating "#EOM" as End-of-Paragraph, springs to mind. – waltinator Sep 11 '16 at 17:17
1

With GNU awk1

gawk -vRS='#EOM\n' '{gsub(/\n[[:xdigit:]-]+/,"");} {$1=$1} 1' file.txt
  • set the record separator to #EOM followed by a newline
  • remove sequences of hex digits and hyphens preceded by newlines; this retains the initial sequence of each record since its newline was eaten by the previous RS
  • re-assign a field $1=$1 to force the record to be re-evaluated with default output separators
  • print the record (because 1 is true)

Testing

$ gawk -vRS='#EOM\n' '{gsub(/\n[[:xdigit:]-]+/,"");} {$1=$1} 1' file.txt
b929e3e1-29db this is a long message that is continued onto multiple lines
ff513a72-570d this is a short message
980d10aa-bbed another short message


1. it doesn't appear to work with mawk, for reasons I don't understand (even after replacing + by \{1,\} and various other tweaks)

  • It seems to work fine here with mawk 1.3.4 20141206. FWIW. – Satō Katsura Sep 11 '16 at 17:55
  • @SatoKatsura thanks - seems mine is 1.3.3 – steeldriver Sep 11 '16 at 17:57
  • It's the [:xdigit:] that doesn't work, [a-f0-9-]+ works with mawk 1.3.3 Nov 1996 (rather hideously old, that). – ilkkachu Sep 11 '16 at 20:07

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.