LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 06-10-2008, 11:42 PM   #1
icecoolcorey
LQ Newbie
 
Registered: Jun 2008
Posts: 19

Rep: Reputation: 0
Post Trying to change String using sed with a string \/home\/user\/Desktop


I am currently working with a script in a simple terminal using a bash shell. I am working on this file and I have to alter the
VAR=/home/user/Desktop
To
VAR=/home/user2/Desktop.
I have been using sed to change the whole line using
echo | cat filename | sed ‘/^VAR/g’ > fileName
then I replace this with
echo “VAR=”$VAR >> fileName.

Now the problem I have is when I use the
read AN
it should read something like this (/home/user2/Desktop)
I am trying to use this variable to change The VAR=/home/user/Desktop in a file with the new variable $AN(/home/user2/Desktop) by using the sed command.

Read AN
CHANGE= echo $AN | sed s:/:\\\\\\/:g
This will change the /home/user2/Desktop that the user entered to
\/home\/user2\/Desktop (echo out $CHANGE)
Now for the tricky part, I am trying to use this new string to change only part of the VAR=* using sed.
So it would look something like this.
echo | cat fileName | sed s/$CHANGE/newstring0rBlank/g > fileName

any help would be grate. E-mail me if you post please.

Read AN
CHANGE= echo $AN | sed s:/:\\\\\\/:g
echo | cat fileName | sed s/$CHANGE/newstring0rBlank/g > fileName

Last edited by icecoolcorey; 06-11-2008 at 01:11 AM. Reason: e-mail
 
Old 06-11-2008, 12:08 AM   #2
icecoolcorey
LQ Newbie
 
Registered: Jun 2008
Posts: 19

Original Poster
Rep: Reputation: 0
I don’t know if this is the best way to go about changing the variable. I though that because sed needed to have the escape (\) before looking for metacharacters. So if the new string is \/home\/user2\/Desktop then It would work, I thought.
 
Old 06-11-2008, 12:15 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I am finding this very difficult to follow. I'll just point out a couple of things:

Quote:
echo | cat filename | sed ‘/^VAR/g’ > fileName
I can't see how the SED statement here can do anything useful. (If the line begins with "VAR", copy hold buffer to pattern buffer??) What is it supposed to do?

Quote:
CHANGE= echo $AN | sed s:/:\\\\\\/:g
It looks like you intend to replace all occurences of "/" with "\\\/", and then assign the result to the variable "CHANGE". If so, you need one of these constructs:
CHANGE=`echo......` (Those are backtics, not single quotes.)
OR
CHANGE=$(echo......)
Note no spaces around the "=" in either case

I suggest that you post something a bit more succinct which tells us which commands are doing what you want, and which don't.

Quote:
E-mail me if you could.
NO--that's not how a forum like this works. The questions and answers belong to all members.

Last edited by pixellany; 06-11-2008 at 12:18 AM.
 
Old 06-11-2008, 12:28 AM   #4
icecoolcorey
LQ Newbie
 
Registered: Jun 2008
Posts: 19

Original Poster
Rep: Reputation: 0
I am trying to make a path changing script that will work with the /etc/profile file in a bash shell. I have been able to add files to the path by appending PATH=$PATH:$FILE
if [ $WHATTODO = "a" ]
then
echo "What file would you like to add to your path?(used full path)"
read FILE

echo | cat /etc/profile | sed -e /^PATH/g > /etc/profile
echo "PATH="$PT:$FILE >> /etc/profile
fi
I also want the user to be able to delete files from the path. something like this.
echo -e "What file would you like to remove? (use full path)\n -->"
read DEL
echo -e "Are you sere you want to remove $DEL"
read AN
if [ $AN = "y" ]
then

CHANGE= echo $DEL | sed s:/:\\\\\\\/:g
echo $CHANGE
echo $DEL | sed s:/:\\\\\\\/:g > testfile


else
fi
I don't know if this help you understand. I am not vary good at getting my point a-crossed.

Last edited by icecoolcorey; 06-11-2008 at 12:30 AM.
 
Old 06-11-2008, 12:37 AM   #5
icecoolcorey
LQ Newbie
 
Registered: Jun 2008
Posts: 19

Original Poster
Rep: Reputation: 0
I understand, I didn't mean e-mail me the answers or your advise. Just an e-mail to let me know you posted so I could read it.

Last edited by icecoolcorey; 06-11-2008 at 12:41 AM.
 
Old 06-11-2008, 12:38 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Again, please tell us which command is doing what you want, and which command is not.

