Grepping only if condition matches


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grepping only if condition matches
# 1  
Old 07-21-2016
Grepping only if condition matches

Dear Friends,

I have a flat file which is as follows

Code:
$cat sample
123,456,1,1,1,1
sdfas,345,1,1,1,1
dfgd,234,2,3,4,1
ggffgr,234,4,3,2,1
jkhu,354.1,1,1,1
$

I want to get output of only those lines which has '1' in 3 to 5 position.

So I want output as follows

Code:
123,456,1,1,1,1
sdfas,345,1,1,1,1
jkhu,354.1,1,1,1

Kindly guide.
Anu.



Moderator's Comments:
Mod Comment ... more code tags.

Last edited by zaxxon; 07-21-2016 at 05:16 AM..
# 2  
Old 07-21-2016
What have you tried so far?
# 3  
Old 07-21-2016
I didn't try anything as I know way out only by using "If" statement which unfortunately I do not want to use.
Hence seeking for guidance who know grep well.
# 4  
Old 07-21-2016
Please search forum before posting, kind of question you asked now, repeated several times on fora with different data,

you may try awk, its easy Smilie

Code:
[akshay@localhost tmp]$ cat sample
123,456,1,1,1,1
sdfas,345,1,1,1,1
dfgd,234,2,3,4,1
ggffgr,234,4,3,2,1
jkhu,354.1,1,1,1

[akshay@localhost tmp]$ awk -F, '$3 == 1 &&  $4 == 1 && $5 == 1' sample
123,456,1,1,1,1
sdfas,345,1,1,1,1
jkhu,354.1,1,1,1

[akshay@localhost tmp]$ awk  -F, '{j=1;for(i=3; i<=5; i++)j*=$i==1}j' sample
123,456,1,1,1,1
sdfas,345,1,1,1,1
jkhu,354.1,1,1,1

# 5  
Old 07-21-2016
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
# 6  
Old 07-21-2016
Code:
awk -F, '$3==$4==$5==1' sample

# 7  
Old 07-22-2016
Thank you friends for the help which was much needed. Special thanks to Mr. rbatte1 for taking extra efforts for step by step guiding.
Thank you.
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