Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Find and replace using sed command

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-20-2013
kmanivan82 kmanivan82 is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 3 May 2017, 11:17 AM EDT
Location: Chennai, INDIA
Posts: 76
Thanks: 14
Thanked 0 Times in 0 Posts
Find and replace using sed command

The content of the file filea.txt is as follows.
---------

Code:
case $HOSTNAME in
        aaa)
                DS_PARM_VALUE_SET=vsDev
                APT_Configuration_File=/appl/infoserver/Server/Configurations/2node.apt

                ;;
        bbb)
                DS_PARM_VALUE_SET=vsQA
                APT_Configuration_File=/appl/infoserver/Server/Configurations/2node.apt

                ;;
        ccc)
                DS_PARM_VALUE_SET=vsProd
                APT_Configuration_File=/appl/infoserver/Server/Configurations/2node.apt

                ;;
        *)
                DS_PARM_VALUE_SET=vsDev
                APT_Configurations_File=/appl/infoserver/Server/Configuarations/2node.apt

                ;;
esac

---------

Now aaa has to be changed to 'DEV', bbb has to be changed to 'QA' and ccc has to be changed to 'PROD'.

The new file should be as follows.
---------

Code:
case $HOSTNAME in
        'DEV')
                DS_PARM_VALUE_SET=vsDev
                APT_Configuration_File=/appl/infoserver/Server/Configurations/2node.apt

                ;;
        'QA')
                DS_PARM_VALUE_SET=vsQA
                APT_Configuration_File=/appl/infoserver/Server/Configurations/2node.apt

                ;;
        'PROD')
                DS_PARM_VALUE_SET=vsProd
                APT_Configuration_File=/appl/infoserver/Server/Configurations/2node.apt

                ;;
        *)
                DS_PARM_VALUE_SET=vsDev
                APT_Configurations_File=/appl/infoserver/Server/Configuarations/2node.apt

                ;;
esac

---------
I have tried the following command to perform this. But it did not work.

---------

Code:
find ${Appl_Path} -type f -name '*.properties' -print | while read i
do
    sed -e 's/aaa/\'DEV\'/g' $i > $i.tmp && mv $i.tmp $i
    sed -e 's/bbb/\'QA\'/g' $i > $i.tmp && mv $i.tmp $i
    sed -e 's/ccc/\'PROD\'/g' $i > $i.tmp && mv $i.tmp $i
done

---------


Can anyone help me to fix this issue?

Thanks
Krishnakanth Manivannan

Last edited by Franklin52; 02-21-2013 at 02:27 AM.. Reason: Please use code tags for data and code samples
Sponsored Links
    #2  
Old Unix and Linux 02-20-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 28 June 2017, 1:54 PM EDT
Location: Galactic Empire
Posts: 3,544
Thanks: 251
Thanked 1,288 Times in 1,210 Posts
Please use code tags around code fragments or data samples

Try this awk code:

Code:
awk '   /aaa/ {
                sub("aaa","'\''DEV'\''");
}       /bbb/ {
                sub("bbb","'\''QA'\''");
}       /ccc/ {
                sub("ccc","'\''PROD'\''");
}1' input_file > output_file

Sponsored Links
    #3  
Old Unix and Linux 02-20-2013
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 1 February 2016, 3:35 PM EST
Location: Southern NJ, USA (Nord)
Posts: 4,673
Thanks: 8
Thanked 587 Times in 560 Posts
The gnu sed has -i for update in place. You can just say:
Code:
find ${Appl_Path} -type f -name '*.properties' | while read i
 do
  sed -i '
    s/aaa/'"'"'DEV'"'"'/g
    s/bbb/'"'"'QA'"'"'/g
    s/ccc/'"'"'PROD'"'"'/g
   ' $i
 done

Nothing is meta in ' land, so leave for " land; a single quote in a single quoted string is single-double-single-double-single; I guess I just don't like barefoot \'. Single quotes saves the shell and unwary programmer from any interpretation. Also go to " land for $variables, then come back. Flie globbing is barefoot time.

You, too, deserve nicely indented code where the sed-isms are on different lines than the shell-isms. It prevents bugs and makes maintenance more error-free.

Last edited by DGPickett; 02-20-2013 at 03:15 PM..
    #4  
Old Unix and Linux 02-20-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 28 June 2017, 1:54 PM EDT
Location: Galactic Empire
Posts: 3,544
Thanks: 251
Thanked 1,288 Times in 1,210 Posts
Using sed

Code:
sed 's/aaa/'\''DEV'\''/; s/bbb/'\''QA'\''/; s/ccc/'\''PROD'\''/' input_file

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed command to skip the first line during find and replace operation ks_reddy Shell Programming and Scripting 4 02-17-2013 07:24 AM
sed find and replace command not working Sara183 Shell Programming and Scripting 9 12-06-2012 04:07 PM
sed command to find and replace anand.shah Shell Programming and Scripting 1 11-21-2012 12:23 AM
Sed command to find, manipulate and replace a number LT_2008 Shell Programming and Scripting 6 11-13-2008 10:28 AM
how to use sed or perl command to find and replace a directory in a file wip_vasikaran UNIX for Dummies Questions & Answers 1 08-20-2008 06:22 AM



All times are GMT -4. The time now is 06:39 PM.