Need to delete all lines where any line meets a condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to delete all lines where any line meets a condition
# 1  
Old 10-18-2012
Need to delete all lines where any line meets a condition

Here is an example of a file...

Code:
foo1,good
foo1,good
foo2,error
foo2,good

Note that both rows for foo1 have good in the 2nd field, but one of the foo2 rows has error...

I need something in ksh/awk/perl that will delete ALL foo2 lines if ANY of them have error in the 2nd field...so:

Code:
foo1,good
foo1,good
foo2,error
foo2,good

becomes:

Code:
foo1,good
foo1,good

Please help...I know it should be simple, but I can't figure it out.

Thanks!

Last edited by Scrutinizer; 10-18-2012 at 03:12 PM.. Reason: code tags
# 2  
Old 10-18-2012
try this...

Code:
grep -v $(awk -F, '$2 ~ /error/{print $1}' file1) file1

# 3  
Old 10-18-2012
Code:
{ grep -q "foo2,error" file1 && grep -v "foo2," file1 ; } || cat file1


Last edited by rdrtx1; 10-18-2012 at 03:11 PM.. Reason: To cover not finding error.
# 4  
Old 10-18-2012
awk version:
Code:
awk -F, '$2=="error"{F[$1]} FNR!=NR && !($1 in F)' infile infile

(the input file is specified twice)
# 5  
Old 10-24-2012
Sorry for the late replies...I've been trying to figure this out as much as possible myself and I'm just plain stuck...

Quote:
Originally Posted by Scrutinizer
awk version:
Code:
awk -F, '$2=="error"{F[$1]} FNR!=NR && !($1 in F)' infile infile

(the input file is specified twice)
This gives me
awk: syntax error near line 1
awk: bailing out near line 1


Quote:
Originally Posted by rdrtx1
Code:
{ grep -q "foo2,error" file1 && grep -v "foo2," file1 ; } || cat file1

My flavor of Unix doesn't support grep -q


Quote:
Originally Posted by pamu
try this...
Code:
grep -v $(awk -F, '$2 ~ /error/{print $1}' file1) file1

This simply displays my file as is and doesn't make any changes.
# 6  
Old 10-24-2012
it can't write to file. you need to redirect it.

Code:
grep .... file > temp 
mv temp file

also try with nawk

Last edited by pamu; 10-24-2012 at 12:04 PM..
# 7  
Old 10-24-2012
try:
Code:
awk -F, '
{
 a[NR]=$0;
 if ($0 ~ /foo2.*error/) e=$1;
}
END {
 for (i=1; i<=NR; i++) {
   if (!e) {
     print a[1];
   } else {
     if (a[i] !~ e) print a[i];
   }
 }
}
' file1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print lines based on line number and specified condition

Hi, I have a file like below. 1,2,3,4,5,6,7,8,9I would like to print or copied to a file based of line count in perl If I gave a condition 1 to 3 then it should iterate over above file and print 1 to 3 and then again 1 to 3 etc. output should be 1,2,3 4,5,6 7,8,9 (10 Replies)
Discussion started by: Anjan1
10 Replies

2. UNIX for Dummies Questions & Answers

Remove when substring meets condition

Hi Masters, I need to remove lines when date format is below certain date My file input 20140906|ALASKA|USASEL|TARPUNG|2014-03-01|82176614040|20|1 20140906|ALASKA|USATENG|CRUIEX|2014-08-01|81267079997|5|0 20140906|ALASKA|USASEL|CRUIEMBANG|2013-10-01|82280779814|9|0... (4 Replies)
Discussion started by: radius
4 Replies

3. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

4. Shell Programming and Scripting

Delete lines from file based on condition

I want to keep last 2 days data from a file and want to delete others data from the file. Please help me. Sample Input # cat messages-2 Apr 15 11:25:03 test1 kernel: imklog 4.6.2, log source = /proc/kmsg started. Apr 15 11:25:03 test1 rsyslogd: (re)start Apr 16 19:42:03 test1 kernel:... (2 Replies)
Discussion started by: makauser
2 Replies

5. Shell Programming and Scripting

Delete line with sed or awk (with specified condition)

Hello. I'm trying to delete the lines of a file does not contain the letter "T " (for example) at position 26. So far, I could only print the result: awk '{if (substr ($ 1,1,26)! ~ / T /) print}' file.txt How I can do to eliminate the lines that meet this condition? Help please. ... (4 Replies)
Discussion started by: </kida>
4 Replies

6. Shell Programming and Scripting

script to replace numbers on lines according to condition on the same line

hello everyone my file contains many records, the following is a sample: BEGIN ASX1500000050002010120000000308450201012000177 ASX1100002000000201012000000038450201012000220 ASX1600100005000201012000000038450020101200177 ASX1900100006000201067000000058450020101200177... (2 Replies)
Discussion started by: neemoze
2 Replies

7. Shell Programming and Scripting

awk to print lines based on string match on another line and condition

Hi folks, I have a text file that I need to parse, and I cant figure it out. The source is a report breaking down softwares from various companies with some basic info about them (see source snippet below). Ultimately what I want is an excel sheet with only Adobe and Microsoft software name and... (5 Replies)
Discussion started by: rowie718
5 Replies

8. Shell Programming and Scripting

How to broadcast the message if any condition meets

Hi All, Can any1 help me out in broadcasting a message to all users if a condtion is meet. Like I am trying to get values from a directory for service monitoring. If a condition is meet it should broadcast the message. I try to use wall command but i m not sure how its works as its... (1 Reply)
Discussion started by: jojo123
1 Replies

9. Shell Programming and Scripting

concatenate and display 2 lines as 1 with a condition for 2 line ?

I have 2 pattern of lines (SQL query and Time taken)in a log i need to capture all SQL queries with time taken >20 sec and need to display as one line. 2 lines from log: 2007-10-23 11:39:17,061 DEBUG - SQL Query : SELECT A.GROUP_CD , C.FN_CD FROM UP_GROUP A , PRD_GROUP_TO_FN B , PRD_FN... (7 Replies)
Discussion started by: vithala
7 Replies

10. UNIX for Advanced & Expert Users

Need solution concatenate and display 2 lines as 1 with a condition for 2 line ?

I have 2 pattern of lines (SQL query and Time taken)in a log i need to capture all SQL queries with time taken >20 sec and need to display as one line. 2 lines from log: 2007-10-23 11:39:17,061 DEBUG - SQL Query : SELECT A.GROUP_CD , C.FN_CD FROM UP_GROUP A , PRD_GROUP_TO_FN B , PRD_FN... (1 Reply)
Discussion started by: vithala
1 Replies
Login or Register to Ask a Question