<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to Replace string in a particular line using shell script in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/how-to-replace-string-in-a-particular-line-using-shell-script/m-p/3319056#M12945</link>
    <description>Hi guys,&lt;BR /&gt;     Iam having a file(text) file. I want to replace a part of string with another string.&lt;BR /&gt;&lt;BR /&gt;For example&lt;BR /&gt;*************&lt;BR /&gt;File contents&lt;BR /&gt;&lt;BR /&gt;             text&lt;BR /&gt;The above line is somewhere in the middle of the file,which contains around 200 lines.&lt;BR /&gt;I am unable to point to that particular line and replace it.&lt;BR /&gt;&lt;BR /&gt;I have written a script , that prompts for the string to be replaced.&lt;BR /&gt;&lt;BR /&gt;if input is mytext, then the file should be updated as shown below.&lt;BR /&gt;              mytext&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Please help me out to solve.&lt;BR /&gt;&lt;BR /&gt;Shetty.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
    <pubDate>Wed, 30 Jun 2004 07:39:16 GMT</pubDate>
    <dc:creator>shetty</dc:creator>
    <dc:date>2004-06-30T07:39:16Z</dc:date>
    <item>
      <title>How to Replace string in a particular line using shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-replace-string-in-a-particular-line-using-shell-script/m-p/3319056#M12945</link>
      <description>Hi guys,&lt;BR /&gt;     Iam having a file(text) file. I want to replace a part of string with another string.&lt;BR /&gt;&lt;BR /&gt;For example&lt;BR /&gt;*************&lt;BR /&gt;File contents&lt;BR /&gt;&lt;BR /&gt;             text&lt;BR /&gt;The above line is somewhere in the middle of the file,which contains around 200 lines.&lt;BR /&gt;I am unable to point to that particular line and replace it.&lt;BR /&gt;&lt;BR /&gt;I have written a script , that prompts for the string to be replaced.&lt;BR /&gt;&lt;BR /&gt;if input is mytext, then the file should be updated as shown below.&lt;BR /&gt;              mytext&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Please help me out to solve.&lt;BR /&gt;&lt;BR /&gt;Shetty.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 30 Jun 2004 07:39:16 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-replace-string-in-a-particular-line-using-shell-script/m-p/3319056#M12945</guid>
      <dc:creator>shetty</dc:creator>
      <dc:date>2004-06-30T07:39:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to Replace string in a particular line using shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-replace-string-in-a-particular-line-using-shell-script/m-p/3319057#M12946</link>
      <description>#!/usr/bin/perl -s&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;$strng = "your input";&lt;BR /&gt;&lt;BR /&gt;open FILE , "+&amp;lt; testfile";&lt;BR /&gt;open OUTFILE, "&amp;gt; testfile2";&lt;BR /&gt;&lt;BR /&gt;while ( $line = &lt;FILE&gt; ) {&lt;BR /&gt;&lt;BR /&gt;    if (  $line =~ /()(.*)(&amp;lt;\/html&amp;gt;)/ ) {&lt;BR /&gt;        $line = $1 . $strng . $3;&lt;BR /&gt;        print OUTFILE "$line\n";&lt;BR /&gt;    } else {&lt;BR /&gt;        print OUTFILE $line;&lt;BR /&gt;    }&lt;BR /&gt;&lt;BR /&gt;}&lt;BR /&gt;close FILE;&lt;BR /&gt;close OUTFILE;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;outfile2 has the good data now... you can move it over outfile.&lt;BR /&gt;&lt;/FILE&gt;</description>
      <pubDate>Wed, 30 Jun 2004 08:54:17 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-replace-string-in-a-particular-line-using-shell-script/m-p/3319057#M12946</guid>
      <dc:creator>Olivier Drouin</dc:creator>
      <dc:date>2004-06-30T08:54:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to Replace string in a particular line using shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-replace-string-in-a-particular-line-using-shell-script/m-p/3319058#M12947</link>
      <description>Hi olivier,&lt;BR /&gt;&lt;BR /&gt;   Thank you for the quick answer.&lt;BR /&gt;I am doing this using shell script in unix and perl is something new to me.&lt;BR /&gt;&lt;BR /&gt;can this be done using shell script.&lt;BR /&gt;  &lt;BR /&gt;Regards&lt;BR /&gt;shetty.</description>
      <pubDate>Wed, 30 Jun 2004 09:07:05 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-replace-string-in-a-particular-line-using-shell-script/m-p/3319058#M12947</guid>
      <dc:creator>shetty</dc:creator>
      <dc:date>2004-06-30T09:07:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to Replace string in a particular line using shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-replace-string-in-a-particular-line-using-shell-script/m-p/3319059#M12948</link>
      <description>Shetty,&lt;BR /&gt;&lt;BR /&gt;Are you looking something like to search and replace the pattern anywhere in the file ?&lt;BR /&gt;&lt;BR /&gt;If yes, why not use sed,&lt;BR /&gt;#sed "s/\text\&amp;lt;\/html\&amp;gt;/\mytext\&amp;lt;\/html\&amp;gt;/" myfile &amp;gt; mynewfile&lt;BR /&gt;&lt;BR /&gt;regds,&lt;BR /&gt;Abdul.</description>
      <pubDate>Wed, 30 Jun 2004 09:15:50 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-replace-string-in-a-particular-line-using-shell-script/m-p/3319059#M12948</guid>
      <dc:creator>Abdul Rahiman</dc:creator>
      <dc:date>2004-06-30T09:15:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to Replace string in a particular line using shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-replace-string-in-a-particular-line-using-shell-script/m-p/3319060#M12949</link>
      <description>Hi Sheety,&lt;BR /&gt;&lt;BR /&gt;yes, the simpliest way si to use sed:&lt;BR /&gt;&lt;BR /&gt;sed 's/text/mytext/' orig_file.txt | tee                                                new_file.txt&lt;BR /&gt;&lt;BR /&gt;-s option means substitude, &lt;BR /&gt;| tee is piping into tee command &lt;BR /&gt;&lt;BR /&gt;tee  copy standard input (STDIN) to file(s) and   on standard output (STDOUT) - you can see the output of your script&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;br Jan</description>
      <pubDate>Wed, 30 Jun 2004 09:46:57 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-replace-string-in-a-particular-line-using-shell-script/m-p/3319060#M12949</guid>
      <dc:creator>Jan Sladky</dc:creator>
      <dc:date>2004-06-30T09:46:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to Replace string in a particular line using shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-replace-string-in-a-particular-line-using-shell-script/m-p/3319061#M12950</link>
      <description>yep...sed is the way to go if you do it in shell script.</description>
      <pubDate>Wed, 30 Jun 2004 20:32:14 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-replace-string-in-a-particular-line-using-shell-script/m-p/3319061#M12950</guid>
      <dc:creator>Olivier Drouin</dc:creator>
      <dc:date>2004-06-30T20:32:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to Replace string in a particular line using shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-replace-string-in-a-particular-line-using-shell-script/m-p/3319062#M12951</link>
      <description>hai,&lt;BR /&gt;&lt;BR /&gt; 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&lt;BR /&gt;  To replace text as haitext&lt;BR /&gt;&lt;BR /&gt; use &lt;BR /&gt;   &lt;ESC&gt;:s/&amp;gt;texthaitext&lt;BR /&gt;  &lt;BR /&gt;It will change the text to haitext&lt;BR /&gt;  &lt;BR /&gt;  Do change with sed,&lt;BR /&gt;  cat &lt;FILENAME&gt; | sed -e s/&amp;gt;texthaitext&lt;/FILENAME&gt;&lt;BR /&gt; where g is used to all string replacements globally.&lt;BR /&gt;  &lt;BR /&gt;Using shell script as,&lt;BR /&gt;#####################################################&lt;BR /&gt;  #!/usr/bin/ksh&lt;BR /&gt;  # Debugging mode&lt;BR /&gt;  set -x&lt;BR /&gt;  &lt;BR /&gt;  FILENAME=$1&lt;BR /&gt;  STRING=$2&lt;BR /&gt;  &lt;BR /&gt;  if [[ $# -ne 2 ]]; then&lt;BR /&gt;    echo "Usage: $0 &lt;FILENAME&gt; String to be replaced&amp;gt;"&lt;BR /&gt;    exit 1&lt;BR /&gt;  fi&lt;BR /&gt;  &lt;BR /&gt;  cat $FILENAME | sed -e 's/text/'$STRING'/g'&lt;BR /&gt;  &lt;BR /&gt;  set +x&lt;BR /&gt;  exit 0&lt;BR /&gt;  &lt;BR /&gt;  #####################################################&lt;BR /&gt;  &lt;BR /&gt;Regards,&lt;BR /&gt;Muthukumar.&lt;/FILENAME&gt;&lt;/ESC&gt;</description>
      <pubDate>Thu, 01 Jul 2004 01:59:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-replace-string-in-a-particular-line-using-shell-script/m-p/3319062#M12951</guid>
      <dc:creator>Muthukumar_5</dc:creator>
      <dc:date>2004-07-01T01:59:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to Replace string in a particular line using shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-replace-string-in-a-particular-line-using-shell-script/m-p/3319063#M12952</link>
      <description>hi shetty,&lt;BR /&gt;&lt;BR /&gt;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:&lt;BR /&gt;&lt;BR /&gt;perl -e 's/mytext/mytext&amp;lt;\/html&amp;gt;/gi' -p -i.bak *.html&lt;BR /&gt;&lt;BR /&gt;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&lt;BR /&gt;&lt;BR /&gt;hope this will help</description>
      <pubDate>Mon, 05 Jul 2004 13:32:37 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-replace-string-in-a-particular-line-using-shell-script/m-p/3319063#M12952</guid>
      <dc:creator>edgarc</dc:creator>
      <dc:date>2004-07-05T13:32:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to Replace string in a particular line using shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-replace-string-in-a-particular-line-using-shell-script/m-p/3319064#M12953</link>
      <description>Hi Shetty'&lt;BR /&gt;&lt;BR /&gt;With Sed &amp;amp; awk you can replace a pattern with an string&lt;BR /&gt;&lt;BR /&gt;for example&lt;BR /&gt;sed 's/pattern/replacing string/' file-name&lt;BR /&gt;&lt;BR /&gt;if u want to replace it in a file&lt;BR /&gt;______________________________&lt;BR /&gt;mv original duplicate&lt;BR /&gt;&lt;BR /&gt;sed 's/pattern/replacing string/' duplicate &amp;gt; original&lt;BR /&gt;rm duplicate&lt;BR /&gt;___________________________&lt;BR /&gt;this  will do for u.&lt;BR /&gt;the Beauty of Unix lies here&lt;BR /&gt;in the pattern if u are having some special characters then u have to escape them with esc character (forward slash).&lt;BR /&gt;&lt;BR /&gt;and also there is no rule that u have to use the back slash as seperator&lt;BR /&gt;&lt;BR /&gt;u can very well use any characters&lt;BR /&gt;&lt;BR /&gt;like sed 'p!patteren!' file -name&lt;BR /&gt;this way we can use the Stream line editor as powerful as possible,.&lt;BR /&gt;&lt;BR /&gt;this link will help u alot&lt;BR /&gt;&lt;A href="http://www.unix.org.ua/orelly/unix/sedawk/ch02_04.htm" target="_blank"&gt;http://www.unix.org.ua/orelly/unix/sedawk/ch02_04.htm&lt;/A&gt;</description>
      <pubDate>Thu, 23 Sep 2004 10:59:29 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-replace-string-in-a-particular-line-using-shell-script/m-p/3319064#M12953</guid>
      <dc:creator>vetriselvan s</dc:creator>
      <dc:date>2004-09-23T10:59:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to Replace string in a particular line using shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-replace-string-in-a-particular-line-using-shell-script/m-p/3319065#M12954</link>
      <description>Vetri,&lt;BR /&gt;&lt;BR /&gt; We don't need to duplication files on text processing and change, we can simply do as,&lt;BR /&gt;&lt;BR /&gt;  echo "`sed -e 's/pattern/change/g' &lt;INPUT-FILENAME&gt;'" &amp;gt; &lt;INPUT-FILENAME&gt;&lt;BR /&gt;&lt;BR /&gt;  else as,&lt;BR /&gt; sed -e 's/pattern/change/g' &lt;INPUT-FILENAME&gt; | cat - &lt;INPUT-FILENAME&gt;&lt;BR /&gt;&lt;BR /&gt;  And more we can do pattern change on mulitple files and save updates on the same file as,&lt;BR /&gt;&lt;BR /&gt;  perl -p -i -e 's/&lt;PATTERN&gt;/&lt;CHANGE&gt;/g' &lt;FIELNAME&gt;&lt;BR /&gt;&lt;BR /&gt; It will update automatically.&lt;BR /&gt;&lt;BR /&gt;And shetty when you are processing html files there will be change to use / \ etc there.. &lt;BR /&gt;&lt;BR /&gt; We can use % or @ or ; or : as demilter to / there.&lt;BR /&gt;&lt;BR /&gt; In this example we can do easily as,&lt;BR /&gt;&lt;BR /&gt; echo "Enter your own replace"&lt;BR /&gt; read replace&lt;BR /&gt;&lt;BR /&gt; perl -p -i -e 's%mytext%'$replace'%g' &lt;FILES&gt;&lt;BR /&gt;&lt;BR /&gt; It will update automatically there.&lt;BR /&gt;&lt;/FILES&gt;&lt;/FIELNAME&gt;&lt;/CHANGE&gt;&lt;/PATTERN&gt;&lt;/INPUT-FILENAME&gt;&lt;/INPUT-FILENAME&gt;&lt;/INPUT-FILENAME&gt;&lt;/INPUT-FILENAME&gt;</description>
      <pubDate>Fri, 24 Sep 2004 08:43:41 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-replace-string-in-a-particular-line-using-shell-script/m-p/3319065#M12954</guid>
      <dc:creator>Muthukumar_5</dc:creator>
      <dc:date>2004-09-24T08:43:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to Replace string in a particular line using shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-replace-string-in-a-particular-line-using-shell-script/m-p/3319066#M12955</link>
      <description>sh replace.sh source_string destination_string On_which_file&lt;BR /&gt;&lt;BR /&gt;replace.sh&lt;BR /&gt;-----------&lt;BR /&gt;#!/bin/bash&lt;BR /&gt;TEMPFILE=/tmp/replace.tmp&lt;BR /&gt;if test $# -lt 3; then&lt;BR /&gt;        echo Please Follow the format sh scriptname srcstr deststr filename&lt;BR /&gt;        exit 1&lt;BR /&gt;fi&lt;BR /&gt;srcstr=$1 #The first argument as source string&lt;BR /&gt;deststr=$2 #The second argument as destination string&lt;BR /&gt;shift 2&lt;BR /&gt;for file ; do&lt;BR /&gt;        occurrence=`grep $srcstr $file | wc -l | sed -e "s/ //g"`&lt;BR /&gt;        if test $nummatches -eq 0; then&lt;BR /&gt;                echo $file: 0 occurrences&lt;BR /&gt;        else&lt;BR /&gt;                echo $file: replacing $occurrence occurrences&lt;BR /&gt;                sed -e "s+$srcstr+$deststr+g" $file &amp;gt; $TEMPFILE&lt;BR /&gt;                mv $TEMPFILE $file&lt;BR /&gt;        fi&lt;BR /&gt;done</description>
      <pubDate>Thu, 14 Sep 2006 05:34:19 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-replace-string-in-a-particular-line-using-shell-script/m-p/3319066#M12955</guid>
      <dc:creator>arun_swain2000</dc:creator>
      <dc:date>2006-09-14T05:34:19Z</dc:date>
    </item>
  </channel>
</rss>

