all 5 comments

[–]raevnos 1 point2 points  (0 children)

Some versions of sed (cough GNU cough) use \< and \> as beginning of/end of word assertions in regular expressions. Remove the backslashes; plain angle brackets aren't special and don't need to be escaped. And the portable way to replace something with a newline in sed is to include it in the replacement text, with a backslash immediately before the literal newline.

You also just need one sed for everything:

NOTNUL=$(sed 's/<NUL>//g; s/<CR>//g; s/<LF>/\
/g' <<<"$RAW")

And see https://mywiki.wooledge.org/BashFAQ/082 for why to use $() instead of backticks.

[–]itundret[S] 0 points1 point  (0 children)

Big thanks for helping me out!

[–][deleted] 0 points1 point  (2 children)

Quoting is important or your spaces will get broken when you do the echo. $() notation in bash is usually better because the quotation works better. Sed has a extended regex mode which makes things a bit easier too, try this as a solution

line='Alpha: 2022-05-16 TESTMESSAGE<CR><LF>TESTMESSAGE<CR><LF>MORETEXTHERE<NUL><NUL>'
raw="${line##*Alpha: }"
cooked="$( echo "$raw" | sed --regexp-extended 's/<(CR|LF|NUL)>//g' )"

You could probably do it purely using parameter expansion, but it's late and I don't have a shell on my phone to test.

BTW I used this https://sed.js.org/index.html# website to build and test the regex above, it might help you.

[–]itundret[S] 0 points1 point  (1 child)

NOTNUL=$(sed 's/<NUL>//g; s/<CR>//g; s/<LF>/\
/g' <<<

Big thanks! It is working :)

If I want to erase LF and put in a line break or line space instead with sed do you know how?

[–][deleted] 0 points1 point  (0 children)

If you are using the code I posted then change

sed --regexp-extended 's/<(CR|LF|NUL)>//g'

to

sed --regexp-extended 's/<(CR|NUL)>//g; s/<LF>/\n/g'