Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not sure what I am supposed to use to do this?

Status
Not open for further replies.

milage

Programmer
Jul 13, 2001
58
US
Hi,

Say I have a string in my shell script and I want to do a find and replace on it what I am supposed to use and how? I have tried using sed (but then realised this was just for files?) and am now a bit stumped.

All I want to do is to find all instances of "^m" within a string and replace them with " ".

How do you do you this?

Cheers

Rob
 
try using dos2unix command. EDC
--------------------------------------
This is Unix-Land. In quiet nights, you can hear the Windows machines
reboot.
 
Or try this:
sed 's/'`echo "\015"`'//g' yourfile > newfile
HTH ;-) Dickie Bird
Honi soit qui mal y pense
 
Since you're not using files, DickieBird's example should be
Code:
echo $string | sed 's`echo "\015"`'//g'
//Daniel
 
There are many ways to skin a cat, here are a few:

Cleaning ^M's from DOS files

Example file with ^M's

# vi /tmp/hosts.dos

194.19.11.10 bill bill.foo.com^M
10.19.11.203 sorry sorry.foo.com^M
10.19.11.161 happy happy.foo.com^M
happy happy.foo.com^M

There are a couple of ways that the Ctrl-M ( ^M) can be stripped out of the
file. The first is the tr command, which is used to translate characters. It
is possible to tell tr to delete all Ctrl-M characters.

# tr -d &quot;\015&quot; < /tmp/hosts.dos > /tmp/hosts.unix
In this tr command, you delete ( -d) all occurrences of the \015 or Ctrl-M
character from the file /tmp/hosts.dos and rewrite the output to
/tmp/hosts.unix file. Another way to strip the Ctrl-M character is to pass
the file through sed and have it perform a substitution:

# sed 's/^V^M//g' /tmp/hosts.dos > /tmp/hosts.unix

In this version, sed processes the file /tmp/hosts.dos searching for all
occurrences of the Ctrl-M character. When it finds one, it performs a
substitution of the character. In this case, you can swap the Ctrl-M with
null and output the results into the /tmp/hosts.unix file.

The sed command can also be used from within the vi editor. The vi editor
has two modes: the insert mode and the command mode. From within the command
mode, sed can be executed to perform the substitution.

vi /tmp/hosts.dos

When inside the vi editor, make sure you are in the command mode by pressing
the Esc key. Pressing the colon ( :) key allows you to input the sed
command.

:%s/^V^M//g
This command behaves the same as using the sed command from a UNIX shell. It
searches for all occurrences of the Ctrl-M character. When it finds one, it
substitutes the character with nothing. You can then continue working in the
file if you have more changes to make.

Good Luck
Laurie.
 
Ok, I've tried this:

echo $string | sed 's`echo &quot;^M&quot;`'//g'

and it errored with and EOF

so I got rid of one of the '

echo $string | sed 's`echo &quot;^M&quot;`//g'

and it doesn't error anymore.

However, rather than echoing it out to the screen I need to put it into another variable. How do I do this?

Cheers for the help



 
new_var=`echo $var | sed -e &quot;s/\¨/ /g`

sed command :
-e as editing
s as subttitude
\¨ : the slash indicate that don't interpretate ¨
/ / : second patern
g as global if the patern appears more than one time

make shure than you using good executing kote ( alt gr ` spacebar )
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top