Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing a word after a matched pattern

Status
Not open for further replies.

maxmave

Programmer
Jun 3, 2008
5
US
Hello,

Actually i want to replace the word after a matched pattern.

For Ex:
lets say that i am reading a file line by line

while read line
do
echo $line
# i need to search whether a pattern exists in the file and replace the word after if the pattern exist.
# for example : $line=" this is a test line"
# $Pattern="is a"
echo "Enter a new value to replace the '"$Var1"' : "
read NwPat <&1
# i want to retrive $Var1="test" and replace it with a another pattern
# $NwPat which is provided as user input.
done < $INT_FILE


Any help would be useful.

Thanks

Rahul
 
got the solution guys its TXT=`echo $line | sed -e 's/.* //'`

Thanks

rahul
 
If
[ol]
[li]Your search pattern is $search[/li]
[li]Your additional string is $add[/li]
[/ol]
Then
Code:
TXT=$(echo $line|sed -e's/'"$search"'/'"$search"' '"$add"'/')
As an example
Code:
$ search="is a"
$ add="added bit"
$ line="This is a line"
$ TXT=$(echo $line|sed -e 's/'"$search"'/'"$search"' '"$add"'/')
$ echo $TXT
This is a added bit line


On the internet no one knows you're a dog

Columb Healy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top