How to replace the word?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace the word?
# 1  
Old 05-24-2012
MySQL How to replace the word?

Hi Everybody,

I am having a query like I want to change only first three patterns of the first line in a file.

ex: I like unix unix unix unix is very much.

after using the command it should be like

I like linux linux linux unix is very much.

Thanks,
Naga
# 2  
Old 05-24-2012
Hi


Code:
$ cat file
 I like unix unix unix unix is very much
$ awk '{j=0;for(i=1;i<=NF&&j<3;i++)if($i=="unix"){$i="linux";j++;}}1' file
I like linux linux linux unix is very much

Guru.
# 3  
Old 05-24-2012
Another approach:
Code:
sed -e 's/unix/linux/g' -e 's/linux/unix/4' file

# 4  
Old 05-24-2012
Using sed (longer but more foolproof than the previous one... Smilie)

Code:
echo "I like unix unix unix unix is very much."|sed 's/unix/linux/1
s/unix/linux/1
s/unix/linux/1'

Will also work for an input like

Code:
I like unix unix linux unix unix is very much.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How do i replace a word ending with "key" using awk excpet for one word?

echo {mbr_key,grp_key,dep_key,abc,xyz,aaa,ccc} | awk 'gsub(/^|abc,|$/,"") {print}' Required output {grp_key,xyz,aaa,ccc} (5 Replies)
Discussion started by: 100bees
5 Replies

2. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name>XXX.com</name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

3. Shell Programming and Scripting

Find and replace a word in all the files (that contain the word) under a directory

Hi Everyone, I am looking for a simple way for replacing all the files under a directory that use the server "xsgd1234dap" with "xsdr3423pap". For Example: In the Directory, $pwd /home/nick $ grep -l "xsgd1234dap" *.sh | wc -l 119 I have "119" files that are still using... (5 Replies)
Discussion started by: filter
5 Replies

4. Shell Programming and Scripting

replace word

Hi All, I want to do some calculation in 5th field and then replace the original 5th value to calcualted new value: ifile 1|2|3|4|one two three|test Expecting ofile 1|2|3|4|twothree|test I tried below commands and acheived. awk -F"|" '{print $5}' ifile | awk '{if (NF==3) {print... (5 Replies)
Discussion started by: Jairaj
5 Replies

5. Shell Programming and Scripting

Replace a word in a string starting with another word

Hi All, I have a file in which a number of lines are starting with similar first word but different next words. I want to replace the any nth word(not 1st or 2nd) with another word. Eg:- My file contains are like this:- Ram is a boy. Ram is a good boy. Ram plays cricket. Here I want to... (2 Replies)
Discussion started by: mukeshbaranwal
2 Replies

6. Shell Programming and Scripting

How to replace a word with another word

Hello Friends, How to substitue with a word by another word in file written in VI Editor. Thanks & Regards Siva Ranganath 7760774961 (2 Replies)
Discussion started by: sivaranganath
2 Replies

7. Shell Programming and Scripting

Replace a word after a particular word in a file

Hi, I want to replace a word in a file which occurs after a particular word. For example : $cat file.txt CASE WHEN AND c1 = 'I' AND c2= '2' THEN 1 WHEN AND c1= 'I' AND c2= '0' THEN 2 So in this example i want to replace... (4 Replies)
Discussion started by: ashwin3086
4 Replies

8. Shell Programming and Scripting

Replace a word from a string

How can i replace a particular word from string i.e. var="shiv_dutt_para_shar" wrd="para" rep_wrd="PARA" what i am trying to do that first i'll search if $var catains #wrd or not. if it contains then i've to replace $wrd with $rep_wrd. I have tried following #!/bin/sh t="shiv... (5 Replies)
Discussion started by: jadoo_c2
5 Replies

9. Solaris

Replace word.

Dear I want to ask about replace word ( sharing word ) in files ( more than one files ) in one directory. Example:- I serach about ( ahmad ) in directory ( x ) i saw the results in ( 10 ) files ( differetn files ) i want to replace this word ( ahmad ) to ( Mohammad ) in one command. how i... (2 Replies)
Discussion started by: abu_hassan
2 Replies

10. Shell Programming and Scripting

how to replace a word with another

Hi All, v_notes = "Ravi & kumar & garlapati" i want out put like "Ravi and kumar and garlapati" What is the command to get this as output, we have to replace all & with and. Hope this is clear. Regards, Ravi Kumar Garlapati (1 Reply)
Discussion started by: rkrgarlapati
1 Replies
Login or Register to Ask a Question