A sed doubt - need explanation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A sed doubt - need explanation
# 1  
Old 09-18-2008
A sed doubt - need explanation

Hi,

The following command works fine for me, but I could not grasp the logic working behind of sed command, it's obscure to me Smilie Smilie

Code:
echo "./20080916/core/audioex.amr" | sed "s%\([^.]\)/%\1_%g"

o/p: ./20080916_core_audioex.amr

Could anyone please explain to me in detail, that how the sed is working here?
# 2  
Old 09-18-2008
replaces all "/" characters with "_" for every line that starts with "."
# 3  
Old 09-18-2008
Nope, it changes any slash which is preceded by a character which is not dot into an underscore.

^ means "start of line" only outside of [...] character classes. In a character class, ^ is the negation operator if it's the first character after the opening square bracket.

The parentheses \(...\) just select which part to use in the substitution; anything matching the expression between \( and \) will be available in the substitution part as \1. (If you have multiple parentheses, the following ones will be in \2, \3, etc. Numbering corresponds to the order of the opening parentheses.)

So: take any character except a dot, followed by slash, and substitute with whatever matched the "not a dot" and underscore.
# 4  
Old 09-18-2008
Quote:
replaces all "/" characters with "_" for every line that starts with "."

well...not quite...and you can see that by running itSmilie...the fuller breakdown is:

Code:
sed "s        # substiute

%              # use % as delimiter, so we have %LHS%RHS% 
\([^.]\)/        # LHS i.e. pattern to work on - the \(..\) captures the interior pattern - which is [^.]\ i.e. anything that isn't a . , followed by the /
%
\1_            # this replaces what was in the brackes ( ), followed by a _
%
g"              # globally

i.e. ./ won't match, but all other X/ will, and will get replaced on the RHS by X_

# 5  
Old 09-25-2008
But in this code:
Code:
echo dogdog | sed -r s/\(.*\)\\1/cat/

I have 3 doubts:
1) I am getting the output as 'cat' but, if I wrapped the sed command inside single quotes, the output is 'dogdog', why this change?
2) why the syntax is '\\1' and not as '\1' in sed? it reports error if I change so
3) why it is not '/\1'?

N.B: echo dogdog | sed -r 's/(.*)\1/cat/' # Ans: cat

Last edited by royalibrahim; 09-25-2008 at 04:11 AM..
# 6  
Old 09-25-2008
It's not really a sed question, it's a shell question. An unquoted backslash is interpreted by the shell. In so many words, to get a backslash in a sed script, single-quote the script or double the backslashes. Either way is fine. The point is to protect them from the shell. Usually quoting is the preferred mechanism for anything longer than just a few characters.

The s command in sed requires three separators, one on each side and one in the middle. It can be any character but slash is often used. If you add more separators, you will produce a syntax error (sometimes a rather confused one, as sed will simply try to read whatever comes after the third separator as options to the substitution).

Code:
sed 's%from%to%'  #fine, separator is %
sed 's/from/to/'  # fine, separator is /
sed 's%from%to%gp'  # fine, separator is %, options are gp
sed 's%from%\1%to%'  # error, separator is %, to% are not valid options


Last edited by era; 09-25-2008 at 06:16 AM.. Reason: s%from%to% explanation
# 7  
Old 09-26-2008
Quote:
Originally Posted by era
It's not really a sed question, it's a shell question. An unquoted backslash is interpreted by the shell. In so many words, to get a backslash in a sed script, single-quote the script or double the backslashes. Either way is fine. The point is to protect them from the shell. Usually quoting is the preferred mechanism for anything longer than just a few characters.

The s command in sed requires three separators, one on each side and one in the middle. It can be any character but slash is often used. If you add more separators, you will produce a syntax error (sometimes a rather confused one, as sed will simply try to read whatever comes after the third separator as options to the substitution).

Code:
sed 's%from%to%'  #fine, separator is %
sed 's/from/to/'  # fine, separator is /
sed 's%from%to%gp'  # fine, separator is %, options are gp
sed 's%from%\1%to%'  # error, separator is %, to% are not valid options

Hi era, thanks for the explanation. But still I have questions here:

