LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 02-16-2006, 05:13 PM   #1
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Rep: Reputation: 30
tcsh script, replace word in file


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
 
Old 02-16-2006, 05:24 PM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
Code:
perl -pi.bak -e 's/THIS/THAT/g' -e 's/WHO/WHERE/g' file ...

Last edited by bigearsbilly; 02-16-2006 at 05:25 PM.
 
Old 02-16-2006, 06:44 PM   #3
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Original Poster
Rep: Reputation: 30
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..
 
Old 02-16-2006, 07:38 PM   #4
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 162Reputation: 162
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?
 
Old 02-17-2006, 04:03 AM   #5
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Original Poster
Rep: Reputation: 30
i get..


sed -e 's/1/2/g' aaa: Command not found.
 
Old 02-17-2006, 04:09 AM   #6
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 162Reputation: 162
So it's resolving:
Code:
sed -e 's/$argv[$ptr]/$argv[$ptr2]/g' $argv[1]
As
Code:
sed -e 's/1/2/g' aaa
And not finding the sed command. That's odd - have you tried it with the full path to sed?
 
Old 02-17-2006, 05:32 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
well, the variables won't get expanded because they are in single quotes.
's/$argv[$ptr]/$argv[$ptr2]/g'
use double quotes.

Code:
> set x=`date`
> echo $x
Fri Feb 17 10:30:58 GMT 2006
> echo '$x'
$x
google:"c shell considered harmful"



repeat ...
Code:
perl -pi.bak -e 's/THIS/THAT/g' -e 's/WHO/WHERE/g' file ...
 
Old 02-17-2006, 01:25 PM   #8
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Original Poster
Rep: Reputation: 30
its resolving the arguments all right, those are what i entered in the command line... its just not executing the command... if i try with perl, i get

perl -pi.bak 's/1/2/g' aaa: Command not found.

is there a specific way you have to execute a command in tcsh?

the weird thing is, it will create the backupfile, if i try it without quotes, but it will just not replace the words...

if this is not going to work, how do i read a file in tcsh? word for word?

Last edited by true_atlantis; 02-17-2006 at 01:33 PM.
 
Old 02-17-2006, 02:02 PM   #9
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 162Reputation: 162
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)?
 
Old 02-17-2006, 02:21 PM   #10
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Original Poster
Rep: Reputation: 30
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)
 
Old 02-17-2006, 02:24 PM   #11
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 162Reputation: 162
Well, if it's working now - that's a good thing
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help! Script or commanded needed to replace text in a file farmerjoe Programming 3 01-02-2005 06:59 PM
help! Script or command needed to replace text in a file. farmerjoe Linux - Newbie 2 01-02-2005 04:07 PM
Script to search and replace in text file - kinda... jeffreybluml Programming 45 11-07-2004 06:37 PM
Search and replace text in file using shell script? matthurne Linux - Software 2 11-02-2004 11:11 AM
How to replace a word within a file elegantly Linh Programming 3 10-23-2003 11:21 AM


All times are GMT -5. The time now is 04:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration