Pages

mardi 12 juillet 2011

use sed to replace characters in strings: csh

1)set a= 'la1_001 la1_002'
set output = `echo $a| sed 's/la1/ttla1/g' `

to replace all the "la1" in a into "ttla1".

2)set a= 'la1_001 la1_002'
set b = "ttla1"
set temp = "la1"
set output = `echo $a| sed "s/$temp/$b/g" `

to replace all the "la1" in a into "ttla1". But here the double quote is used in the sed to get the value of $temp and $b. if using the single quote, it will look for the strings 'temp' and replace with 'b', which finds nothing and then changes nothing to the output.

3) to change the path
set a= 'la1_001 la1_002'
set b = "tt/la1"
set temp = "la1"
set output = `echo $a| sed "s;$temp;$b;g" `

It can replace all the 'la1' in a into 'tt/la1'. Here the ";" are used to separate the strings in sed, because the separator "/" can confuse with the slash in the path string of "tt/la1". Otherwise it will cause the errors of 'unknown option to 's''. Be attention with the special simbols such as #, / etc for the replacement.

Perhaps set output = `echo $a| sed "s:$temp:$b:g" ` works as well. a post about this question is seen in http://dbaspot.com/shell/359294-replace-sed-command-print.html.


Note: sed "s/searching_pattern/replace_pattern/g/p":
g for all the occurances of the search pattern and changing them to the replace_pattern.
p for the printing.

6) we must have a "entre" made in the end of a script on tcsh !!!!
For example,

foreach f (`ls`)
echo $f
end
(then enter the "enter" key), so that the tcsh knows the end of the script. Otherwise, it would not work.


7) to set up alias in tcsh
alias newname oldname
If the old name contains the space, then use
alias newname 'ssh ddd@sss'
(Note, here use the single quote !!!)

2 commentaires: