Operating System - HP-UX
1831658 Members
2197 Online
110029 Solutions
New Discussion

Search and replace Text in a File

 
SOLVED
Go to solution
Robjan
Frequent Advisor

Search and replace Text in a File

Hi ,

I want a replace a text

like "/asia/Calcutta" to "/india" in a

file using some unix commands . Any

suggestions would be helpful .

--Rob

9 REPLIES 9
Steven Schweda
Honored Contributor

Re: Search and replace Text in a File

"man sed"

A Forum search for "sed" should find many
examples.
Ninad_1
Honored Contributor
Solution

Re: Search and replace Text in a File

Hi,

sed 's/\/asia\/Calcutta/\/India/g'

would be one approach.

Regards,
Ninad
Bill Hassell
Honored Contributor

Re: Search and replace Text in a File

sed can be quite intimidating to use, especially if you aren't familiar with vi. The attached script chgafile will replace one string with another. The default is to show before and after lines (with the string highlighted) but make no changes. Run it with no options to see all the options documented.


Bill Hassell, sysadmin
Frank de Vries
Respected Contributor

Re: Search and replace Text in a File

You can use ! instead of slashes to avoid
having to quote directory slashes

sed 's!/asia/Calcutta/!/India/g'

also you can put the various sed commands
in a seperate text file

's!/asia/Calcutta/!/India/g'
's!/asia/Bomnay/!/India/g'
's!/asia/Delhi/!/India/g'

sed -f script_file

-f script_file Take script from file script_file.
Look before you leap
AwadheshPandey
Honored Contributor

Re: Search and replace Text in a File

u can use vi editor for this

vi file name
in cmd mode

If you want to replace asia only
:%s/asia/india/g
In case you want to replace /asia/Calcutta with /india then use
:%s/asia\/Calcutta/india/g
It's kind of fun to do the impossible
Robjan
Frequent Advisor

Re: Search and replace Text in a File

Hi,
All the suggestion posted by you doesnt help . For everything I am getting line cannot be parsed message .
if there is any other way please help me out .
--Rob
Steven Schweda
Honored Contributor

Re: Search and replace Text in a File

> All the suggestion posted by you doesnt
> help .

You're not helping us much, either.

> For everything I am getting line cannot be
> parsed message .

"Everything" covers a big region. Perhaps if
we were psychic, or if you showed your actual
commands with their actual results, we might
be able to guess what you did wrong.

"Doesn't work" is not a useful problem
description.
OldSchool
Honored Contributor

Re: Search and replace Text in a File

"..All the suggestion posted by you doesnt help . For everything I am getting line cannot be parsed message.."

Hmm...seems to work for me (which is what I expected). As Frank noted, its easiest to avoid using a character appearing in the text as the "commmand delimter". Doing so means you don't have to go thru the tedious and error-prone step of "escaping" the delimiter. Thus:

fcat#: pg fsm
/asia/calcutta
/asia/bombay
fcat#:
fcat#: sed -e 's!/asia/calcutta!/india/!g' fsm
/india/
/asia/bombay


Note: having to "escape" the delimiter can be a royal pain....and you tend to see "cannot be parsed" errors. Too many things influence what is the "correct" number of "escapes", like "is this run from a command line" or "is this in a shell script" or others.
Robjan
Frequent Advisor

Re: Search and replace Text in a File

My problem is resolved .
Thanks for all your suggestions