954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Sed or Perl Help

Hey guys I'm stuck. I have been trying to write a sed or perl script to change the below. But each time I run the sed script or perl it completely zeros out the file.

I want to change this
/opt/dragon/bin/runit

to this
cd /opt/dragon/bin; /opt/dragon/bin/runit


#!/bin/bash
for fl in *.php; do
mv $fl $fl.old
sed 's/old/new/g' $fl.old > $fl
rm -f $fl.old
done

dozierc
Newbie Poster
2 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Your sed line is saying to substitute the word new for the word old globally in the filename.old file and output to the original file name. You would need something like this:

sed s/\/opt\/dragon\/bin\/runit/cd\ \/opt\/dragon\/bin\ \;\ \/opt\/dragon\/bin\/runit/g


But this would do the same thing:

sed s/\/opt\/dragon\/bin\/runit/cd\ \/opt\/dragon\/bin\ \;\ \/\.\/runit/g


You have to use the backslash to tell sed to ignore the normal operation of the slash, space, semi-colon and period and make them part of the substitution. The second line changes to the directory then starting in the current directory run the program called runit.

rch1231
Posting Shark
959 posts since Sep 2009
Reputation Points: 119
Solved Threads: 142
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You