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 > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
regular expression help pls miwinter UNIX for Dummies Questions & Answers 3 06-05-2008 01:14 AM
Regular expression maxvirrozeito UNIX for Dummies Questions & Answers 1 12-14-2007 08:02 AM
regular expression and awk nickg UNIX for Dummies Questions & Answers 2 08-16-2007 05:23 PM
regular expression..help!! andy2000 UNIX for Dummies Questions & Answers 1 07-25-2007 03:31 PM
Regular Expression + Aritmetical Expression Z0mby Shell Programming and Scripting 2 05-21-2002 10:59 AM

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 03-04-2009
not4google not4google is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 15
Regular Expression - Replace if x contains y except if...

Hi all,

I have a file which contains 1000s of lines of text. I need to delete all lines with the words "Red" EXCEPT if the line also contains the word "GREEN"...

For example:

ThisIs some random text that should be red deleted
ThisIs some random text that should NOT be red deleted green

In this case the first line should be deleted totally BUT the second line should remain in the file...Any suggestions on this would be appreciated,
  #2 (permalink)  
Old 03-04-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,759
one way:
Code:
awk ' { if(index($0, "green" )>0)  {print $0; next}
          if(index($0, "red") >0 ) {next}
          print $0
        } ' inputfile > newfile
  #3 (permalink)  
Old 03-04-2009
ce9888 ce9888 is offline
Registered User
  
 

Join Date: Aug 2008
Location: Montreal, Qc, CA
Posts: 98
There is probably a more efficient way to di this...

Code:
while read line
do
     echo $line | grep -i red > /dev/null
     if [ $? -eq 0 ]
     then
         echo $line | grep -i green > /dev/null
         if [ $? -eq 0 ]
         then
              echo $line >> new_file
         fi
     else
         echo $line >> new_file
     fi
done < input_file
  #4 (permalink)  
Old 03-04-2009
ShawnMilo ShawnMilo is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 252
Code:
 cat temp.txt  | perl -ne 'if(/green/i){print;}else{print unless /red/i} '
  #5 (permalink)  
Old 03-04-2009
radoulov's Avatar
radoulov radoulov is online now Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,868
Code:
awk '/red/&&!/green/{next}1' infile
Or:
Code:
sed '/red/{/green/!d;}' infile
Or:

Code:
perl -ne'print unless/red/&&!/green/' infile
Closed Thread

Bookmarks

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 04:04 PM.


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