![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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.. |
|
||||
|
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]*:\(.*\):\(/.*/.*\):\(/.*/.*\) ...'
|
|
||||
|
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 Code:
sed 's%\('"$name"':.:[0-9]*:\)[0-9]*:\(.*\):\(/.*/.*\):\(/.*/.*\)%\1'"$answer"'\2%' /etc/passwd
Last edited by era; 10-08-2008 at 02:15 AM.. Reason: Need to escape variables if they contain the separator |
|
||||
|
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 | ||
|
|
![]() |
| Bookmarks |
| Tags |
| quoting, sed syntax |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|