Question about using sed (solved)

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
stu90

Question about using sed (solved)

#1 Post by stu90 »

Hello,
I am requiring a little help with sed.

If have this file which i wish to run sed on:

Code: Select all

/usr
/usr/bin
 yad
/usr/share
/usr/share/pixmaps
 yad.png
sed -i '/\//s/^/\/root/'
This sed code seems to do what i require - which is to add /root only to the start of the lines that begin with a forward slash /

sed result:

Code: Select all

/root/usr
/root/usr/bin
 yad
/root/usr/share
/root/usr/share/pixmaps
 yad.png
I wish to move the forward slash and /root to variables and if possible mabe lose the back slash escape to make it easier to remember for future use - how would i format the sed string to accomplish this?

what i would like:

Code: Select all

FIND="/"
ADD="/root"
sed -i '/\$FIND/s/^/\$ADD/'
thanks.
Last edited by stu90 on Fri 04 Nov 2011, 17:42, edited 1 time in total.

User avatar
dejan555
Posts: 2798
Joined: Sun 30 Nov 2008, 11:57
Location: Montenegro
Contact:

#2 Post by dejan555 »

I've learnt that for separator with sed you can use anything you like so if you want to lose backslash use some other separator:

Code: Select all

sed -i 's:^/:/root/:'
puppy.b0x.me stuff mirrored [url=https://drive.google.com/open?id=0B_Mb589v0iCXNnhSZWRwd3R2UWs]HERE[/url] or [url=http://archive.org/details/Puppy_Linux_puppy.b0x.me_mirror]HERE[/url]

User avatar
dejan555
Posts: 2798
Joined: Sun 30 Nov 2008, 11:57
Location: Montenegro
Contact:

#3 Post by dejan555 »

And with variables it would go something like this, someone correct me if I'm wrong but I tested it and it works:

Code: Select all

FIND="/"
ADD="/root/"
sed -i 's:^'"$FIND"':'"$ADD"':' 
puppy.b0x.me stuff mirrored [url=https://drive.google.com/open?id=0B_Mb589v0iCXNnhSZWRwd3R2UWs]HERE[/url] or [url=http://archive.org/details/Puppy_Linux_puppy.b0x.me_mirror]HERE[/url]

stu90

#4 Post by stu90 »

dejan555 wrote:And with variables it would go something like this, someone correct me if I'm wrong but I tested it and it works:

Code: Select all

FIND="/"
ADD="/root/"
sed -i 's:^'"$FIND"':'"$ADD"':' 
Many thanks dejan555 - that is just what i am looking for! :)

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#5 Post by technosaurus »

If you have a variable, you don't even need sed many times

echo ${VAR//$OLD/$NEW}

Will echo the string $VAR and replace substring $OLD with substring $NEW
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
dejan555
Posts: 2798
Joined: Sun 30 Nov 2008, 11:57
Location: Montenegro
Contact:

#6 Post by dejan555 »

Good to know, but will this work with text files with multiple VAR instances? Can you use it with cat or something?
sed also has 'g' switch to replace globaly even if there are multiple instances on same line
puppy.b0x.me stuff mirrored [url=https://drive.google.com/open?id=0B_Mb589v0iCXNnhSZWRwd3R2UWs]HERE[/url] or [url=http://archive.org/details/Puppy_Linux_puppy.b0x.me_mirror]HERE[/url]

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#7 Post by seaside »

dejan555,

You can use bash like this-
Replace only first match-
${string/pattern/replacement}

Replace all matches-
${string//pattern/replacement}

Replace beginning-
${string/#pattern/replacement}

Replace end-
${string/%pattern/replacement}

Example:replace all patterns in file-
cat file | echo "${string//pattern/replacement}"
Sed has more "line power". :)

Regards,
s

User avatar
dejan555
Posts: 2798
Joined: Sun 30 Nov 2008, 11:57
Location: Montenegro
Contact:

#8 Post by dejan555 »

Thanks seaside
puppy.b0x.me stuff mirrored [url=https://drive.google.com/open?id=0B_Mb589v0iCXNnhSZWRwd3R2UWs]HERE[/url] or [url=http://archive.org/details/Puppy_Linux_puppy.b0x.me_mirror]HERE[/url]

Post Reply