943,776 Members | Top Members by Rank

Ad:
Jan 5th, 2009
0

Editing files using SED

Expand Post »
I want to replace a text in a file eg (old_text) with another text eg (new_text) . new_text is a variable in my shell script. few methods that i tried ..
but this doesnt work
cat $line".sh" sed -e 's/abc/ABC/g' $line".sh"

sed 's/ordprg/new_string/g' $line".sh" > $line".sh"

Could some one can help me on this?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jinsonsani is offline Offline
3 posts
since Jan 2009
Jan 5th, 2009
0

Re: Editing files using SED

in the first attempt, you need to add a pipe ('|') between the cat command and the sed command
Moderator
Featured Poster
Reputation Points: 183
Solved Threads: 89
Posting Virtuoso
DimaYasny is offline Offline
1,772 posts
since Jan 2007
Jan 5th, 2009
0

Re: Editing files using SED

Click to Expand / Collapse  Quote originally posted by jinsonsani ...
I want to replace a text in a file eg (old_text) with another text eg (new_text) . new_text is a variable in my shell script. few methods that i tried ..
but this doesnt work
cat $line".sh" sed -e 's/abc/ABC/g' $line".sh"

sed 's/ordprg/new_string/g' $line".sh" > $line".sh"

Could some one can help me on this?
You don't need cat to display the content of `$line".sh"' to sed, in order to substitute some text.

sed 's/ordprg/new_string/g' $line".sh" > $line".sh" redirecting to the same file you have opened, is the best way of corrupting your file or at best getting a empty file.
You need to redirect the output to another file and then rename it to your original filename. If you use the GNU sed, the -i option will do that for you automatically behind the scene.

sed 's/old_string/new_string/g' "$filename" > temp_file After that rename: mv temp_file "$filename"
Last edited by Aia; Jan 5th, 2009 at 7:50 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jan 5th, 2009
0

Re: Editing files using SED

Hey There,

Just a suggestion. If you're using Gnu Sed, you can use the -i option and it will do the changes inline (basically taking care of writing to a tmp file and then copying back, which Aia had suggested. As Aia noted, it's never a good idea to overwrite your original file with output from that same file)

That would boil your command line down to:

Quote ...
sed -i 's/old_string/new_string/g' "$filename"
That makes things easier to type Even so, whether you have the option or not, redirecting your sed output to a file and then copying back is the best method. Especially if you're not sure of the outcome, in which case you should just write your statement and see what you see on STDOUT or redirect into a temp file and look it over to see if it's doing exactly what you want/need.

Best wishes,

Mike
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007
Jan 6th, 2009
0

Re: Editing files using SED

Thanks a lot. temp_file works fine. but now i have a different problem . when i read a text from a file and create a new file using that text two small letters disappearing from the text and that leads to an invalid file path . two letters are "e"and "n". while reading from the file it fails to read these two letters . could you please tell me possible reason for this . is it taking those letters as some delimiter or something ?

Pre requesties

1)template.txt in the same path contains a text "ordprg"and a blank line

2)filenames.sh in the same path contains two lines
"ABCDEFJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrtuvwxyz"
Following program will eliminate "e" and "n"


#My Shell script is

Shell Scripting Syntax (Toggle Plain Text)
  1. processLine()
  2. {
  3. line="$@" # get all args
  4.  
  5. # F1=$(echo $line | awk '{ print $1 }')
  6.  
  7. #echo $line
  8. #touch $line".sh"
  9. #echo $line >> $line".sh"
  10.  
  11. #echo "date_stamp="'`'"date +%Y%m%d.%H%M%S"'`'" # used to date stamp all files for this run" >> $line".sh"
  12.  
  13. #fname="/home/jtest/"$line".sh"
  14. #fname="./"$line".sh"
  15.  
  16. #echo $fname
  17. #pname ="cp /home/jtest/test.txt /home/rms_user/jtest/$fname
  18. #cp /home/jtest/test.txt $fname
  19. #echo fname
  20.  
  21. }
  22.  
  23. ### Main script stars here ###
  24. # Store file name
  25. FILE=""
  26.  
  27. # Make sure we get file name as command line argument
  28. # Else read it from standard input device
  29.  
  30. if [ "$1" == "" ]; then
  31. FILE="/home/jtest/filenames.sh"
  32. #echo "k.txt"
  33. else
  34. FILE="$1"
  35. #echo "1k.txt"
  36. # make sure file exist and readable
  37. if [ ! -f $FILE ]; then
  38. #echo "2k.txt"
  39. echo "$FILE : does not exists"
  40. exit 1
  41. elif [ ! -r $FILE ]; then
  42. #echo "3k.txt"
  43. echo "$FILE: can not read"
  44. exit 2
  45. fi
  46. fi
  47. # read $FILE using the file descriptors
  48.  
  49. # Set loop separator to end of line
  50. BAKIFS=$IFS
  51. echo $IFS
  52. IFS=$(echo -en "\n\b")
  53. exec 3<&0
  54. exec 0<$FILE
  55. while read line
  56. do
  57. # use $line variable to process line in processLine() function
  58. processLine $line
  59. fname="./"$line".sh"
  60.  
  61. #pnmae="/home/jtest/"
  62. #echo $pname
  63.  
  64. echo $fname
  65.  
  66. cp /home/jtest/template.txt $fname
  67.  
  68. #echo /home/jtest/template.txt $fname
  69.  
  70.  
  71. sed 's/ordprg/'$line".sh"'/g' $line".sh" > temp_file
  72.  
  73. mv temp_file $line".sh"
  74.  
  75. #echo "#filename" $line".sh" >> $line".sh"
  76.  
  77. done
  78. exec 0<&3
  79.  
  80. # restore $IFS which was used to determine what the field separators are
  81. BAKIFS=$ORIGIFS
  82. exit 0
  83.  

# Please Respond to this Thanks in Advance
Last edited by peter_budo; Dec 21st, 2009 at 5:28 am. Reason: Adding code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jinsonsani is offline Offline
3 posts
since Jan 2009
Jan 6th, 2009
0

Re: Editing files using SED

Hey There,

I think your problem has to do with the "echo" that you're using (shell built-in vs. the binary). I'd guess that on this line, the echo doesn't accept the -e and -n arguments and is using them, literally, as IFS characters, which would result in your losing them:

IFS=$(echo -en "\n\b")

Hope that helps, and best wishes,

Mike
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007
Jan 7th, 2009
0

Re: Editing files using SED

Thats Great. I am obliged to you . This code works fine . I eliminated -en . hope it will not be harmful for me in the future.
RGDS
jinsonsanik@yahoo.com

Click to Expand / Collapse  Quote originally posted by eggi ...
Hey There,

I think your problem has to do with the "echo" that you're using (shell built-in vs. the binary). I'd guess that on this line, the echo doesn't accept the -e and -n arguments and is using them, literally, as IFS characters, which would result in your losing them:

IFS=$(echo -en "\n\b")

Hope that helps, and best wishes,

Mike
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jinsonsani is offline Offline
3 posts
since Jan 2009
Jan 7th, 2009
0

Re: Editing files using SED

Great news!

Glad I could help you out

Best wishes,

Mike
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Appending to a variable
Next Thread in Shell Scripting Forum Timeline: Comparing two folder





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


Follow us on Twitter


© 2011 DaniWeb® LLC