Replace a word from a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a word from a string
# 1  
Old 11-19-2009
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 dutt para shar"
a="para"
new="PARA"
flag=`echo "$t" | grep -c "$a"`
if [ $flag -eq 1 ] ; then
echo $flag
s=`echo "$t" | sed 's/$a/$new/'`
echo "s: $s"
else
echo "nop"
fi

but outptu is same (shiv dutt para shar)
i am unable to replace $a with $new.

please help me.
# 2  
Old 11-19-2009
You can do something like that :
Code:
var="shiv_dutt_para_shar"
wrd="para"
rep_wrd="PARA"

s=$(echo "$var" | sed "s/$wrd/$rep_wrd/")
if [ "$s" = "$var" ]
then echo "nop"
else echo "s: $s"
fi

Jean-Pierre.

---------- Post updated at 15:07 ---------- Previous update was at 15:03 ----------

With bash :
Code:
var="shiv_dutt_para_shar"
wrd="para"
rep_wrd="PARA"

s=${var/$wrd/$rep_wrd}
if [ "$s" = "$var" ]
then echo "nop"
else echo "s: $s"
fi

Jean-Pierre.
# 3  
Old 11-19-2009
thanks Jean-Pierre, thanks a lot

Last edited by jadoo_c2; 11-20-2009 at 12:02 AM..
# 4  
Old 11-19-2009

In any POSIX shell:

Code:
_sub() ## USAGE: _sub STRING SUBSTRING REPLACEMENT
{      ## RESULT: stored in $_SUB
  _SUB=$1
  [ -n "$2" ] || return 1 ## nothing to search for: error
  s_srch=${2}  ## pattern to replace
  rep=$3       ## replacement string
  case $_SUB in
    *$s_srch*)    ## if pattern exists in the string
       sr1=${_SUB%%$s_srch*}  ## take the string preceding the first match
       sr2=${_SUB#*$s_srch}   ## and the string following the first match
       _SUB=$sr1$rep$sr2      ## and sandwich the replacement string between them
       ;;
    *) return 1 ;;   ## if the pattern does not exist, return an error code
  esac
}

sub() ## USAGE: sub STRING SUBSTRING REPLACEMENT
{     ## RESULT: printed to stdout
  _sub "$@"
  printf "%s\n" "$_SUB"
}

If the shell is bash or ksh93, I replace _sub() with:

Code:
_sub () 
{ 
    _SUB=${1/$2/"$3"}
}

# 5  
Old 11-20-2009
thanks to all of you for your valuable help.

---------- Post updated at 10:29 AM ---------- Previous update was at 09:32 AM ----------

i have one more dout.

can i append a word in string i.e.
var-"SHIV_DUTT_PARA_SHAR"
apd_wrd="MR"

now i want following output - "MR_SHIV_DUTT_PARA_SHAR".

$apd_wrd can be appended any where like "SHIV_DUTT_MR_PARA_SHAR"

How can i do that?

cfajohnson, will your script work for this?


---------- Post updated at 11:15 AM ---------- Previous update was at 10:29 AM ----------

please help
# 6  
Old 11-22-2009
Code:
$ var="SHIV_DUTT_PARA_SHAR"
apd_wrd="MR"
apd="${apd_wrd}_${var}"
echo $apd

MR_SHIV_DUTT_PARA_SHAR

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search and replace the string with new word using xml tags

Hi All i need to replace the url1 inside <remote> tag in below xml in first instance and in the second instance with url2. any help appreciated <locations> <hudson.scm.SubversionSCM_-ModuleLocation> <remote>https://svn2015.com/svn/repos/internalshard</remote> ... (4 Replies)
Discussion started by: madankumar.t@hp
4 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 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

5. Shell Programming and Scripting

grep part of word or Another word from a string

Hi all, FileOne family balance >>>>> 0 0 0 0 java.io.FileNotFoundException: Settings.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) .. .... ..... ..... java.lang.NullPointerException ... ..... ...... Stacktrace: at... (2 Replies)
Discussion started by: linuxadmin
2 Replies

6. 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

7. Shell Programming and Scripting

Replace first word after string INITIAL

Hi I have a file with hundreds of lines, some of the lines have word INITIAL followed by some numbers like ....INITIAL 1234535 .... ....INITIAL 5768644 .... I would like to replace the number after word INITLA with 4K how can I do it? Cant get my heard around this! The string is always... (3 Replies)
Discussion started by: halacil
3 Replies

8. Shell Programming and Scripting

Replace a word After a string.

Hello Gurus, I have a file which has foll contents scattered: ,TotUnasgndNetAmt FROM DEV_EIS_T.Wkly_SO_CCD_MOSumSnpsht WHERE CalDayRunDt = '2010-07-21' UNION ALL SELECT CalDayRunDt ,BusWkCd ,'N' I want to replace 2010-07-21 that starts after ' and ends before... (8 Replies)
Discussion started by: indrajit_u
8 Replies

9. UNIX for Dummies Questions & Answers

Seach for part of string and replace whole word

I am trying to find words in a text with a certain ending with sed and replace them with themselves but wrapped in tabs ex.: The fish swims in the water. -> searching for -ms ending The fish <tab>swims<tab>in the water. I've been trying all sorts of commands and get either an error... (5 Replies)
Discussion started by: stinnes
5 Replies

10. Shell Programming and Scripting

Replace a word with string from another file

Hi, this belongs a little to my other post but only at the starting point. With find -name "*.htm*" i got a list like this: ./1999/01/file1.html ./1999/01/file2.html ./1999/02/file1.html ./2000/04/file1.html ./2000/04/file2.html ./2000/04/file3.html ./2000/file1.html... (2 Replies)
Discussion started by: Tonda
2 Replies
Login or Register to Ask a Question