LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Trying to change String using sed with a string \/home\/user\/Desktop (https://www.linuxquestions.org/questions/programming-9/trying-to-change-string-using-sed-with-a-string-%5C-home%5C-user%5C-desktop-648472/)

icecoolcorey 06-10-2008 11:42 PM

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

icecoolcorey 06-11-2008 12:08 AM

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.

pixellany 06-11-2008 12:15 AM

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.

icecoolcorey 06-11-2008 12:28 AM

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.

icecoolcorey 06-11-2008 12:37 AM

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.

pixellany 06-11-2008 12:38 AM

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

icecoolcorey 06-11-2008 12:56 AM

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.)

pixellany 06-11-2008 07:26 AM

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

ghostdog74 06-11-2008 07:54 AM

Quote:

Originally Posted by icecoolcorey (Post 3181129)
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


icecoolcorey 06-11-2008 09:46 PM

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.

icecoolcorey 06-12-2008 11:32 PM

chpath.tar.gz
PATH Changer. Add or remove path with a bash shell. fedora 8 distribution

Thanks for all your help!!!


All times are GMT -5. The time now is 05:18 PM.