LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 02-03-2005, 07:01 PM   #1
prx
LQ Newbie
 
Registered: Feb 2005
Location: Boulder County, Colorado
Distribution: Centos5,6,7(production)/OSX(dev/personal laptop)
Posts: 11

Rep: Reputation: 0
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
 
Old 02-03-2005, 08:04 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
Old 02-03-2005, 08:30 PM   #3
prx
LQ Newbie
 
Registered: Feb 2005
Location: Boulder County, Colorado
Distribution: Centos5,6,7(production)/OSX(dev/personal laptop)
Posts: 11

Original Poster
Rep: Reputation: 0
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.
 
Old 02-03-2005, 10:04 PM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
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
 
Old 02-03-2005, 10:18 PM   #5
prx
LQ Newbie
 
Registered: Feb 2005
Location: Boulder County, Colorado
Distribution: Centos5,6,7(production)/OSX(dev/personal laptop)
Posts: 11

Original Poster
Rep: Reputation: 0
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.
 
Old 02-03-2005, 10:24 PM   #6
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
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.
 
Old 02-03-2005, 10:40 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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

 
Old 02-03-2005, 11:00 PM   #8
prx
LQ Newbie
 
Registered: Feb 2005
Location: Boulder County, Colorado
Distribution: Centos5,6,7(production)/OSX(dev/personal laptop)
Posts: 11

Original Poster
Rep: Reputation: 0
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed & Awk hinetvenkat Linux - Software 4 05-30-2005 05:10 AM
Sed Command & Passing Values joey52 Linux - Newbie 4 12-27-2004 07:46 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
Tutorial on sed & awk LinuxTiro Programming 4 11-10-2003 03:53 AM
sed & grep script? dolvmin Linux - Software 20 09-22-2003 06:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration