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
search for words with capital leters djdaniel3 Shell Programming and Scripting 7 04-07-2008 02:54 AM
Search File for Specific Words mevasquez UNIX for Dummies Questions & Answers 2 12-04-2007 04:31 PM
Search files that all contain 4 specific words WoodenSword Shell Programming and Scripting 13 01-22-2007 06:57 AM
Search and replace words between two keywords vrrajeeb Shell Programming and Scripting 6 05-11-2005 12:15 PM
search for words in file Agent_Orange Shell Programming and Scripting 4 10-25-2002 05:29 AM

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

Join Date: May 2008
Posts: 18
search two words in sed

I've following sed command working fine -
sed '/search_pattern1/ !s/pattern1/pattern2/" file

Now, I want to search two patterns - search_pattern1 and search_pattern2 .


How can put these into above sed statement ?

Thanks in advance.
  #2 (permalink)  
Old 07-29-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
Code:
sed '/search_pattern1\|search_pattern2/ !s/pattern1/pattern2/" file
Different sed dialects might not want the backslash before the | alternation operator.
  #3 (permalink)  
Old 07-30-2008
ajitkumar2 ajitkumar2 is offline
Registered User
  
 

Join Date: May 2008
Posts: 18
This doesn't work.
  #4 (permalink)  
Old 07-30-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
Do you get a syntax error, or doesn't it do what you expect? Your request can be interpreted at least two ways (lines not containing search_pattern1 or search_pattern2 should have pattern1 replaced with pattern2; or, lines not containing search_pattern1 should have pattern1 replaced with pattern2, and lines not containing search_pattern2 should have some unspecified substitution applied to them) so perhaps you can elaborate with example input, example output, and what exactly you have tried so far.
  #5 (permalink)  
Old 07-30-2008
ajitkumar2 ajitkumar2 is offline
Registered User
  
 

Join Date: May 2008
Posts: 18
OK, here is what I want to do

1. I want perform some opreation on the lines which does not have search_pattern1 or search_pattern2.

2. And the operation is - in those lines replace pattern1 by pattern2, if at all pattern1 is present. if pattern1 is not present leave them as it is.


In the case where I 've only one search_pattern, The following command works file -

sed '/search_pattern1/ !s/pattern1/pattern2/" file


Now, I want to add one more word to search "search_pattern2" with "or" condition, I'm not able to fit that into sed.

something like -

sed '/search_pattern1|search_pattern1/ !s/pattern1/pattern2/" file

should work,but it sed treats it like a single word - "search_pattern1|search_pattern1". it does not give a syntaxk error.
  #6 (permalink)  
Old 07-30-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
Did you try with a backslash before the |?
  #7 (permalink)  
Old 07-30-2008
Tiger75 Tiger75 is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 4
sed - two search patterns...

Can you try the following and let me know whether it works?

sed '/search_pattern1/ !s/pattern1/pattern2/;/search_pattern2/ !s/pattern1/pattern2/' file
Closed Thread

Bookmarks

Tags
multiple patterns, regular expressions

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 02:53 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