LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-05-2012, 09:09 AM   #1
msriram
LQ Newbie
 
Registered: May 2011
Posts: 3

Rep: Reputation: 0
Scripting- sed command - replacing strings excluding the lines matching a pattern(s)


Hi,

I am having difficulty in using the sed command for finding and replacing strings in a file.
My requirement was to replace all the strings matching a pattern except in lines containing a specific pattern.

Used the below command for this.

sed "/$EXCLUDING_PATTERN/!s/$FIND_STRING/$REPLACE_STRING"

This one worked - it replaced all occurances of $FIND_STRING with $REPLACE_STRING except in lines where $EXCLUDING_PATTERN was found.

I wanted to extend this to multiple exclude patterns - say lines containing any of these patterns $EXCLUDE_PATTERN1, $EXCLUDE_PATTERN2...etc should be ignored while replacing.

Please can somebody help me get the command right for the above requirement?
 
Old 11-05-2012, 09:29 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Using extended regular expressions (see option -r) you can separate multiple patterns using pipes, e.g.
Code:
sed -r "/$EXCLUDING_PATTERN_1|$EXCLUDING_PATTERN_2/!s/$FIND_STRING/$REPLACE_STRING/"
Hope this helps.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Appending matching strings to specific lines (sed/bash) suntzu Programming 18 09-08-2012 03:29 PM
Sed command to print matching lines and 2 lines above.. DX398 Programming 12 10-01-2008 08:25 AM
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 05:08 PM
Sed pattern matching digitalbrutus Programming 1 08-20-2006 01:37 PM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration