1834628 Members
3057 Online
110069 Solutions
New Discussion

Need help on scripting

 
SOLVED
Go to solution
Dewa Negara_4
Regular Advisor

Need help on scripting

Hi All,

Please help on my scripting problem below. On my script, I want to replace all lines that contain a specific word to particular entry. For example, file1 contains :

flower rose red
fruit apple sweet
fruit orange sweet
fruit water melon not sweet

Want to replace all lines that contain fruit with word ' FRUIT NICE', so the target file will be as below:

flower rose red
FRUIT NICE
FRUIT NICE
FRUIT NICE

Thanks in advance.

Regards,
Negara
Santos
10 REPLIES 10
T G Manikandan
Honored Contributor

Re: Need help on scripting

#sed -e 's/fruit/FRUIT NICE/g' <script-name>
Karthik S S
Honored Contributor

Re: Need help on scripting

sed /fruit/s/fruit/FRUIT\ NICE/g filename > filename.new

mv filename.new filename

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Need help on scripting

# perl -pe'/\bfruit\b/i and$_="FRUIT NICE\n"' file

or

# perl -pe's/.*\bfruit\b.*/FRUIT NICE/i' file

or

...

TMTOWTDI

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Dewa Negara_4
Regular Advisor

Re: Need help on scripting

Thanks, but it gave me this output.
# sed -e 's/fruit/FRUIT NICE/g' file1
flower rose red
FRUIT NICE apple sweet
FRUIT NICE orange sweet
FRUIT NICE water melon not sweet

It is now what I want. I want this output :

flower rose red
FRUIT NICE
FRUIT NICE
FRUIT NICE

Thanks.
Santos
Dewa Negara_4
Regular Advisor

Re: Need help on scripting

Thanks Karthik,

But it gave me the output below.

# sed /fruit/s/fruit/FRUIT\ NICE/g file1
flower rose red
FRUIT NICE apple sweet
FRUIT NICE orange sweet
FRUIT NICE water melon not sweet

Please help. Thanks.
Santos
Dewa Negara_4
Regular Advisor

Re: Need help on scripting

Procura,

Thanks. It looks fine. But it is possible use a shell sctipt instead of perl?

Thanks.

# perl -pe's/.*\bfruit\b.*/FRUIT NICE/i' file1
flower rose red
FRUIT NICE
FRUIT NICE
FRUIT NICE
Santos
Ravi_8
Honored Contributor

Re: Need help on scripting

Hi,

procura's answer works. sed and awk are not working
never give up
Dewa Negara_4
Regular Advisor

Re: Need help on scripting

Hi All,

Thanks for your great help. It looks fine now.

Thanks and Best Regards,
Dewa
Santos
T G Manikandan
Honored Contributor

Re: Need help on scripting

Try this

#sed -e '/^fruit/s/^.*$/FRUIT NICE/g'

OR
#sed -e '/fruit/s/^.*$/FRUIT NICE/g'
Michael Schulte zur Sur
Honored Contributor

Re: Need help on scripting

Hi,

this works also:
awk '/fruit/{print "FRUIT NICE";next}{print $0}' file

Michael