LinuxAnswers - the LQ Linux tutorial section.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 10-02-2012, 12:15 PM   #1
LQ Newbie
Registered: Oct 2012
Posts: 2

Rep: Reputation: Disabled
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
Old 10-02-2012, 12:45 PM   #2
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
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

Last edited by SecretCode; 10-02-2012 at 12:46 PM.
Old 10-02-2012, 04:15 PM   #3
LQ Newbie
Registered: Oct 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled

Thanks for your help. Greatly appreciated.

Old 10-04-2012, 11:14 AM   #4
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954
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

Last edited by David the H.; 10-04-2012 at 11:20 AM. Reason: changed hex to octal


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
sed command to replace line in text file gengwei89 Linux - Newbie 6 11-06-2011 09:10 AM
how do you replace text between two words in a whole file not just 1 line w/ sed/awk lityit Programming 5 11-04-2011 01:04 AM
sed to change character in text file only once. pobrika Programming 7 09-09-2009 08:43 AM
using sed to replace text on one line in a text file vo1pwf Linux - Newbie 5 06-24-2009 08:54 AM
SED - replace text in file on specific line 3saul Linux - Software 1 03-04-2006 08:01 PM

All times are GMT -5. The time now is 06:55 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration