How to use backslash and variables in sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use backslash and variables in sed
# 1  
Old 01-23-2009
How to use backslash and variables in sed

I have a line that contains backslashes in which I want sed to substitute text with variables.

The line;

\\s008\2033330user$
I want to change this in \\s008.ourschool.com\2033330user$

I now use this script:

USER=2033330user
sed 's/\\'"$USER"'/.ourschool.com\\'"$USER/"

This doesn't seem te work for me. What am I doing wrong?
# 2  
Old 01-23-2009
The double backslash is to protect a single one from being interpreted as a special character by the shell, but I believe that enclosing a single one in single quotes will have the same effect, so try
Code:
USER=2033330user
sed 's/\'"$USER"'/.ourschool.com\'"$USER/"

or stick with double backslashes and enclose the whole lot in double quotes
Code:
USER=2033330user
sed "s/\\$USER/.ourschool.com\\$USER/"

Jerry
# 3  
Old 01-23-2009
Thank you for replying.

If I use your suggestion, the end result is slightly different from what I want. I now get this output:
.ourschool.com\\s008\2033330user
This happens with both your suggestions.

I want this output:
\\s008.ourschool.com\2033330user

Even so, if I use an input with a $, sed doesn't change anything. For example, one of our users has a home directory on \\s008\2033330user$

If I use the sed script, sed gives the same output as the input. It doesn't change the input. Does sed interpret this dollar sign as a special character?

Thank you for time!
# 4  
Old 04-09-2009
Have a similar problem that I'd HUGELY appreciate help with.

I've got a trojan that put one very large line of code into hundreds of files on my server. Its full of back slashes, forward slashes etc.

Is there any quick way of saying:

DELTE THIS LINE IF ITS GOT "<?php if(!function_exists('tmp_lkojfghx')){if(isset" in it?

Im using sed to replace characters with blanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[BASH] Getting a semi-tailing backslash when passing (escaped) variables to script

Heyas Figured me had a 'typo' in tui-conf-set, i went to fix it. Now, i also figured, it might be nice to have tui-conf-set report (to console, not only exit code) wether it could save the variable to the file or not. This said, I appended this code: (the tui-title and tui-echo lines are... (3 Replies)
Discussion started by: sea
3 Replies

2. Shell Programming and Scripting

Adding a backslash in front of square brackets with sed

I'm trying to convert this line: to \ with sed. This is what I have so far: sed -e 's/\]*\)\]/\\\\\/' but this still gives me . Any suggestions? (15 Replies)
Discussion started by: lehaste
15 Replies

3. UNIX for Dummies Questions & Answers

Replace backslash at the end of the string using sed command

I have text file which is a tab delimited one. Sample data from the file is shown below: unix is\ great\ os linux\ is superb I want to replace that backslash with empty string preserving the tab delimiter. Output should be unix is great os linux is ... (3 Replies)
Discussion started by: p.akhilreddy4u
3 Replies

4. Shell Programming and Scripting

How can I get sed to include backslash and 'f' in the output

Both of these fail. One has two form feeds, the second form leaves all the backslashes. bold='\(code\|command\|var\|samp\|option\|strong\)' sed -e "s;@${bold}{"'\(*\)};\fB\2\fP;g' sed -e "s;@${bold}{"'\(*\)};\\fB\2\\fP;g' Obviously, I'm trying to change texi markup into man page markup, but it... (3 Replies)
Discussion started by: bkorb
3 Replies

5. Shell Programming and Scripting

Cut on last backslash on hyperlink string-sed/awk??

hyper link- abc:8081/xyz/2.5.6/rtyp-2.5.6.jar Needs to get "rtyp-2.5.6.jar" i.e character after last backslash "/" how to do this using sed/awk?? help is highly appreciated. (7 Replies)
Discussion started by: kkscm
7 Replies

6. Shell Programming and Scripting

SED script to backslash special characters

I have a shell script that I have written to be a kind of to-do/notepad that's quickly executable from the command line. However, special characters tend to break it pretty well. Ie: "notes -a This is an entry." works fine. "notes -a This is (my) entry." will toss back a bash syntax error on... (5 Replies)
Discussion started by: skylersee
5 Replies

7. Shell Programming and Scripting

sed variable and backslash

I am trying to write a bash shell script, which extracts a sentence from a dynamically created dynamic file and passes it as a variable to sed and uses that sentence as a starting point to pull the content from a log file. the key part of the script is this key=`cat /tmp/dynamic` sed -n... (3 Replies)
Discussion started by: fedora
3 Replies

8. Shell Programming and Scripting

Non-inserting backslash in sed statement

#!/bin/bash wget -O tmp.tmp "YouTube - Pretty Woman- Vivian's Goes Shopping!" temp=`grep 'one&video_id=' tmp.tmp | sed "s/.*one&video_id=\(.*\)'\;.*/\1/"` temp="http://www.youtube.com/get_video?video_id=$temp" temp=`echo $temp|sed -n "s/!/\\!/p"` echo " -O $filename \"$temp\"" Output:... (3 Replies)
Discussion started by: kds1398
3 Replies

9. Shell Programming and Scripting

bash - add backslash in front of variables w/ spaces

Hello, Im writing a script that works by recursively going into directories with find. But I have some directories that have spaces in them.. so I need to parse the variables to add a backslash before the spaces. Im not exactly sure how how to do this in bash, and honestly I dont think I know... (3 Replies)
Discussion started by: trey85stang
3 Replies

10. Shell Programming and Scripting

Sed and awk backslash characters

Hi, I have a variable read from user input: PROFILESROOTDIR="\\194.185.82.188\CMSRepository\EncodingProfiles" awk -F"=" -v gr=$PROFILESROOTDIR '/ProfilesRootDirectoryFromXOEMachine/{$2=gr;}1' OFS="=" $CFGFILE > "${CFGFILE}_new" For this awk to work properly I need to replace in the... (7 Replies)
Discussion started by: potro
7 Replies
Login or Register to Ask a Question