Can any one tell me how to replace a shebang line of a file using sed?
Eg: If a file contains the following shebang line
#!C:/InstantRails/ruby/bin/ruby
I would like to replace it with
#!/usr/local/bin/ruby
The shebang line of the file can be obtained from the command
cat <filename> | head -1
and the replacing tag from the command "which ruby", since the shebang line contains many special characters, i guess the sed command is not working properly. Can anyone help me to replace the shebang line of a file using the
output of "which ruby"
#!/bin/ksh
echo '#!C:/InstantRails/ruby/bin/ruby' | sed "s%#\!C:/InstantRails/ruby/bin/ruby%#\!$(which ruby)%"
Thank you for your prompt reply and it helps me to proceed further. But still I am missing some thing.
Output of the command " sed "s%$(head -1 file1.txt)%#\!$(which ruby)%g" file1.txt " is displaying the modified shebang line along with the contents of the file on the console. But if I pass the o/p using tee operator to the same file the whole contents of the file has been lost.
command:
sed "s%$(head -1 file1.txt)%#\!$(which ruby)%g" file1.txt | tee file1.txt
So I have found a work around to pass the o/p of the command "sed "s%$(head -1 file1.txt)%#\!$(which ruby)%g" file1.txt" to a temparary file and then copying the contents of the temparary file to the original.
command:
sed "s%$(head -1 file1.txt)%#\!$(which ruby)%g" file1.txt | tee file2.txt
cp file2.txt file1.txt
Is there any way I can accomplish the whole task from a single sed command?
{ rm file1.txt; sed "s%$(head -1 file1.txt)%#\!$(which ruby)%g" > file1.txt; } < file1.txt