Sed command in shell script | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Sed command in shell script

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-05-2008
Todd88 Todd88 is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 5 July 2008, 11:55 AM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Sed command in shell script

I have a current code working(named subst1) having a user be able to type this line to substitute words using the sed command:


Code:
subst1 old-pattern new-pattern filename

Here is my shell script:


Code:
#!/bin/bash
# subst1

ARGS=3
E_BADARGS=65

if [ $# -ne "$ARGS" ]
then
  echo "Usage: `basename $0` old-pattern new-pattern filename"
  exit $E_BADARGS
fi

old_pattern=$1
new_pattern=$2

if [ -f "$3" ]
then
    file_name=$3
else
    echo "File \"$3\" does not exist."
    exit $E_BADARGS
fi

mv $file_name $file_name.bak
sed -e "s/$old_pattern/$new_pattern/g" $file_name.bak > $file_name

exit 0

The problem with this script is that it changes the date and creates a bak file. I need to alter this script so that when a user inputs the command line, the script checks if the given old-pattern is in the file and if it is, go ahead and do the substitution. If the old-pattern is not found, the script should do nothing. Any help would be appreciated!
Sponsored Links
    #2  
Old 07-05-2008
stevie_velvet stevie_velvet is offline
Registered User
 
Join Date: Jan 2008
Last Activity: 20 May 2012, 4:08 PM EDT
Location: London e Milano
Posts: 89
Thanks: 0
Thanked 0 Times in 0 Posts
So before your sed, you need to test if the string exists in the file, something like


grep -q "String" <filename> >& /dev/null

if ($status == 0) then
echo "String exist, do something"
else
echo "do something else or exit" >> <filename>
endif


syntax neeeds clearing up bu the skeletal is there & a more elegant 1 line job (.e.e. perl) may be around too
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell script running command in different shell gr8_usk Shell Programming and Scripting 9 05-23-2011 06:30 PM
help with shell script: cp command not working, but mv command works... udelalv Shell Programming and Scripting 1 04-12-2011 04:34 AM
can anyone help with shell script command about searching word with grep command? aintour Shell Programming and Scripting 2 10-14-2009 04:51 PM
Need to Write Shell Script based off of this shell command Rally_Point Shell Programming and Scripting 3 06-10-2009 05:19 PM
how to use cd command in shell script LAKSHMI NARAYAN Shell Programming and Scripting 3 09-09-2007 06:04 AM



All times are GMT -4. The time now is 10:09 PM.