LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 10-02-2012, 11:15 AM   #1
paulr211
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?

Thanks,

Paul


sed s/'`echo "\025"`'/\nl/g < 1004596447.edi >
junkfile
sed: -e expression #1, char 8: unterminated `s' command
 
Old 10-02-2012, 11:45 AM   #2
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
Instead of
Code:
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
Code:
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
Code:
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
Code:
sed "s/\x15/\n/g" 1004596447.edi > junkfile

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

Original Poster
Rep: Reputation: Disabled
Everyone,

Thanks for your help. Greatly appreciated.

Paul
 
Old 10-04-2012, 10: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: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
sed is a bit overweight for this function. It's usually recommended to use tr for one-to-one character translation.

Code:
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:

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

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


Reply


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 08: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 12:04 AM
sed to change character in text file only once. pobrika Programming 7 09-09-2009 07:43 AM
using sed to replace text on one line in a text file vo1pwf Linux - Newbie 5 06-24-2009 07:54 AM
SED - replace text in file on specific line 3saul Linux - Software 1 03-04-2006 07:01 PM


All times are GMT -5. The time now is 08:45 AM.

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