The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
escape character in tcsh balareddy Shell Programming and Scripting 1 08-15-2008 05:58 AM
Escape character deepakpv Shell Programming and Scripting 4 02-16-2007 03:19 AM
awk / escape character OFFSIHR Shell Programming and Scripting 8 11-29-2006 01:28 PM
Escape character in vi? stevefox Shell Programming and Scripting 4 11-17-2005 03:38 PM
possible to escape the \ character in sed? gammaman UNIX for Dummies Questions & Answers 1 07-07-2005 02:49 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-07-2008
c00kie88 c00kie88 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 63
Angry Escape character - sed

Hi All,

How do i write in sed for the 6th and 7th field of etc/passwd file as it involves "/" character?

Does mine below is correct? It's incomplete script as i need help with syntax as i always getting may errors

Example of etc/passwd file:

blah:x:1055:600:blah blah:/home/blah:/bin/ksh

sed "s/\($name:.:[0-9]*:\)[0-9]*:\(.*\):\(\/.*\/.*\):\(\/.*\/.*\)

Last edited by c00kie88; 10-07-2008 at 08:17 AM..
  #2 (permalink)  
Old 10-07-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
The separator after s can be any character. Also, I'd recommend using single quotes around the script unless you specifically need double quotes (in which case the backslashes need to be doubled).

Code:
sed 's%\('"$name"':.:[0-9]*:\)[0-9]*:\(.*\):\(/.*/.*\):\(/.*/.*\) ...'
Notice the mix of single and double quotes above.
  #3 (permalink)  
Old 10-08-2008
c00kie88 c00kie88 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 63
i tried that, but it says "SED: command garbled:
  #4 (permalink)  
Old 10-08-2008
c00kie88 c00kie88 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 63
it looks as:

sed "s/\($name:.:[0-9]*:\)[0-9]*:\(.*\):\(/.*/.*\):\(/.*/.*\)/\1$answer\2/" /etc/passwd

I tried the single quote as era suggested, but SED is not happy
  #5 (permalink)  
Old 10-08-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Repeat: you need to double the backslashes if you use double quotes. This is an artefact of the shell's quoting mechanisms, not of sed syntax as such. If you use the slash as separator, you do need to backslash any slashes which are not separators; but it's better to simply use a different separator character.

Code:
sed "s%\\($name:.:[0-9]*:\\)[0-9]*:\\(.*\\):\\(/.*/.*\\):\\(/.*/.*\\)%\1$answer\2%" /etc/passwd
With single quotes, that becomes

Code:
sed 's%\('"$name"':.:[0-9]*:\)[0-9]*:\(.*\):\(/.*/.*\):\(/.*/.*\)%\1'"$answer"'\2%' /etc/passwd
The variables $name or $answer obviously cannot contain the separator character in their values (or you need to escape the values).

Last edited by era; 10-08-2008 at 02:15 AM.. Reason: Need to escape variables if they contain the separator
  #6 (permalink)  
Old 10-08-2008
c00kie88 c00kie88 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 63
Ok..

I use single quote..

sed 's/\('"$name"':.:[0-9]*:\)[0-9]*:\(.*\):\(/.*/.*\):\(/.*/.*\)' /etc/passwd

but still no good..

what did i do wrong???
  #7 (permalink)  
Old 10-08-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
You are not copy+pasting correctly, and not reporting back what the error message is. "Still no good" is not a useful diagnostic. But if you carefully use your mouse to copy+paste the commands above, and carefully copy+paste any errors back here, we might be able to help you.

"Unknown option to s" means you have something after (what sed thinks is) the final separator which is not a valid option for s/from/to/gp (here, "g" and "p" are options). In this case, it's simply because you still use slash as the separator, without escaping those slashes which are not separators. Change the separator to %, or escape the slashes as required.

Also, your command is partial; you are missing the "to" part \1$answer\2
Sponsored Links
Closed Thread

Bookmarks

Tags
quoting, sed syntax

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 09:56 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0