Non-inserting backslash in sed statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Non-inserting backslash in sed statement
# 1  
Old 03-07-2009
Non-inserting backslash in sed statement

Code:
#!/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:
Code:
--02:19:32--  YouTube - Pretty Woman- Vivian's Goes Shopping!
           => `tmp.tmp'
Resolving YouTube - Broadcast Yourself.... 208.65.153.238
Connecting to www.youtube.com|208.65.153.238|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 96,276 (94K) [text/html]
100%[=================================================================================================================>] 96,276       308.44K/s
02:19:33 (307.59 KB/s) - `tmp.tmp' saved [96276/96276]
 -O   " Woman- Vivian\'s Goes Shopping!"

I'm just learning about scripting & I'm working on a youtube download script but I can't get a backslash inserted to escape an exclaimation point. My 3rd assignment of the temp variable (with the second sed statement) doesn't appear to be replacing "!" with "\!" .
Anyone have an Idea of what I'm doing wrong?

Last edited by Franklin52; 03-07-2009 at 09:37 AM.. Reason: Url tags removed
# 2  
Old 03-07-2009
Use single quotes:

Code:
sed -n 's/!/\\!/p'

instead of double quotes:

Code:
sed -n "s/!/\\!/p"

Regards
# 3  
Old 03-08-2009
I thought single quotes too, and that didn't work right. Here is what actually worked after much trial and error:

Code:
sed -n s/'!'/'\\''!'/p

# 4  
Old 03-10-2009
Here was the final complete code if anyone was interested. I know the sleep commands aren't needed, and neither is the for loop, but they make it feel like the program is taking a few seconds to do behind the scenes processing instead of instantly going to the video download.

Code:
#!/bin/bash

errored() {
  echo "Usage:   `basename $0` [-f savefile] URL"
  echo "Example: `basename $0` -f Rick http://www.youtube.com/watch?v=oHg5SJYRHA0"
  echo "         Saves video with id oHg5SJYRHA0 to Rick.flv in your current directory"
  echo
  exit 65
}

if [ $# -ne 3 ]; then errored; fi

while getopts ':f:' Option
do
 case $Option in
  f ) fn=$OPTARG;;
  * ) errored;;
 esac
done

shift $(($OPTIND -1))

if [ -z $1 ]; then errored; fi
if [ -z $fn ]; then fn=$1; fi

vidid=$1
filename=$fn".flv"
echo -e "\nFilename $filename is now set\n"
temp=$(wget -qO - $vidid|grep '&video_id='|head -n 1|sed -e 's/.*video_id=//' -e 's/'\;'/''/')
echo -e "Searching $vidid for video location\c"

for (( i = 0 ; i <= 3; i++ ))
do
 echo -e ".\c"
 sleep 1
done

echo -e "\n\nHidden URL found: $temp\n"
sleep 1
temp=$(echo "http://www.youtube.com/get_video?video_id=$temp")

echo -e "Retrieving video \"$temp\"\n"

wget -O $filename "$temp"

The function errored () and the saving of the input arguments was borrowed from http://www.johnlawrence.net/code/?f=youtube.sh ... they help with the prettiness & basic validation.

Last edited by kds1398; 03-11-2009 at 12:22 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 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

Please help inserting the correct if-then-else statement

Hi All, I have this script below that checks rpm code version to Linux and Solaris servers. The script is run using arguement ($1) which is server_list.txt. It runs as: $ ./scriptname server_list.txt #!/bin/bash QUERY="/opt/rpm/bin/rpm -qa --queryformat '%{installtime}... (0 Replies)
Discussion started by: linuxgeek
0 Replies

9. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: Tubbie
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