Search for / and insert \ for every instance


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for / and insert \ for every instance
# 1  
Old 07-25-2013
RedHat Search for / and insert \ for every instance

Hi,

I want to do a sed (linux) or the alternative in PHP to insert a backslash in front of every occurrence of every forward slash

ex.

/archive/data/stanley
-->
\/archive\/data\/stanley

I appreciate it!
# 2  
Old 07-25-2013
Code:
sed 's#\/#\\\/#g'

OR
Code:
sed 's#\/#\\&#g'

This User Gave Thanks to Yoda For This Post:
# 3  
Old 07-25-2013
one more

Quote:
Originally Posted by Yoda
Code:
sed 's#\/#\\\/#g'

OR
Code:
sed 's#\/#\\&#g'

If i want to assign post result in shell script as:
assigning the literal string to a variable ..

echo $VERSION | sed 's#\/#\\\/#g' > $MOD_VERSION

What's the way to make that work?
# 4  
Old 07-25-2013
Quote:
Originally Posted by Yoda
Code:
sed 's#\/#\\\/#g'

OR
Code:
sed 's#\/#\\&#g'

/ is only special when it's used as the delimiter.

The highlighted backslashes are incorrect and produce undefined escape sequences.

POSIX sed:
Quote:
The meaning of a <backslash> immediately followed by any character other than '&', <backslash>, a digit, or the delimiter character used for this command, is unspecified.
Regards,
Alister
# 5  
Old 07-25-2013
Quote:
Originally Posted by deadyetagain
If i want to assign post result in shell script as:
assigning the literal string to a variable ..
You don't need sed for that in bash or ksh:

Code:
VERSION="${VERSION//\//\\/}"

if you are forced to use an old shell:

Code:
VERSION=$(echo "$VERSION" | sed 's#/#\\/#g'

This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search and Insert

I have a following file: # User defined in group admin = boss, sysadmin phpteam = php1, php2, php3 rubyteam = ruby1, ruby2 # SVN root should only be access by boss and system admin @admin = rw * = # Main web repository should only be access by boss and system admin, others... (3 Replies)
Discussion started by: ankur328
3 Replies

2. Shell Programming and Scripting

Search and Insert

Hi All I got one problem and want your help,what i want to do is Input 123 key0=A;B;C;:key1=X;Y;Z;: here 123 is a username I should search the username (in this case it is 123) in file and if found it should insert the additional string D as below Here 123 and key are tab seperated ... (6 Replies)
Discussion started by: parthmittal2007
6 Replies

3. Shell Programming and Scripting

Search text file, then grep next instance of string

I need to be able to search for a beginning line header, then use grep or something else to get the very next instance of a particular string, which will ALWAYS be in "Line5". What I have is some data that appears like this: Line1 Line2 Line3 Line4 Line5 Line6 Line7 Line1 Line2 ...... (4 Replies)
Discussion started by: Akilleez
4 Replies

4. Shell Programming and Scripting

sed: how to limit pattern search to first instance only

I need to reduce a file's size below 50MB by deleting chucks of text. The following sed does this. sed '/^begpattern/,/endpattern/d' myfile However, it's possible that the file size can get below 50MB by just deleting the first instance of the pattern. How do I code that into sed? Or can awk... (8 Replies)
Discussion started by: mariod1049
8 Replies

5. Shell Programming and Scripting

Search for last instance of word in a file

Hi I'm trying to search for the last instance of the word 'cache' in a HTML file that I have downloaded from YouTube. I'm using the following syntax, but an error is thrown when I try it. grep -f "cache" Also I wish to append the above grep command to the below so that the search for cache... (3 Replies)
Discussion started by: colmbell
3 Replies

6. Shell Programming and Scripting

KSH - search for string and insert line above

Hi,, Please excuse my lack of knowledge here. I think I am tring to do something fairly advanced yet am a bit of a beginner..... I want to use a korn shell (as is the norm with others in my company) to write a script to search a file for a string and insert some text above that line. Sudo... (5 Replies)
Discussion started by: HugoDarley
5 Replies

7. Shell Programming and Scripting

Search the last instance of a string in a file

I have a big database log file which keepsgrowing daily. OS is HP UX. Here is a small part of it: Tue Jan 27 04:03:22 2009 : add session begin for mfgeb on /dev/pts/th. : Converting relative path to absolute path. : add session end. Tue Jan 27 04:03:29... (6 Replies)
Discussion started by: dinesh1178
6 Replies

8. Shell Programming and Scripting

replace first instance(not first instance in line)

Alright, I think I know what I am doing with sed(which probably means I don't). But I cant figure out how to replace just the first occurance of a string. I have tried sed, ed, and grep but can't seem to figure it out. If you have any suggestions I am open to anything! (3 Replies)
Discussion started by: IronHorse7
3 Replies

9. Shell Programming and Scripting

Search and insert between Pattrens...

Hi Every One... I wanted to inserted a line in between matched pattrens.. Ex... InPut File.. WRITEQ TS ************************** aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbb cccccccccccccccccccccccc SOME PATTREN's RESP ( WS-RESP ) ... (7 Replies)
Discussion started by: pbsrinivas
7 Replies

10. Shell Programming and Scripting

sed search and insert

how can i modify a file using sed when searching for a pattern and insert newline after the pattern? pattern example: $ ...(any characters)...$ $ may082001.../tmp/msg.001,,$ REPT CLEAR ALARMS ON UNIT 1 $ may082001.../tmp/msg.002,,$ UNIT 1 IN SERVICE into: $... (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question