sed and escaping & in something like: echo $y | sed 's/&/_/g'
tempa=$(echo $y | sed 's/&/_/g')
here, I am trying to replace all instances of the character "&" with "_" I've tried: tempa=$(echo $y | sed 's/\&/_/g') and tempa=$(echo $y | sed 's/"&"/_/g') and a few other random shots in the dark. I can't seem to escape the & Thanks in advance. paul |
Personally, I found I had to escape the & just to be able to assign it, after that it worked:
t1=xx\&yy echo $t1|sed 's/&/_/' xx_yy echo $t1 xx&yy If I didn't escape it during the assignment this happens: t1=xx&yy [1] 9893 bash: yy: command not found [1]+ Done t1=xx HTH |
thanks so much....
and my appologies for maybe not explaining this well enough I am getting my variable $y from files in a directory... like this: cd /somedirectory for * in y so I'm not sure how to escape the character in the declaration... where I am looping through all files in somedirectory that may be poorly named. I am using this to easily replace spaces with underscores, but I cannot replace the "&" symbol. What I have are filenames that are like this: somefile name apples & oranges and I want to use sed to rename those files: somefilename apples_and_oranges thanks again, Paul |
This might do for the & part...
Code:
#!/bin/bash |
yes, that's what I'm doing.
The problem is, the "&" is special to the sed function and I can't escape it. For any other character, what you suggest works. See this: # echo "bubb&les" | sed -e 's/&/_/g' bubb_les # echo "bubb&les" | sed -e 's/b/_/g' _u__&les so it just ignores the "&" ...yea, still stuck on this one. |
Quote:
|
So you want to replace spaces and '&' chars: to do what you want, you need a separate sed cmd for each char you want to replace becuase you are replacing them with different replacement chars.
Like this: t1='apples & oranges' t2=`echo $t1|sed 's/&/and/'` echo $t2 apples and oranges t3=`echo $t2|sed 's/ /_/g'` echo $t3 apples_and_oranges :) |
thanks to everyone for the help.
here's one for someone else who wants to do this later. Could be done more efficently I'm sure, but this seems to work great. This bash script replaces spaces with underscores and removes the '&' character from directories and files recursively from the current directory down. Example: # find ./ ./blah & blah1 ./blah & blah1/blah & blah1 ./blah & blah1/blah & blah1/blah & blah4 ./blah & blah1/blah & blah1/blah & blah3 ./blah & blah1/blah & blah1/blah & blah2 ./blah & blah1/blah & blah1/blah & blah2/bo & bo1 ./blah & blah1/blah & blah1/blah & blah2/bo & bo2 ./blah & blah1/blah & blah1/blah & blah2/bo & bo3 ./blah & blah1/blah & blah1/blah & blah1 ./blah & blah1/blah & blah2 ./blah & blah1/blah & blah3 ./blah & blah1/blah & blah4 # sr # ls blah_blah1 # find ./ ./blah_blah1 ./blah_blah1/blah_blah2 ./blah_blah1/blah_blah3 ./blah_blah1/blah_blah4 ./blah_blah1/blah_blah1 ./blah_blah1/blah_blah1/blah_blah4 ./blah_blah1/blah_blah1/blah_blah3 ./blah_blah1/blah_blah1/blah_blah2 ./blah_blah1/blah_blah1/blah_blah2/bo_bo2 ./blah_blah1/blah_blah1/blah_blah2/bo_bo3 ./blah_blah1/blah_blah1/blah_blah2/bo_bo1 ./blah_blah1/blah_blah1/blah_blah1 the code: #!/bin/sh for i in * do if [ -d "$i" ] # if * is a directory then cd "$i" # descend into the directory for y in * do # tempa=$(echo $y | sed 's/ /_/g') tempa=$(echo $y | sed 's/ /_/g' | sed -e 's/\&//g' | sed 's/__/_/g') if [ "$y" != "$tempa" ] then mv "$y" "$tempa" fi if [ -d "$tempa" ] # if this is also a directory, call this program then cd "$tempa" sr; # this is the name of THIS program, must be in your PATH cd .. fi done cd .. fi # tempa=$(echo $i | sed 's/ /_/g') tempa=$(echo $i | sed 's/ /_/g' | sed -e 's/\&//g' | sed 's/__/_/g') if [ "$i" != "$tempa" ] then mv "$i" "$tempa" fi done -Paul Rennix |
All times are GMT -5. The time now is 03:35 PM. |