ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
ok, i am trying to write a script in tcsh that replaces like this
rep1 file word1 word2 [word1 word2 ...]
where it will replace word1 with word2 in the file named file.
i am using sed, but for some reason, it will not actually do any replacing... anyone see why? or have an easier way to do this?
Code:
#!/bin/tcsh
#check number of arguments
if ( $#argv < 3 ) then
echo Usage: rep1 file string1 string2 [string1 string2 ...]
echo Usage: where string1 is replaced by string2 in file
echo Warning: If there is an odd number of strings, the last will be cut off!
else
#argv[1] = file
#does the file exist?
if( -e $argv[1] ) then
#check if backup file exists
if ( -e $argv[1]_backup) then
echo "ERROR: backup file already exists ($argv[1]_backup)"
exit
endif
#create backup
#cp $argv[1] $argv[1]_backup
@ ptr = 2
@ ptr2 = 3
# $argv[$ptr] = word to search for
# $argv[$ptr + 1] = word to replace with
while ( $ptr < $#argv )
echo " sed -e 's/$argv[$ptr]/$argv[$ptr2]/g' $argv[1] "
sed -e 's/$argv[$ptr]/$argv[$ptr2]/g' $argv[1]
@ ptr += 2
@ ptr2 += 2
end
else
#file does not exist
echo "ERROR: File does not exist ($argv[1])"
exit
endif
endif
i get the same problem if i insert that instead of the 'sed' command. it doesnt do anything to the file. but if i do either of those commands from a shell they will work..
I don't have a PC here to test it on, but it could be the variables inside the single quotes aren't being expanded. Can you try double quotes in the sed command?
It's running on my PC under the following setup. I have a file called aaa which contains several outputs of the date command. I have a script which is basically the one you posted earlier but with double quotes instead of single quotes for the sed command:
Code:
#!/bin/tcsh
#
if ( $#argv < 3 ) then
echo Usage: rep1 file string1 string2 [string1 string2 ...]
echo Usage: where string1 is replaced by string2 in file
echo Warning: If there is an odd number of strings, the last will be cut off!
else
#argv[1] = file
#does the file exist?
if( -e $argv[1] ) then
#check if backup file exists
if ( -e $argv[1]_backup) then
echo "ERROR: backup file already exists ($argv[1]_backup)"
exit
endif
#create backup
#cp $argv[1] $argv[1]_backup
@ ptr = 2
@ ptr2 = 3
# $argv[$ptr] = word to search for
# $argv[$ptr + 1] = word to replace with
while ( $ptr < $#argv )
echo " sed -e s/$argv[$ptr]/$argv[$ptr2]/g $argv[1] "
sed -e "s/$argv[$ptr]/$argv[$ptr2]/g" $argv[1]
@ ptr += 2
@ ptr2 += 2
end
else
#file does not exist
echo "ERROR: File does not exist ($argv[1])"
exit
endif
endif
When I enter a tcsh shell (just in case you were using one as well as your script) and check the contents of aaa and then run the script, it does the substitution:
Code:
$ /bin/tcsh
> cat ./aaa
Sat Feb 18 04:40:46 EST 2006
Sat Feb 18 04:40:49 EST 2006
Sat Feb 18 04:40:49 EST 2006
Sat Feb 18 04:40:50 EST 2006
Sat Feb 18 04:40:50 EST 2006
Sat Feb 18 04:40:51 EST 2006
> ./runme aaa EST ABC
sed -e s/EST/ABC/g aaa
Sat Feb 18 04:40:46 ABC 2006
Sat Feb 18 04:40:49 ABC 2006
Sat Feb 18 04:40:49 ABC 2006
Sat Feb 18 04:40:50 ABC 2006
Sat Feb 18 04:40:50 ABC 2006
Sat Feb 18 04:40:51 ABC 2006
> exit
If it were a disk space problem you should get an IO error, if it were the permissions of the script, you should get permission denied. If I modify the name of the sed command to something else, I get Command not found.
Can you try it again with the full path to sed as the command (e.g. /bin/sed or /usr/bin/sed)?
the problem is that tcsh does not interperate anything in single quotes... the problem before was i was putting the whole command in double quotes (idiot)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.