Operating System - HP-UX
1849943 Members
2128 Online
104049 Solutions
New Discussion

Re: replace characters in a file

 
SOLVED
Go to solution
Sajeesh O.K
Advisor

replace characters in a file

Hi

Anybody know how to replace a charater string with a different string from a file having 1000 lines.The file is like this

aaaa:bbbb:cccc:dddd:eeee:ffff:gggg:

I want to replace cccc with xxxxx in all lines

Thanks
Sajeesh


15 REPLIES 15
H.Merijn Brand (procura
Honored Contributor

Re: replace characters in a file

# perl -pi -e's/cccc/xxxxx/' file

or more safe, if cccc can occur in a word like ddccccee

# perl -pi -e's/\bcccc\b/xxxxx/' file

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Arunvijai_4
Honored Contributor

Re: replace characters in a file

HI,

You can use sed.

# sed 's/cccc/xxxxx/g' file

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
spex
Honored Contributor

Re: replace characters in a file

Sajeesh,

'tr' also works:

tr 'cccc' 'xxxx'
PCS
H.Merijn Brand (procura
Honored Contributor

Re: replace characters in a file

no, tr works on a character by character basis.
what you wrote here is essentially the same as

tr c x new_file

and will change *every* occurance of 'c' to 'x' in file in every line. That's not what he wants

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Jose Mosquera
Honored Contributor

Re: replace characters in a file

Hi,

#cat file|sed 's/cccc/xxxxx/g' > newfile

Rgds.
spex
Honored Contributor

Re: replace characters in a file

Oops. Good catch, procura. My apologies.
H.Merijn Brand (procura
Honored Contributor

Re: replace characters in a file

Jose, that's the same answer that Arunvijai gave, and even worse, you promote this awful unneeded use of cat

# cat file | process

should be prevented wherever possible. It's just a useless extra load on the CPU, as processes are started that are not needed in a sane unix environment, where

# process < file

does exactly the same, but uses one less process. Cat can be useful if *more* input files create the input stream for process, as input redirection does not support that

# cat file1 file2 | process

Enjoy, Have (efficient) FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Peter Nikitka
Honored Contributor

Re: replace characters in a file

Hi,

lets play 'sed' a little...
I think your field delimiter is ':' - this fact was not used in the sed-solutions so far.

from=cccc
to=xxxx
sed -e "s/^$from:/$to:/" -e "s/:$from/:$to/g" input >output

But as you see from the previous responses: there is more than one way ...

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Jose Mosquera
Honored Contributor

Re: replace characters in a file

Please excuse me, especially the partner Pharaoh, and omit this answer.

About the sharp comment of the Mr. Procura, there are not words...

Rgds.
H.Merijn Brand (procura
Honored Contributor

Re: replace characters in a file

Jose, sorry if I sounded harsh, and it is nothing personal, but somehow the useless use of cat always triggers this reaction with me.
Again, nothing personal, but yet another good oportunity to set things straight!

Enjoy, Have FUN! H.Merijn [ no points here please ]
Enjoy, Have FUN! H.Merijn
Sajeesh O.K
Advisor

Re: replace characters in a file

Hi All

Thanks for your answer. Sorry. I didn't make it clear.

aaa bbb ccc ddd eee fff represent strings.
and delemited with : just like passwd file.
I want to replace the 3rd field with "---"

Kindly provide the solution

Thanks
Sajeesh

Junghun
Occasional Advisor

Re: replace characters in a file

for example the filename is 111 and turn it to file 222
#from=ccc
#to=---
#sed -e "s/$from/$to/" -e "s/$from/$to/g" 111 > 222

--Z.X.
H.Merijn Brand (procura
Honored Contributor
Solution

Re: replace characters in a file

> aaa bbb ccc ddd eee fff represent strings.
> and delemited with : just like passwd file.
> I want to replace the 3rd field with "---"

# perl -pi -aF: -e'$F[2]="---";$_=join":",@F' file

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Sajeesh O.K
Advisor

Re: replace characters in a file

procura

Perfect. Thanks for the promt reply


Sajeesh
Peter Nikitka
Honored Contributor

Re: replace characters in a file

Hi,

to complete the thread, here a solution with awk:

awk -F: '$3 == "ccccc" {$3="-----"}
{print}' input >output

Or sed?

sed 's/^\([^:]*:[^:]*:\)'$from'\(.*\)/\1'$to'\2/' input >output

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"