The approach I would recommend is to try the commands one at a time to be sure you know how they work. Then build the script.

What book are you using? I recommend starting with "Bash Guide for Beginners" by Machtelt Garrels. free at tldp.org
 
Old 06-11-2008, 12:56 AM   #7
icecoolcorey
LQ Newbie
 
Registered: Jun 2008
Posts: 19

Original Poster
Rep: Reputation: 0
I am using linux Guide to linux certification. I am 3 mounts along with linux.)(a noob)

I think the command I need is one that changes the $VAR that = /home/user2/Dektop to \/home\/user2\/Desktop so i can use it with sed.
like this sed 's/\/home\/user2\/Desktop/g'
I am trying to change /home/user/Desktop/ in

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:
/home/user:/root/bin:/home/user/Desktop:/root/bin:

to nothing, leaving me with
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/root/bin:

I don't know if I am going at this the right way, to change the file I use the
echo | cat /etc/profile | sed s/\/home\/user2\/Desktop/g > /etc/profile

does this help.

The main thing I need, is to use sed (or something) to change the backslashes from the entered file path to be able to use the file path entered in sed or awk ,as a changing Variable. (meaning every time the user types in a file path to remove.)

Last edited by icecoolcorey; 06-11-2008 at 01:42 AM. Reason: added
 
Old 06-11-2008, 07:26 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I think you really need to work on commands one at a time. Be sure you understand how each command works before trying to write a script.

First, you don't need to modify a variable "so I can use it with SED". Just make the SED statement do what you want directly. In your example, you want to remove something from the PATH variable which appears in /etc/profile. Let's start with a simple example. To remove all of the entries "/root/bin", do this:

sed -i '/^PATH/s%/root/bin:%%g' /etc/profile

The -i flag tells sed to edit the file "in place". Note that you don't need to "cat" the file and then pass it to sed. Note that I used "%" as the delimiter, since "/" appears in the actual text to be changed. The logic of this sed construct is:
find all lines beginning with "PATH", and replace all occurrences of /root/bin with nothing. Note also the added ":"

Now, suppose you want to use the content of a variable to determine what to remove:

var="/root/bin"
sed -i "/^PATH/s%$var:%%g" /etc/profile

Note that I changed the single quote to a double. This is necessary to allow "$var" to be expanded before sed executes.

In addition to the text I suggested, go here for a very good SED tutorial: http://www.grymoire.com/Unix/Sed.html
 
Old 06-11-2008, 07:54 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by icecoolcorey View Post
I am currently working with a script in a simple terminal using a bash shell. I am working on this file and I have to alter the
VAR=/home/user/Desktop
To
VAR=/home/user2/Desktop.
I have been using sed to change the whole line using
echo | cat filename | sed ‘/^VAR/g’ > fileName
then I replace this with
echo “VAR=”$VAR >> fileName.

you have structured data with visible delimiters, use awk instead. you don't have to worry too much on escaping backslashes and stuffs.
Code:
# s="VAR=/home/user/Desktop"
# echo $s | awk -F= '{$2="/home/user2/Desktop";print}' OFS="="
VAR=/home/user2/Desktop
 
Old 06-11-2008, 09:46 PM   #10
icecoolcorey
LQ Newbie
 
Registered: Jun 2008
Posts: 19

Original Poster
Rep: Reputation: 0
Well pixellany your advise worked. I just had to change it a little because it’s in a script file. Thanks for your patience!!! and all the writing you did.

read VAR
#(user enters)/home/sbin
#VAR is = to /home/sbin
echo | cat /etc/profile | sed “/^PATH/s%$VAR%%g” > /etc/profile
(THE –i didn’t work right.)

If you fill like one more question? I was wondering how to execute the /etc/profile without having the user exit.
 
Old 06-12-2008, 11:32 PM   #11
icecoolcorey
LQ Newbie
 
Registered: Jun 2008
Posts: 19

Original Poster
Rep: Reputation: 0
chpath.tar.gz
PATH Changer. Add or remove path with a bash shell. fedora 8 distribution

Thanks for all your help!!!

Last edited by icecoolcorey; 09-22-2009 at 08:41 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 replace string octeto Programming 4 06-06-2007 02:09 AM
Remove string in sed twantrd Programming 7 09-13-2006 02:28 PM
How can I replace this string with another using sed? dave4545 Programming 7 01-27-2006 10:58 AM
insert string with sed greg108 Programming 7 02-18-2005 01:11 PM
[sed] replace string? chuanyung Programming 3 03-11-2004 08:42 PM

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

All times are GMT -5. The time now is 12:22 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