-   Linux - Software (
-   -   SED - How to replace a control character (NAK) from a text file (

paulr211 10-02-2012 11:15 AM

SED - How to replace a control character (NAK) from a text file
I have a text (EDI) file which uses the NAK (hex 15) as a record separator. I need to replace it with a newline so I can further process the file.
I searched for a solution with Google. The echo in the command below is supposed to handle the control character but I get the unterminated command error. Putting single quotes around the substitute gives an unknown command error.

How can I get this to work?



sed s/'`echo "\025"`'/\nl/g < 1004596447.edi >
sed: -e expression #1, char 8: unterminated `s' command

SecretCode 10-02-2012 11:45 AM

Instead of

sed s/'`echo "\025"`'/\nl/g  < 1004596447.edi > junkfile
1. you would need -e in the echo command to get escape sequences processed
2. in your sed command put the quotes around the whole command not just one bit
3. and you would need double quotes if you want backticks to take effect inside them

sed "s/`echo -e "\025"`/\nl/g"  < 1004596447.edi > junkfile
4. but most of all, you don't need that use of echo; regular expressions allow you to match hex characters directly

sed "s/\x15/\nl/g"  < 1004596447.edi > junkfile
5. you probably meant just \n not \nl
6. you don't need to redirect input - sed takes an input file name

sed "s/\x15/\n/g" 1004596447.edi > junkfile

paulr211 10-02-2012 03:15 PM


Thanks for your help. Greatly appreciated.


David the H. 10-04-2012 10:14 AM

sed is a bit overweight for this function. It's usually recommended to use tr for one-to-one character translation.


tr '\025' '\n' <infile >outfile
And please use ***[code][/code] tags*** around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

Edit: BTW, bash and other advanced shells have a relatively new "ansi-c" form of quoting: $'..', that expands backslashed characters in a way similar to echo -e. So you could also do this in sed:


sed $'s/\x15/\n/g' <infile >outfile

All times are GMT -5. The time now is 01:21 PM.