[Q1] why echo dogdog | sed -r 's/\(.*\)\\1/cat/' is giving output dogdog
and echo dogdog | sed -r s/\(.*\)\\1/cat/ is giving output cat? If you say the backslash is interpreted it should give syntax error for the first script not the variable output

[Q2] why echo dogdog | sed -r 's/\(.*\)\1/cat/' is giving me syntax error? double slashing \\1 works fine though it is inside single-quotes. If inside a single-quoted script, escaping the parentheses \(...\) does not make any difference means this rule should also be applied for single or doubling the backslashes for '\1'. But here '\1' is giving error whereas '\\1' is working fine though the sed script has 3 separators or pattern delimiters. I couldn't understand this ambiguity

Last edited by royalibrahim; 09-26-2008 at 07:42 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command explanation

Will someone give me an explanation on how the sed command below works. sed 's/.*//' Thanks! (3 Replies)
Discussion started by: scj2012
3 Replies

2. UNIX for Dummies Questions & Answers

Doubt in sed

Hi All Can some one explain what does the given two sed commands do :confused: sed "s/\'/\\\'/g" | sed 's/\"/\\\"/g' Please find the sample code i used to find out what this is doing , but it has confused me more :wall: $ cat sri1.txt \ ' " $ sed 's/\"/\\\"/g' sri1.txt \ '... (5 Replies)
Discussion started by: Sri3001
5 Replies

3. Shell Programming and Scripting

sed sorting command explanation

sed '$!N; /^\(.*\)\n\1$/!P; D' i found this file which removes duplicates irrespective for sorted or unsorted file. keep first occurance and remove the further occurances. can any1 explain how this is working.. i need to remove duplicates following file. duplicate criteria is not the... (3 Replies)
Discussion started by: mukeshguliao
3 Replies

4. UNIX for Dummies Questions & Answers

Explanation of the command sed -n -e 's_.*>\(.*\)<.*_\1_p' filename.xml

Hi Friends, I am trying to modify a script .The script contains this line: sed -n -e 's_.*>\(.*\)<.*_\1_p' filename.xml I am not great in sed command.I know, it is regular expression to match a pattern string that starts with s_ and ends with 1_.I doesnot give the desired result. Can... (4 Replies)
Discussion started by: rajsharma
4 Replies

5. UNIX for Dummies Questions & Answers

SED command explanation

can someone please explain the below sed command.. sed 's/\(*|\)\(.*\)/\2\1/' (6 Replies)
Discussion started by: raghu_shekar
6 Replies

6. Shell Programming and Scripting

sed doubt...

Hi, i need find and replace a sting with a new variable having value as spaces in between. Eg: set a = "i am variable" set b = "i am second" sed -e 's/find_string/'$a'/g' -e 's/find2_str/'$b'/g' input_file here it is giving error... How to get an varaible, which is... (6 Replies)
Discussion started by: vasanth.vadalur
6 Replies

7. Shell Programming and Scripting

Explanation for interesting sed behaviour?

This is my first post so hi to you all. I have browsed these forums in the past and what a great community and resource this is! Thanks to all the contributors ... I look forward to being able to give something back. In the meantime, I have a little conundrum concerning sed. My very simple... (6 Replies)
Discussion started by: Gavster
6 Replies

8. Shell Programming and Scripting

sed command explanation needed

Hi, Could you please explain me the below statement -- phrase wise. sed -e :a -e '$q;N;'$cnt',$D;ba' abc.txt > xyz.txt if suppose $cnt contains value: 10 it copies last 9 lines of abc.txt to xyz.txt why it is copying last 9 rather than 10. and also what is ba and $D over there in... (4 Replies)
Discussion started by: subbukns
4 Replies

9. UNIX for Dummies Questions & Answers

doubt in sed

hi all, i have a variable exported as VAR=ATTRIB then tried with, echo "tt" | sed 's/^/$VAR/' expected result as ttATTRIB but obtained only, $VARtt i could nt get where i am wrong. Thanks. (3 Replies)
Discussion started by: matrixmadhan
3 Replies

10. Shell Programming and Scripting

doubt it sed

Hello.. i want to use variable in sed.. like sed 's/ROOTMAILID/$variable/g' conf.test but its not working.. please help thanks in advance esham (2 Replies)
Discussion started by: esham
2 Replies
Login or Register to Ask a Question