944,133 Members | Top Members by Rank

Ad:
Nov 9th, 2009
0

file replacement not working!

Expand Post »
Hi Team,

I am stuck in a very tight situation.. need your help

The below script does not work even please let me know where is the problem and rectify me.
Script as below:
replace.sh

Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/bash
  2. file_name=$1
  3. old_templet=$2
  4. new_templet=$3
  5.  
  6. for file in file_name
  7. do
  8. cat $file | sed "s/old_templet/new_templet/g" > $file.new
  9. done

1)Here the file_name stands for file name
2)old_templet stands for old word to change
3)new_templet stands for new word

But when the script it gives me the below error:
./replace.sh one single oracle
cat: file_name: No such file or directory

Thanks for your patience..
hope 4 a speedy reply

cheers!
Whizkidash
Last edited by peter_budo; Nov 9th, 2009 at 5:34 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
whizkidash is offline Offline
20 posts
since Nov 2009
Nov 9th, 2009
1
Re: file replacement not working!
$file_name, not file_name
ditto for the other variables
use "sed -e" not just "sed"
and change $file.new to ${file}.new just to be safe

Edit: So many problems, so little text. ;-)
Last edited by masijade; Nov 9th, 2009 at 3:20 am.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Nov 9th, 2009
1
Re: file replacement not working!
Masijade answered your question but i'd like to throw something else in there. You should add checks for your arguments on the command line and verify the file exists:
Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/bash
  2. file_name=$1
  3. old_templet=$2
  4. new_templet=$3
  5.  
  6. if [ "$3" = "" ]; then
  7. echo "usage: ${0} <file_name> <new_templet> <old_templet>"
  8. exit 1
  9. fi
  10.  
  11. if ! test -f ./${file_name}; then
  12. echo "file not found"
  13. exit 1
  14. fi
  15.  
  16. if test -f ./${file_name}.new; then
  17. rm ./${file_name}.new
  18. fi
  19.  
  20. sed "s/${old_templet}/${new_templet}/g" ${file_name} >> ${file_name}.new
Last edited by sknake; Nov 9th, 2009 at 5:17 am.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Shell script to append text file with today's date.
Next Thread in Shell Scripting Forum Timeline: OMG... simple question driving me nuts





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC