Strange Sed !!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Strange Sed !!
# 1  
Old 08-23-2012
Strange Sed !!

Code:
 
wload/lscp/home/lscpvbf > v=1/1/1/
wload/lscp/home/lscpvbf > v=`echo $v|sed -e "s/\//\\\//g"`;echo $v
1/1/1/
wload/lscp/home/lscpvbf > v=1/1/1/
wload/lscp/home/lscpvbf > v=`echo $v|sed -e 's/\//\\\//g'`;echo $v
sed: Function s/\//\\//g cannot be parsed.
wload/lscp/home/lscpvbf > v=1/1/1/
wload/lscp/home/lscpvbf > v=`echo $v|sed -e 's?\/?\\\/?g'`;echo $v
1\/1\/1\/
wload/lscp/home/lscpvbf > uname -a
AIX tide2e16 3 5 00C7810F4C00
wload/lscp/home/lscpvbf >


Can some one please let me know the reason behind this strange behaviour of SED?
# 2  
Old 08-23-2012
Quote:
Originally Posted by vidyadhar85
Code:
 
wload/lscp/home/lscpvbf > v=1/1/1/
wload/lscp/home/lscpvbf > v=`echo $v|sed -e "s/\//\\\//g"`;echo $v
1/1/1/
wload/lscp/home/lscpvbf > v=1/1/1/
wload/lscp/home/lscpvbf > v=`echo $v|sed -e 's/\//\\\//g'`;echo $v
sed: Function s/\//\\//g cannot be parsed.
wload/lscp/home/lscpvbf > v=1/1/1/
wload/lscp/home/lscpvbf > v=`echo $v|sed -e 's?\/?\\\/?g'`;echo $v
1\/1\/1\/
wload/lscp/home/lscpvbf > uname -a
AIX tide2e16 3 5 00C7810F4C00
wload/lscp/home/lscpvbf >

Can some one please let me know the reason behind this strange behaviour of SED?
One extra backslash is there in your script....

see below output..

Code:
$ v=1/1/1/
$ echo "$v" | sed 's/\//\\/g'
1\1\1\

# 3  
Old 08-23-2012
I will only breakdown the first example. From that hopefully you'll be able to understand the rest.

There are three levels of parsing in `sed -e "s/\//\\\//g"` which may consider a backslash to be special (for each step, active escape sequences are highlighted in bold red):

1) With the obsolete form of command substitution, `...`, the backslash is special when it is followed by another backslash, dollar, or backtick.
`sed -e "s/\//\\\//g"` ==> sed -e "s/\//\\//g"

2) Now that the shell has processed the text of the subshell's command, it creates the subshell to execute the command. That subshell then processes the double-quoted string. In a double-quoted string, the backslash is also special when it occurs before certain characters, but it's not the same set of characters as when parsing a backtick command substition. In this context, backslash is special when followed by another backslash, a dollar, a double-quote, a backtick, or a newline.
sed -e "s/\//\\//g" ==> sed -e s/\//\//g

3) Finally, we reach sed, which sees an expression that replaces a backslash-escaped regular expression delimiter (forward slash in this case) with the same. This does not yield any change in the text.
s/\//\//g

Regards,
Alister

Last edited by alister; 08-23-2012 at 12:41 PM..
This User Gave Thanks to alister For This Post:
# 4  
Old 08-24-2012
Quote:
Originally Posted by alister
I will only breakdown the first example. From that hopefully you'll be able to understand the rest.

There are three levels of parsing in `sed -e "s/\//\\\//g"` which may consider a backslash to be special (for each step, active escape sequences are highlighted in bold red):

1) With the obsolete form of command substitution, `...`, the backslash is special when it is followed by another backslash, dollar, or backtick.
`sed -e "s/\//\\\//g"` ==> sed -e "s/\//\\//g"

2) Now that the shell has processed the text of the subshell's command, it creates the subshell to execute the command. That subshell then processes the double-quoted string. In a double-quoted string, the backslash is also special when it occurs before certain characters, but it's not the same set of characters as when parsing a backtick command substition. In this context, backslash is special when followed by another backslash, a dollar, a double-quote, a backtick, or a newline.
sed -e "s/\//\\//g" ==> sed -e s/\//\//g

3) Finally, we reach sed, which sees an expression that replaces a backslash-escaped regular expression delimiter (forward slash in this case) with the same. This does not yield any change in the text.
s/\//\//g

Regards,
Alister
Thanks Alister for the reply....

Does this mean we cant achieve the required result (1/1/1/-->1\/1\/1\/) from SED with backslash as delimeter?

Regards,
Vidya
# 5  
Old 08-24-2012
Here you go...
Code:
$ echo $v | sed 's/\//\\\//g'
1\/1\/1\/


I got the output for the same that you have used..

If you want to modify the value of "v", assign the above reult to v..
Code:
v=`echo $v | sed 's/\//\\\//g'`


Last edited by Franklin52; 08-24-2012 at 04:25 AM.. Reason: Please use code tags for data and code samples
# 6  
Old 08-24-2012
You can achieve it like this:

Code:
v=$(echo $v|sed -e 's/\//\\\//g')

or
Code:
v=`echo $v|sed -e 's/\\//\\\\\//g'`

This User Gave Thanks to Chubler_XL 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

sed parser behaving strange on replacing multiple words in multiple files

I have 4000 files like $cat clus_grp_seq10_g.phy 18 1002 anig_OJJ65951_1 ATGGTTTCGCAGCGTGATAGAGAATTGTTTAGGGATGATATTCGCTCGCGAGGAACGAAGCTCAATGCTGCCGAGCGCGAGAGTCTGCTAAGGCCATATCTGCCAGATCCGTCTGACCTTCCACGCAGGCCACTTCAGCGGCGCAAGAAGGTTCCTCG aver_OOF92921_1 ... (1 Reply)
Discussion started by: sammy777888
1 Replies

2. Shell Programming and Scripting

strange: sed and awk print at end instead of begin of line

Hi! I have a strange behaviour from sed and awk, but I'm not sure, if I'm doing something wrong: I have a list of words, where I want to add the following string at the end of each line: \;\;\;\;0\;1 I try like this: $ cat myfile | awk '{if ( $0 != "" ) print $0"\;\;\;\;0\;1"}' Result:... (5 Replies)
Discussion started by: regisl67
5 Replies

3. Shell Programming and Scripting

sed strange quotes behavior

Hi gurus input file: 1 2 3 4 desired output 1 2\ 2a 3 4 I tried (6 Replies)
Discussion started by: wakatana
6 Replies

4. Shell Programming and Scripting

sed issues with strange char

Hi all, I try to create a shell script to had the xiti tag at the end of servals web pages just before the <body/> tag. here is my script : #!/bin/bash ################################################################## rm -R /home/hibern/TEMP/hibern cp -R... (5 Replies)
Discussion started by: hibern
5 Replies

5. Shell Programming and Scripting

Lines with strange characters and sed...

Dear All: I Have a bunch of files which I'd like to process with a shell script. The problem is that the files have strange characters in their headers, like �g�8@L-000-MSG2__-ABCD________-FIRA_____-000001___-200806181330-__ ��e� Data from BLABLABLA, Instrument: BLABLA, Date:... (4 Replies)
Discussion started by: luiscarvalheiro
4 Replies

6. Shell Programming and Scripting

strange behaviour from sed???

Hi all, I want to do a very simple thing with sed. I want to print out the line number of a disk I have defined in /etc/exports, so I do: It's all good, but here's the problem. When I define md0 in a variable, I get nothing from sed: Why is that? can anybody please help? Thanks (2 Replies)
Discussion started by: alirezan
2 Replies

7. UNIX for Advanced & Expert Users

Strange sed behaviour

$ echo a.bc | sed -e "s/\|/\\|/g" |a|.|b|c| $ Is the behavior of the sed statement expected ? Or is this a bug in sed ? OS details Linux 2.6.9-55.0.0.0.2.ELsmp #1 SMP Wed May 2 14:59:56 PDT 2007 i686 i686 i386 GNU/Linux (8 Replies)
Discussion started by: vino
8 Replies

8. Solaris

Something strange...

Hi all, Thanks for any replies and for reading in advance. We have upgraded one of our database instances to 10g on a Solaris 8 box, anyhow the other day it started trying to ping loads of weird IP addresses that we don't use, since our systems all run on pretty similar IP's. It all behind... (0 Replies)
Discussion started by: B14speedfreak
0 Replies

9. UNIX for Dummies Questions & Answers

strange sed behavior

I have a file called products.kp which contains, for example, 12345678,1^M 87654321,2^M 13579123,3 when I run the command cat products.kp| sed -f kp.sed where kp.sed contains s,^M,, I get the output 12345678,1 87654321,2 13579123,3 (5 Replies)
Discussion started by: Kevin Pryke
5 Replies

10. UNIX for Dummies Questions & Answers

strange

Hi All I am doing a locate <file_name> on my Redhat 7 System. I am unable to get the output. All the keep getting is: locate: this is not a vlaid slocate database: /var/lib/locate/slocate.db What des this mean? Is my system compromised? Thanks in advance. KS (13 Replies)
Discussion started by: skotapal
13 Replies
Login or Register to Ask a Question