Operating System - HP-UX
1819941 Members
3387 Online
109607 Solutions
New Discussion юеВ

Re: character replacemnt in a file

 
SOLVED
Go to solution
sheevm
Regular Advisor

character replacemnt in a file

Hi,

Can someone help me in how to replace globally certain characeters in a file?

Ex: I have several lines looks like

abcd <1?
abcd <20>
abcd <5>
abcd <100>

I want to make abcd <5000> for all the occurance of abcd, the value in <> is different.

Thanks for your help.
Rajim
be good and do good
10 REPLIES 10
siva0123
Trusted Contributor

Re: character replacemnt in a file

Hi ,

Open the file in vi editor
Go to command mode and
Then use the following the syntax
:$s/str1/str2/g

str1 ==>the string pattern to be replaced
str2 ==>The string which has to be there


Please make sure there is no space in the command
sheevm
Regular Advisor

Re: character replacemnt in a file

hI,

But str2 has different value on each occurence as you see in my example. Is there a way to use the wild card

abcd <*> for abcd <1> and abcd <20> etc...?

Thanks
be good and do good
Frank de Vries
Respected Contributor

Re: character replacemnt in a file

hello

Then I would do this:

in vi mode type :
1,$s/abcd <.*>/abcd <5000>/g

Look before you leap
Frank de Vries
Respected Contributor
Solution

Re: character replacemnt in a file

sorry, I pressed submit to quickly,
I wanted to explain what it means.

:1,$ is from line 1 to last line

the .* between < > means any character

good luck
Look before you leap
Frank de Vries
Respected Contributor

Re: character replacemnt in a file

sorry, I pressed submit to quickly,
I wanted to explain what it means.

:1,$ is from line 1 to last line

the .* between < > means any character and any number of characters.

So that should fit <1> or <100> or <23000>
whatever , will be changed to <5000>

good luck
Look before you leap
Frank de Vries
Respected Contributor

Re: character replacemnt in a file

sorry, I pressed submit to quickly,
I wanted to explain what it means.

:1,$ is from line 1 to last line

the .* between < > means any character and any number of characters.

So that should fit <1> or <100> or <23000>
whatever , will be changed to <5000>

You do not have to repeat the abcd, I
did that for clarity:
just the matching pattern which I hope
in your case is nicely between the < >


good luck
Look before you leap
Sandman!
Honored Contributor

Re: character replacemnt in a file

# sed 's/^abcd <[0-9]*>/abcd <5000>/g' file
sheevm
Regular Advisor

Re: character replacemnt in a file

Frank,

The command you gave did not work. Here is the sample file.

I want to repalce everything in ()


Column1 varchar2 (1),
Column2 varchar2 (14),
Column3 varchar2 (6),
Column4 varchar2 (4000),
Column5 varchar2 (4000),
Column6 varchar2 (10),
Column7 varchar2 (4000),
Column8 varchar2 (16),
Column9 varchar2 (6),
Column10 varchar2 (28),
Column11 varchar2 (4),
Column12 varchar2 (9),
Column13 varchar2 (1),
Column14 varchar2 (4000),
Column15 varchar2 (3),
Column16 varchar2 (4),

Thanks
be good and do good
Sandman!
Honored Contributor

Re: character replacemnt in a file

Based on the sample input provided here's a sed(1) version that works:

# sed 's/^\(Column[0-9]*\) \(varchar2\) ([0-9]*)/\1 \2 (5000)/p' file

~hope it helps
Frank de Vries
Respected Contributor

Re: character replacemnt in a file

Hi Rajim
Sorry I was away for a long weekend.
I suspect you have your solution already,
but just to complete my answer to what
I would have done.

Yes, naturally I base my command on <>,
when replacing () it becomes a different
ballgame:

If you want to use it with round brackets,
do this:

1,$s!char2 (.*)!char2 (5000)!g

Just to illustrate the different effects:
If you just did

1,$s!(.*)!(5000)!g

It would replace the whole line with (5000).
The brackets are a bit special.


Look before you leap