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
using AWK see the upper lines and lower lines of the strings?? thepurple Shell Programming and Scripting 3 10-07-2008 10:24 AM
How to delete first 5 lines and last five lines in all text files ragavendran31 Shell Programming and Scripting 10 02-21-2008 07:58 AM
How to print only lines in between two strings using awk jisha Shell Programming and Scripting 4 01-11-2008 04:13 AM
How to count lines - ignoring blank lines and commented lines kthatch UNIX for Dummies Questions & Answers 6 05-25-2007 01:21 AM
delete blank lines or lines with spaces only vascobrito UNIX for Dummies Questions & Answers 3 01-13-2004 07:36 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 12-16-2008
vj8436 vj8436 is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 25
Grep and delete lines except the lines with strings

Hi

I am writing a script which should read a file and search for certain strings 'approved' or 'removed' and retain only those lines that contain the above strings.

Ex: file name 'test'
test:
Code:
approved package
waiting for approval package
disapproved package
removed package
approved package
approved package
Here i need to delete all the lines except those that contain either 'approved' or 'removed'.

How can i do that.

Last edited by Yogesh Sawant; 04-20-2009 at 03:53 AM.. Reason: added code tags
  #2 (permalink)  
Old 12-16-2008
SFNYC SFNYC is offline
Registered User
  
 

Join Date: Jun 2008
Location: New York City
Posts: 95
Code:
$ cat test
approved package
waiting for approval package
disapproved package
removed package
approved package
approved package

$ egrep -E 'approved|removed' test
approved package
disapproved package
removed package
approved package
approved package
  #3 (permalink)  
Old 12-16-2008
vj8436 vj8436 is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 25
Thank you ... but i have two problems

1) egrep -E does not work. it returns 'Invalid options'

2) as per the above command 'disapproved' is not removed from file
i want that line also to be removed with 'removed' or 'approved' string option.
  #4 (permalink)  
Old 12-16-2008
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
using sed:
Code:
 sed -i -nl -e '/\<approved\>\|\<removed\>/ {p;} ' test
  #5 (permalink)  
Old 12-16-2008
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
Quote:
Originally Posted by vj8436 View Post
as per the above command 'disapproved' is not removed from file
i want that line also to be removed with 'removed' or 'approved' string option.
what you need is word boundaries:
Code:
 egrep -E '\<approved\>|\<removed\>' test

Last edited by Yogesh Sawant; 12-16-2008 at 12:52 PM..
  #6 (permalink)  
Old 12-16-2008
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
YMMV:
Code:
egrep '^(approved|removed)' test
  #7 (permalink)  
Old 12-16-2008
vj8436 vj8436 is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 25
sir,

i get the reply as
Code:
sed: illegal option -- i

Last edited by Yogesh Sawant; 04-20-2009 at 03:54 AM.. Reason: added code tags
Sponsored Links
Closed Thread

Bookmarks

Tags
egrep or, sed regex

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 09:48 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