LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-11-2006, 12:21 PM   #1
daYz
Member
 
Registered: Nov 2003
Distribution: Debian
Posts: 164

Rep: Reputation: 30
How to output two backslashes with sed?


I want to use Sed to substitute a pattern with another pattern that contains two backslahes.
I have tried to accomplish this myself but when I try this Sed outputs only one backslash or none at all, no matter what I try.


The commands I've used are like this:

Code:
sed "s/pattern/substitutionpattern\\/" filename
Code:
sed "s/pattern/substitutionpattern\\\/" filename
Code:
sed "s/pattern/substitutionpattern'\\'/" filename
etc

So can someone tell me how this can be accomplished?

Thanks,

Ben

Last edited by daYz; 10-11-2006 at 12:22 PM.
 
Old 10-11-2006, 12:24 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi,

You are not bound to the / being the seperator when using sed.

sed 's%pattern%substpattern//%g' file should do the trick.
 
Old 10-11-2006, 12:40 PM   #3
daYz
Member
 
Registered: Nov 2003
Distribution: Debian
Posts: 164

Original Poster
Rep: Reputation: 30
Bedankt druuna.

You are right, this works. Can you also tell me how this can be done when everything is between double quotes?

Groeten,

Ben
 
Old 10-11-2006, 02:11 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi,

What do you want to know about the double quotes?

Code:
$ cat infile
1 this #
2 hist #
3 this #

$ sed "s%this%that//%g" infile 
1 that// #
2 hist #
3 that// #

$ cat blaat
#!/bin/bash

sed "s%this%that//%g" $1

$ ./blaat infile
1 that// #
2 hist #
3 that// #
In general you want to use single quotes, only time I use double quotes is inside scripts where sed gets it's input from a variable.
I.e: sed "s/${this}/that/g" Because of the double quotes, bash will expand ${THIS} to the content of that variable. Single quotes prevent this.

Hope I answered your question
 
Old 10-11-2006, 03:12 PM   #5
daYz
Member
 
Registered: Nov 2003
Distribution: Debian
Posts: 164

Original Poster
Rep: Reputation: 30
Quote:
In general you want to use single quotes, only time I use double quotes is inside scripts where sed gets it's input from a variable.
That is the reason I'm asking, but I'm afraid I had to say forward slashes instead of backward slashes. So I need to know how it can be done with these things: "\"

Sorry about this.

Regards,

Ben
 
Old 10-11-2006, 03:25 PM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi,

Ok, i get it

This should work: sed 's/this/that\\\\/'

A backslash is a special character and needs to be preceded by a backslash to make it literal (Ie: \\ you end up with \), you need to do this twice, so you'll end up with 4 backslashes inside the sed statement.
 
Old 10-12-2006, 11:23 AM   #7
daYz
Member
 
Registered: Nov 2003
Distribution: Debian
Posts: 164

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by druuna
Hi,

Ok, i get it

This should work: sed 's/this/that\\\\/'

A backslash is a special character and needs to be preceded by a backslash to make it literal (Ie: \\ you end up with \), you need to do this twice, so you'll end up with 4 backslashes inside the sed statement.
Thanks again druuna, but how can it be done when everything is between double quotes?

b.t.w. I have already tried using four forward slashes between double quotes, but that did not work.

Regards,

Ben
 
Old 10-12-2006, 11:39 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi,

You need to use a lot more backslashes.......

sed "s/this/that\\\\\\\\/" (that's 8 backslashes in total).

Funny thing is, if you use 7 backslashes it also works.... Don't ask me why.

Hope this helps.
 
Old 10-12-2006, 03:44 PM   #9
daYz
Member
 
Registered: Nov 2003
Distribution: Debian
Posts: 164

Original Poster
Rep: Reputation: 30
Great! It works indeed.

Thank you very much druuna. You have helped me real well. I have spend several ours on this myself without success, so I am glad you have helped me out.

Regards/Groeten,

Ben
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to process ping output with sed? Griffon26 Linux - General 5 08-30-2009 05:16 PM
Sed gives output in pipes only when buffer is full jahvascriptmaniac Linux - Software 2 08-31-2006 06:57 PM
sed script to read only columns 4 to 6 in output database cranium2004 Programming 10 02-28-2006 08:20 AM
weird sed output schneidz Programming 3 05-19-2005 12:16 PM
insert output of sed into a variable hwouters Linux - General 3 11-06-2004 08:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:37 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration