sed and escaping & in something like: echo $y | sed 's/&/_/g'
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.
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.
By your first example, sed is removing the & just fine. In the second example, you only asked it to remove the b so the & is not going to be removed in that case.
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
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.
#!/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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.