Operating System - Linux
1819792 Members
3090 Online
109607 Solutions
New Discussion юеВ

How to Replace string in a particular line using shell script

 
shetty
Frequent Advisor

How to Replace string in a particular line using shell script

Hi guys,
Iam having a file(text) file. I want to replace a part of string with another string.

For example
*************
File contents

text
The above line is somewhere in the middle of the file,which contains around 200 lines.
I am unable to point to that particular line and replace it.

I have written a script , that prompts for the string to be replaced.

if input is mytext, then the file should be updated as shown below.
mytext


Please help me out to solve.

Shetty.


10 REPLIES 10
Olivier Drouin
Trusted Contributor

Re: How to Replace string in a particular line using shell script

#!/usr/bin/perl -s


$strng = "your input";

open FILE , "+< testfile";
open OUTFILE, "> testfile2";

while ( $line = ) {

if ( $line =~ /()(.*)(<\/html>)/ ) {
$line = $1 . $strng . $3;
print OUTFILE "$line\n";
} else {
print OUTFILE $line;
}

}
close FILE;
close OUTFILE;


outfile2 has the good data now... you can move it over outfile.
shetty
Frequent Advisor

Re: How to Replace string in a particular line using shell script

Hi olivier,

Thank you for the quick answer.
I am doing this using shell script in unix and perl is something new to me.

can this be done using shell script.

Regards
shetty.
Abdul Rahiman
Esteemed Contributor

Re: How to Replace string in a particular line using shell script

Shetty,

Are you looking something like to search and replace the pattern anywhere in the file ?

If yes, why not use sed,
#sed "s/\text\<\/html\>/\mytext\<\/html\>/" myfile > mynewfile

regds,
Abdul.
No unix, no fun
Jan Sladky
Trusted Contributor

Re: How to Replace string in a particular line using shell script

Hi Sheety,

yes, the simpliest way si to use sed:

sed 's/text/mytext/' orig_file.txt | tee new_file.txt

-s option means substitude,
| tee is piping into tee command

tee copy standard input (STDIN) to file(s) and on standard output (STDOUT) - you can see the output of your script


br Jan
GSM, Intelligent Networks, UNIX
Olivier Drouin
Trusted Contributor

Re: How to Replace string in a particular line using shell script

yep...sed is the way to go if you do it in shell script.
Muthukumar_5
Honored Contributor

Re: How to Replace string in a particular line using shell script

hai,

To replace the string we can use sed command. If you want to do in the editor itself we can do it,in vi editor as like
To replace text as haitext

use
:s/>texthaitext

It will change the text to haitext

Do change with sed,
cat | sed -e s/>texthaitext
where g is used to all string replacements globally.

Using shell script as,
#####################################################
#!/usr/bin/ksh
# Debugging mode
set -x

FILENAME=$1
STRING=$2

if [[ $# -ne 2 ]]; then
echo "Usage: $0 String to be replaced>"
exit 1
fi

cat $FILENAME | sed -e 's/text/'$STRING'/g'

set +x
exit 0

#####################################################

Regards,
Muthukumar.
Easy to suggest when don't know about the problem!
edgarc
New Member

Re: How to Replace string in a particular line using shell script

hi shetty,

if you want to search and replace certain string patterns on multiple files, for example: if you want to replace the string mytext with mytext to all *.html files in your current directory, you can execute a command like the ff using perl:

perl -e 's/mytext/mytext<\/html>/gi' -p -i.bak *.html

will replace all occurrences of the string mytext on all files with .html extension on the directory where you executed the command, at the same time retaining the original file with .bak extension and the updated file with the original file names

hope this will help
vetriselvan s
Advisor

Re: How to Replace string in a particular line using shell script

Hi Shetty'

With Sed & awk you can replace a pattern with an string

for example
sed 's/pattern/replacing string/' file-name

if u want to replace it in a file
______________________________
mv original duplicate

sed 's/pattern/replacing string/' duplicate > original
rm duplicate
___________________________
this will do for u.
the Beauty of Unix lies here
in the pattern if u are having some special characters then u have to escape them with esc character (forward slash).

and also there is no rule that u have to use the back slash as seperator

u can very well use any characters

like sed 'p!patteren!' file -name
this way we can use the Stream line editor as powerful as possible,.

this link will help u alot
http://www.unix.org.ua/orelly/unix/sedawk/ch02_04.htm
Muthukumar_5
Honored Contributor

Re: How to Replace string in a particular line using shell script

Vetri,

We don't need to duplication files on text processing and change, we can simply do as,

echo "`sed -e 's/pattern/change/g' '" >

else as,
sed -e 's/pattern/change/g' | cat -

And more we can do pattern change on mulitple files and save updates on the same file as,

perl -p -i -e 's///g'

It will update automatically.

And shetty when you are processing html files there will be change to use / \ etc there..

We can use % or @ or ; or : as demilter to / there.

In this example we can do easily as,

echo "Enter your own replace"
read replace

perl -p -i -e 's%mytext%'$replace'%g'

It will update automatically there.
Easy to suggest when don't know about the problem!
arun_swain2000
New Member

Re: How to Replace string in a particular line using shell script

sh replace.sh source_string destination_string On_which_file

replace.sh
-----------
#!/bin/bash
TEMPFILE=/tmp/replace.tmp
if test $# -lt 3; then
echo Please Follow the format sh scriptname srcstr deststr filename
exit 1
fi
srcstr=$1 #The first argument as source string
deststr=$2 #The second argument as destination string
shift 2
for file ; do
occurrence=`grep $srcstr $file | wc -l | sed -e "s/ //g"`
if test $nummatches -eq 0; then
echo $file: 0 occurrences
else
echo $file: replacing $occurrence occurrences
sed -e "s+$srcstr+$deststr+g" $file > $TEMPFILE
mv $TEMPFILE $file
fi
done