Grepping only if condition matches


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grepping only if condition matches
# 8  
Old 07-22-2016
Quote:
Originally Posted by rdrtx1
Code:
awk -F, '$3==$4==$5==1' sample

This might work on some systems, but it certainly is not portable.

The standards state that there is no associativity for the == operator and some versions of awk produce the syntax error:
Code:
awk: syntax error at source line 1
 context is
	 >>> $3==$4== <<< 
awk: bailing out at source line 1

If we rewrite the expression as:
Code:
awk -F, '$3==($4==($5==1))'

then there are lots of cases where that expression will evaluate to 1 even if all three of those fields are not set to 1. For example, the above command will print any of the following lines:
Code:
a,b,1,1,1
a,b,1,0,X for any X other than 1
a,b,0,1,X for any X other than 1
a,b,0,W,X for any W other than 0 or 1 for any X

Of course, it could also be rewritten as:
Code:
awk -F, '(($3==$4)==$5)==1))'

which would print any of the following lines:
Code:
a,b,1,1,1
a,b,X,X,1 for any X
a,b,X,Y,0 for any X that is not Y

# 9  
Old 07-22-2016
Quote:
Originally Posted by rbatte1
So if your file has several fields that you can create an expression for, then that should do it.

If the separator is , then an ignored field is .*,) meaning zero or more (*) of any character (. followed by the field separator (,)

So, to count from the beginnig of the line your expression starts as ^.*,.*, to signify start of record (^) the ignore two fields. You can then tag on 1,1,1, to specify your requirements and the rest doesn't matter if it matches or not.

I think you can end up with:-
Code:
egrep "^.*,.*,1,1,1," input_file

From your sample input, I get one less line because the one starting jkhu does not have the correct field separator between fields 2 & 3.



I hope that this helps,
Robin
Note that grep will work as well as egrep (or the preferred syntax grep -E) for the RE being used in this thread.

Note also that the RE suggested works correctly only if there are exactly 6 fields (separated by 5 commas) on each input line. Since BREs and EREs use a greedy match, the RE .*, can match more than one field if there are more than 5 commas on a line. For example, that egrep command will also print the lines:
Code:
a,b,c,1,1,1,2
a,b,0,0,1,1,1,2
1,2,1,2,1,2,1,2,1,2,1,1,1,2

in addition to lines with 1 in fields 3,4, and 5 that only have 6 fields.

To make it work correctly on a line containing six commas (i.e. 7 fields), you would need to change the RE to:
Code:
.*,.*,1,1,1,.*,

and you would need to add an additional .*, to the end of that RE for each additional field in your input file.

Alternatively, we could use an RE that only matches non-comma characters in each of the first two fields:
Code:
grep '^[^,]*,[^,]*,1,1,1,' input_file

which will only print lines with 1 in fields 3, 4, and 5 as long as there are at least six fields on each line. ([^,]* is an RE that matches zero or more occurrences (*) of any character that is not a comma ([^,]) followed by a comma (,). And, the leading ^ in the entire RE anchors the match to the start of the line.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find a text and if condition matches then replace it

Need a script that can find text in a file and replace it accordingly. This is the file I have: while IFS=',' read -r f1 f2 f3 do { nohup /home/testuser/dbmaintenance/sys_offline_maintenance.sh $f1 $f2 $f3 > $f2.out & } done < "/home/testuser/dbmaintenance/week1offlineserver.txt" In... (4 Replies)
Discussion started by: singhhe
4 Replies

2. Shell Programming and Scripting

If condition return 0 even when it fails to satisfy te condition

HI My doubt may be basic one but I need to get it clarified.. When i use "if" condition that checks for many AND, OR logical conditions like if ]; then return 0 fi Even the if condition fails it returns as zero.. Any clue.. But if i add else condition like if ]; ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

3. Shell Programming and Scripting

Compare 2 files and print matches and non-matches in separate files

Hi all, I have two files, chap.txt and complex.txt. chap.txt looks like this: a d l m r k complex.txt looks like this: a c d e l m n j a d l p q r c p r m ......... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

4. Shell Programming and Scripting

Send email if the condition matches

Hello everyone, I am trying to create function or script to send email from an address book file. Here is the file format i have, Susan:Miller:M:123 Main Street:Philadelphia:PA:17101:666-645-6666:Susan.Miller@gmail.com:07/12/1979 Robert:Langan:S:32 North Avenue:San... (5 Replies)
Discussion started by: asistant
5 Replies

5. Shell Programming and Scripting

grepping multiple matches in a single string

Hi All, I'm trying to grep for 3 patterns in a string of gibberish. It so happens that each line is appended by a date/time stamp and i was able to figure out how to extract only the datetime. here is the string.. i have to display tinker tailor soldier spy Please can some help... (2 Replies)
Discussion started by: Irishboy24
2 Replies

6. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

7. HP-UX

Difference between [condition] and [[condition]] and ((condition)) when used with if condition

Executed the following if conditions .. and got different results . only (( )) gave correct o/p with all scenarios . Can anybody please let me know what is the difference between and ] and ((condition)) when used with if condition. And why each condition gave different result. 1.... (2 Replies)
Discussion started by: soumyabubun
2 Replies

8. Shell Programming and Scripting

Please help on grepping

Hi All, I have a log file and I want to parse the logfile with a script.A sample text is shown below: I would grep on "return code " on this file. Any idea how the lines above and below the grep patterns could also be extracted. Thanks! nua7 The runLoggingInstall return code is 0... (3 Replies)
Discussion started by: nua7
3 Replies

9. Shell Programming and Scripting

grepping around

Using shell scripts, I use grep to find the word “error” in a log file: grep error this.log. How can I print or get the line 3 lines below the line that word “error” is located? Thanks in advance for your response. (9 Replies)
Discussion started by: cbeauty
9 Replies

10. UNIX for Dummies Questions & Answers

grepping

Is there a way to grep for something and then print out 10 lines after it. for example if I want to grep for a word, then output the following 10 or whatever number of lines after the word. (5 Replies)
Discussion started by: eloquent99
5 Replies
Login or Register to Ask a Question