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
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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-07-2009
raghav288 raghav288 is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 21
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
  #2 (permalink)  
Old 01-07-2009
Panos1962 Panos1962 is offline
Registered User
  
 

Join Date: Dec 2008
Location: Thessaloniki, GREECE
Posts: 29
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.
Closed Thread

Bookmarks

Tags
awk, replace, sed, shell

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 07:46 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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