Grep and delete lines except the lines with strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep and delete lines except the lines with strings
# 1  
Old 12-16-2008
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 04:53 AM.. Reason: added code tags
# 2  
Old 12-16-2008
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  
Old 12-16-2008
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  
Old 12-16-2008
using sed:
Code:
 sed -i -nl -e '/\<approved\>\|\<removed\>/ {p;} ' test

# 5  
Old 12-16-2008
Quote:
Originally Posted by vj8436
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  
Old 12-16-2008
YMMV:
Code:
egrep '^(approved|removed)' test

# 7  
Old 12-16-2008
sir,

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


Last edited by Yogesh Sawant; 04-20-2009 at 04:54 AM.. Reason: added code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete lines starting with these strings

Platform : RHEL 5.8 I have text file called myapplication.log . In this file, I have around 800 lines which start with the followng three strings PWRBRKER-3493 PWRBRKER-7834 SCHEDULER-ERROR How can I delete these lines in one go ? (13 Replies)
Discussion started by: omega3
13 Replies

2. Shell Programming and Scripting

Print lines between two strings multiple occurencies (with sed, awk, or grep)

Hello, I can extract lines in a file, between two strings but only one time. If there are multiple occurencies, my command show only one block. Example, monfichier.txt contains : debut_sect texte L1 texte L2 texte L3 texte L4 fin_sect donnees inutiles 1 donnees inutiles 2 ... (8 Replies)
Discussion started by: theclem35
8 Replies

3. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

4. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

5. UNIX for Advanced & Expert Users

In a huge file, Delete duplicate lines leaving unique lines

Hi All, I have a very huge file (4GB) which has duplicate lines. I want to delete duplicate lines leaving unique lines. Sort, uniq, awk '!x++' are not working as its running out of buffer space. I dont know if this works : I want to read each line of the File in a For Loop, and want to... (16 Replies)
Discussion started by: krishnix
16 Replies

6. UNIX for Dummies Questions & Answers

Delete lines with duplicate strings based on date

Hey all, a relative bash/script newbie trying solve a problem. I've got a text file with lots of lines that I've been able to clean up and format with awk/sed/cut, but now I'd like to remove the lines with duplicate usernames based on time stamp. Here's what the data looks like 2007-11-03... (3 Replies)
Discussion started by: mattv
3 Replies

7. Shell Programming and Scripting

How to get lines started with matched strings using sed or grep for loop?

I have a huge file and want to separate it into several subsets. The file looks like: C1 C2 C3 C4 ... (variable names) 1 .... 2 .... 3 .... : 22 .... 23 .... I want to separate the huge file using the column 1, which has numbers from 1 to 23 (but there are different amount of... (8 Replies)
Discussion started by: AMBER
8 Replies

8. Shell Programming and Scripting

grep a string in the lines between 2 strings of a file

Hi , Please help me with the following problem: I have an xml file with the following lines <cisco:name> <cisco:mdNm>Cisco Device 7500 A Series</cisco:mdNm> <cisco:meNm>10.1.100.19</cisco:meNm> <cisco:ehNm>/shelf=1</cisco:ehNm> <cisco:subname> <cisco:sptp>Cisco PortA... (8 Replies)
Discussion started by: bhagirathi
8 Replies

9. Shell Programming and Scripting

Removing empty lines(space) between two lines containing strings

Hi, Please provide shell script to Remove empty lines(space) between two lines containing strings in a file. Input File : A1/EXT "BAP_BSC6/07B/00" 844 090602 1605 RXOCF-465 PDTR11 1 SITE ON BATTERY A2/EXT... (3 Replies)
Discussion started by: sudhakaryadav
3 Replies

10. Shell Programming and Scripting

using AWK see the upper lines and lower lines of the strings??

Hi experts, You cool guys already given me the awk script below- awk '/9366109380/,printed==5 { ++printed; print; }' 2008-09-14.0.log Morever, i have one more things- when i awk 9366109380, i can also see the Upper 3 lines as well as below 5 lines of that string. Line 1.... (3 Replies)
Discussion started by: thepurple
3 Replies
Login or Register to Ask a Question