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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
02-03-2005, 07:01 PM
|
#1
|
LQ Newbie
Registered: Feb 2005
Location: Boulder County, Colorado
Distribution: Centos5,6,7(production)/OSX(dev/personal laptop)
Posts: 11
Rep:
|
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
|
|
|
02-03-2005, 08:04 PM
|
#2
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,441
|
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
|
|
|
02-03-2005, 08:30 PM
|
#3
|
LQ Newbie
Registered: Feb 2005
Location: Boulder County, Colorado
Distribution: Centos5,6,7(production)/OSX(dev/personal laptop)
Posts: 11
Original Poster
Rep:
|
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
Last edited by prx; 02-03-2005 at 09:50 PM.
|
|
|
02-03-2005, 10:04 PM
|
#4
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
This might do for the & part...
Code:
#!/bin/bash
cd /home/images
for i in * ; do
j=`echo $i | sed -e 's/&/_/g'`
mv "$i" "$j"; done
|
|
|
02-03-2005, 10:18 PM
|
#5
|
LQ Newbie
Registered: Feb 2005
Location: Boulder County, Colorado
Distribution: Centos5,6,7(production)/OSX(dev/personal laptop)
Posts: 11
Original Poster
Rep:
|
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.
|
|
|
02-03-2005, 10:24 PM
|
#6
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
Quote:
# echo "bubb&les" | sed -e 's/&/_/g'
bubb_les
# echo "bubb&les" | sed -e 's/b/_/g'
_u__&les
so it just ignores the "&"
|
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.
|
|
|
02-03-2005, 10:40 PM
|
#7
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,441
|
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

|
|
|
02-03-2005, 11:00 PM
|
#8
|
LQ Newbie
Registered: Feb 2005
Location: Boulder County, Colorado
Distribution: Centos5,6,7(production)/OSX(dev/personal laptop)
Posts: 11
Original Poster
Rep:
|
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
Last edited by prx; 02-04-2005 at 01:52 AM.
|
|
|
All times are GMT -5. The time now is 07:51 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|