![]() |
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 |
| comment/delete a particular pattern starting from second line of the matching pattern | imas | Shell Programming and Scripting | 4 | 10-13-2008 02:37 AM |
| Pattern matching | blue_bird | UNIX and Linux Applications | 3 | 10-08-2008 01:23 AM |
| ksh pattern matching | ripat | Shell Programming and Scripting | 5 | 02-10-2008 04:44 PM |
| pattern matching | malle | Shell Programming and Scripting | 3 | 01-31-2007 05:23 AM |
| pattern matching | larryase | UNIX for Dummies Questions & Answers | 3 | 11-22-2004 06:54 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Replacing all except for the matching pattern
Hi all
I need to replace all characters in a file except for the matching pattern eg. I need to replace all character with '*' except for the pattern "abc" Input "sdfhgsdf abc ##%$#abcsdfh sdfjkfff" Output "******abc******abc*************" Request for single liner solution |
|
||||
|
What about three liner solution?
In vi/vim:
:g/abc/s//[Control-V][Control-\]/g :g/[^[Control-V][Control-\]]/\*/g :g/[Control-V][Control-\]/s//abc/g I'm using [Control-V][Control-\] to replace 'abc' pattern with 034 octal character. Then replace all other characters with '*' (or something else) and then replace 034 octal character back to 'abc' again. You can use any other character instead of octal 034, e.g. '@' but this character must not occur in the file. If this is the case, then the same procedure beomes easier: :g/abc/s//@/g :g/[^@]/s//*/g :g/@/s//abc/g You can, of course, map this procedure to a single keystroke (map command in vi/vim) to get this one-liner of yours. |
![]() |
| Bookmarks |
| Tags |
| awk, replace, sed, shell |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|