- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- How to Replace string in a particular line using s...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-30-2004 12:39 AM
тАО06-30-2004 12:39 AM
How to Replace string in a particular line using shell script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-30-2004 01:54 AM
тАО06-30-2004 01:54 AM
Re: How to Replace string in a particular line using shell script
$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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-30-2004 02:07 AM
тАО06-30-2004 02:07 AM
Re: How to Replace string in a particular line using shell script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-30-2004 02:15 AM
тАО06-30-2004 02:15 AM
Re: How to Replace string in a particular line using shell script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-30-2004 02:46 AM
тАО06-30-2004 02:46 AM
Re: How to Replace string in a particular line using shell script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-30-2004 01:32 PM
тАО06-30-2004 01:32 PM
Re: How to Replace string in a particular line using shell script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-30-2004 06:59 PM
тАО06-30-2004 06:59 PM
Re: How to Replace string in a particular line using shell script
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
It will change the text to haitext
Do change with sed,
cat
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
exit 1
fi
cat $FILENAME | sed -e 's/text/'$STRING'/g'
set +x
exit 0
#####################################################
Regards,
Muthukumar.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-05-2004 06:32 AM
тАО07-05-2004 06:32 AM
Re: How to Replace string in a particular line using shell script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-23-2004 03:59 AM
тАО09-23-2004 03:59 AM
Re: How to Replace string in a particular line using shell script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2004 01:43 AM
тАО09-24-2004 01:43 AM
Re: How to Replace string in a particular line using shell script
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'
And more we can do pattern change on mulitple files and save updates on the same file as,
perl -p -i -e 's/
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-13-2006 10:34 PM
тАО09-13-2006 10:34 PM
Re: How to Replace string in a particular line using shell script
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