understanding the sed command

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting understanding the sed command
# 1  
Old 08-06-2009
understanding the sed command


I am trying to understand the sed command here.

adx001 $ a=/clocal/dctrdata/user/dctrdat1/trdroot/recouncil
adx001 $ b=`echo $a |  sed 's/\//\\\\\//g'`
adx001 $ echo $b

The sed command i took it from the script.

Please correct my understanding. As per my understanding, it searches for / (/\) and replaces it with \\/ (\\\\\/).

But the output is having only \/.

Please tell me where i am going wrong.

Thanks for your help in advance.


---------- Post updated at 08:18 PM ---------- Previous update was at 07:04 PM ----------

Guys found the prob,,
it is because of the echo statement

echo $b

Where the first "/" is taken as escape character.
# 2  
Old 08-06-2009
As per my understanding, it searches for / (/\) and replaces it with \\/ (\\\\\/)
No, it seaches for each occurance of '/' and replaces it with '\\/'
$ echo $a |  sed 's|/|\\\\/|g'

# 3  
Old 08-06-2009
Note the difference of the two equivalent entries:

b=`echo $a |  sed 's/\//\\\\\//g'`
echo $b

echo $a | sed 's/\//\\\\\//g'

Don't know exactly how to explain it but it seems that when the variable is assigned, there is an extra level of escape that is happening when echoing that variable which returns the different result.
# 4  
Old 08-07-2009
peterro and fmurphy, both of you are correct. when the value is assigned to the variable b, it will be \\/clocal\\/dctrdata\\/user\\/dctrdat1\\/trdroot\\/recouncil.

But because of the echo statement below, one of the \ is taken as escape character.

if we do

d02 $echo \\/

This is what is happening.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Quick help on Understanding sed Regex

Hi Guys, Could you please kindly explain what exactly the below SED command will do ? I am quite confused and i assumed that, sed 's/*$/ /' 1. It will remove tab and extra spaces .. with single space. The issue is if it is removing tab then it should be right .. please assist.... (3 Replies)
Discussion started by: Nandy
3 Replies

2. Shell Programming and Scripting

Understanding sed

Hi, can some one suggest me,how "sed" is managed to delete the second field here. Any explanation on , how the below code is working would be appreciated. sed 's/^\(*\)::/\1::/' /etc/passwd sed 's/*:/:/2' /etc/passwd (14 Replies)
Discussion started by: panyam
14 Replies

3. Shell Programming and Scripting

help understanding regex with grep & sed

I have the following line of code that works wonders. I just don't completely understand it as I am just starting to learn regex. Can you help me understand exactly what is happening here? find . -type f | grep -v '^\.$' | sed 's!\.\/!!' (4 Replies)
Discussion started by: trogdortheburni
4 Replies

4. UNIX for Dummies Questions & Answers

understanding sed command

Hi Friends, I need a small help in understanding the below sed command. $ cat t4.txt 1 root 1 58 0 888K 368K sleep 4:06 0.00% init 1 root 1 58 0 888K 368K sleep 4:06 0.00% init last $ sed 's/*$//' t4.txt 1 root 1 58 0 888K ... (3 Replies)
Discussion started by: forroughuse
3 Replies

5. UNIX for Dummies Questions & Answers

Understanding nm command output

After running nm command on any object file from out put can we get to know that wheather a symbol is a call to a function or definition of function ? I am searching a class and function definitions inside many .so files. I have 3 files which contain the symbol but I don't know wheather they... (2 Replies)
Discussion started by: yatrik007
2 Replies

6. Solaris

Understanding 'du' command

Hi I have a questions related 2 commands : 'du' and 'ls'. Why is the difference between output of 'du' and 'ls' cmd's ? Command 'du' : ------------------ jakubn@server1 /home/jakubn $ du -s * 4 engine.ksh 1331 scripts 'du -s *' ---> shows block count size on disk (512 Bytes... (5 Replies)
Discussion started by: presul
5 Replies

7. Shell Programming and Scripting

understanding mv command

hi i was moving a file from one directory to another with the following cmmand mv /home/hsghh/dfd/parent/file.txt . while doing so i i accidently mv /home/hsghh/dfd/dfd . although i gave ctrl c and terminate the move command some of the file are missing in the parent directory and... (1 Reply)
Discussion started by: saravanan71184
1 Replies

8. UNIX for Dummies Questions & Answers

Help understanding sed

I am trying to create a basic script that converts an Oracle script into a Sybase script. The only things im changing are Datatypes and the to_char and to_date functions. I am not really 100% sure of the way it works. I have tried running the functions through a loop to replace each word line... (6 Replies)
Discussion started by: Makaer
6 Replies

9. Shell Programming and Scripting

understanding the kill command

Hi Guys, I like to know if i have a process which triggers 10 different child processes. How to identify out of the 11 processes running which is the parent process and what are the child process? And if i kill the parent process will the child process be killed.. if not is there a way to... (2 Replies)
Discussion started by: mac4rfree
2 Replies

10. Shell Programming and Scripting

Help Needed in understanding this command

Hi All, I search the forum for my query, Glad that got solution to it. But i really want to understand how does this command work. sed -e ':a' -e 's/\("*\),\(*"\)/\1~\2/;ta' Basically it is replacing all the comma(,) characters in between quotes with a tilde. Specially what does ':a' ,... (2 Replies)
Discussion started by: DSDexter
2 Replies
Login or Register to Ask a Question