|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I need to create a shell script, that will search for a string and make a substitution and a backup file, but only if string is found in the file. Can anybody help me. So far I have tried:
#!/bin/csh set firstargument=$1 set secondargument=$2 set filename=$3 grep $firstargument $filename if (true)then sed "s/firstaurgument/secondargument/g" $filename.bak > $filename mv $filname $filename.bak endif -------------------------------------- #!/bin/csh set firstargument=$1 set second argument=$2 set filename=$3 if (grep $firstargument $filename)then mv $filename $filename.bak sed "s/firstargument/secondargument/g" $filename.bak > $filename else endif If you can oofer any help please do. |
|
#2
|
|||
|
|||
|
I prefer not to use csh, preferring a POSIX-compliant(ish) one, like bourne or korn.
Code:
#!/usr/bin/ksh firstargument=$1 secondargument=$2 filename=$3 found=`grep -c "$firstargument" $filename` if [ $found -ne 0 ] then sed "s/$firstaurgument/$secondargument/g" $filename.bak > $filename mv $filname $filename.bak fi All of which pre-supposes that you have a $filename.bak
__________________
"I feel so miserable without you; it's almost like having you here" - Stephen Bishop |
![]() |
| Viewing: Dev Shed Forums > Web Site Management > Scripts > Substition unix script help needed |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|