LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-12-2008, 12:01 PM   #1
wtaicken
LQ Newbie
 
Registered: Dec 2008
Location: Dorset, UK
Distribution: Ubuntu 7.1
Posts: 25

Rep: Reputation: 15
Problems with a substitution using sed


I'm trying to replace the character x in the following text with another character determined as output from an awk script. I'm outputting the character to stdout to verify it is ok. However, although one substitution is ok, the other isn't.

The original text is:

INTER x 12

ARCHIVE x

The character to substitute for x in this case is e.g. 2. But when I do this via sed, I get

12ER 2

ARCHIVE 2

Could the INT in the first line be messing things up?
My sed command line is
Code:
sed -i "s/x/$token/g"
where $token is output from awk

Seems weird!!
 
Old 12-12-2008, 12:13 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by wtaicken View Post
Could the INT in the first line be messing things up?
I don't think so. Maybe there is some weird character in the token variable. Can you post the output of the following command from the shell or script where the token variable is assigned?
Code:
echo $token | od -c
 
Old 12-12-2008, 04:54 PM   #3
wtaicken
LQ Newbie
 
Registered: Dec 2008
Location: Dorset, UK
Distribution: Ubuntu 7.1
Posts: 25

Original Poster
Rep: Reputation: 15
Ok, when it works ok, & token is reported as 2, output is given as

0000000 2 \n
0000002

When it doesn't work, for token as 2, output is

0000000 2 \r \n
0000003

Does that help?
 
Old 12-12-2008, 05:35 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Yes. The \r \n sequence is the carriage return typical of text files written in Windows. You stated that token is determined as output from an awk script. Does the file processed from awk come from a Windows system? If this is the case, just run
Code:
dos2unix file
before processing the file with awk. If it has carriage returns, they will be stripped out, otherwise the file will be left untouched. See man dos2unix for details.

As alternative you can remove the \r from within the awk program using the sub function. For example:
Code:
sub("\r","",target)
where target can be a variable or a field like $NF.

Edit: for a detailed explanation of the sequence of characters at the end of a line of text, see the Wikipedia at the voice "Newline" and check the "Common problems" section.

Last edited by colucix; 12-12-2008 at 06:01 PM.
 
Old 12-15-2008, 04:04 AM   #5
wtaicken
LQ Newbie
 
Registered: Dec 2008
Location: Dorset, UK
Distribution: Ubuntu 7.1
Posts: 25

Original Poster
Rep: Reputation: 15
Ok, thanks that seems to work
 
  


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
variable substitution in sed gaynut Programming 1 07-14-2008 07:38 AM
simple substitution with sed? ocicat Programming 9 02-22-2008 11:45 PM
sed substitution with p flag 7stud Linux - Newbie 2 03-03-2007 04:15 AM
Command substitution and sed daYz Linux - General 9 11-04-2006 01:15 AM
sed substitution error BlkPoohba Programming 1 08-25-2004 02:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:25 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration