awk find the first matching row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk find the first matching row
# 1  
Old 09-29-2014
awk find the first matching row

I would like to find the first matching row and continue to search from the matching row. if column3 equal to 1 in the row, find the next matching row based on the criteria below. column3 in the matching row equal to 0 and column2 equal to column2 in the matching row



test.csv - test data
Code:
2014-09-01 00:01:30|T1|1|0|RE|Y
2014-09-01 00:03:05|T1|0
2014-09-17 23:28:13|T2|1|1|RE|N
2014-09-17 23:29:14|T2|1|0|RE|N      
2014-09-17 23:31:16|T2|0
2014-09-17 23:31:18|T2|0

expected output
Code:
 
2014-09-01 00:01:30|2014-09-01 00:03:05|T1|0|RE|Y
2014-09-17 23:28:13|2014-09-17 23:31:16|T2|0|RE|N

I have tried using the awk below, but the result is not correct.
Code:
 
awk '{if (($3=="0" && $2==B[2] && B[3]=="1"))
{print B[1], $1, B[2], B[4], B[5], B[6]; next}
{split($0,B)}}' FS=\| OFS=\| test.csv

result
Code:
 
2014-09-01 00:01:30|2014-09-01 00:03:05|T1|0|RE|Y
2014-09-17 23:29:14|2014-09-17 23:31:16|T2|0|RE|N
2014-09-17 23:29:14|2014-09-17 23:31:18|T2|0|RE|N

# 2  
Old 09-29-2014
What criteria do you want?

You have shown a program which doesn't do what you want. That's unfortunately not actually helpful in describing what you actually do want. If it was, it'd work, and you wouldn't need to ask.

I see three lines with column 3 as 1, but apparently you only want two of them? Please explain.
# 3  
Old 09-29-2014
Quote:
Originally Posted by Corona688
What criteria do you want?

You have shown a program which doesn't do what you want. That's unfortunately not actually helpful in describing what you actually do want. If it was, it'd work, and you wouldn't need to ask.

I see three lines with column 3 as 1, but apparently you only want two of them? Please explain.
Yes there are 3 lines with column3 as 1.
The matching row for the third line (2014-09-17 23:28:13|T2|1|1|RE|N) is fifth line (2014-09-17 23:31:16|T2|0).
The result will be 2014-09-17 23:28:13|2014-09-17 23:31:16|T2|0|RE|N

After that, i would like to continue to search from fifth line to find the line with column3 as 1.
# 4  
Old 09-29-2014
the description is a bit 'muddy', but see if that helps.
awk -f chail.awk myFile where chail.awk is:
Code:
BEGIN {
  FS=OFS="|"
}
$3=="1" && !($2 in f) {
   f[$2]=$0
   next
}
$3=="0" && $2 in f {
  n=split(f[$2], a, FS)
  printf("%s%c%s%c%s%c%s%c%s\n", a[1], OFS, $1, OFS, $2, OFS, $3, OFS, a[5])
}

# 5  
Old 09-29-2014
Try:
Code:
awk '$3==1 && !p{$3=$2; p=$0} $3==0 && p{n=$1; $0=p; $2=n; p=x; print}' FS=\| OFS=\| file

---
EDIT FS and OFS had fallen off.... Thenks Ravinder...

Last edited by Scrutinizer; 09-29-2014 at 01:32 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 09-29-2014
Quote:
Originally Posted by Scrutinizer
Try:
Code:
awk '$3==1 && !p{$3=$2; p=$0} $3==0 && p{n=$1; $0=p; $2=n; p=x; print}' file

Hello Scrutinizer,

Nice code, I think we can add FS, OFS values to same.

Code:
awk -F"|" '$3==1 && !p{$3=$2; p=$0} $3==0 && p{n=$1; $0=p; $2=n; p=x; print}' OFS="|" Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 09-29-2014
Code:
awk -F'|' '$3==1 && !a[$2] {a[$2]=$1; b[$2]=($5 OFS $6); next} $3==0 && a[$2] {print a[$2], $0, b[$2]; delete a[$2]}' OFS='|' file

Output:
Code:
2014-09-01 00:01:30|2014-09-01 00:03:05|T1|0|RE|Y
2014-09-17 23:28:13|2014-09-17 23:31:16|T2|0|RE|N

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to combine all matching dates and remove non-matching

Using the awk below I am able to combine all the matching dates in $1, but I can not seem to remove the non-matching from the file. Thank you :). file 20161109104500.0+0000,x,5631 20161109104500.0+0000,y,2 20161109104500.0+0000,z,2 20161109104500.0+0000,a,4117... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Comparing same column from two files, printing whole row with matching values

First I'd like to apologize if I opened a thread which is already open somewhere. I did a bit of searching but could quite find what I was looking for, so I will try to explaing what I need. I'm writing a script on our server, got to a point where I have two files with results. Example: File1... (6 Replies)
Discussion started by: mitabrev83
6 Replies

3. UNIX for Dummies Questions & Answers

find Search - Find files not matching a pattern

Hello all, this is my first and probably not my last question around here. I do hope you can help or at least point me in the right direction. My question is as follows, I need to find files and possible folders which are not owner = AAA group = BBB with a said location and all sub folders ... (7 Replies)
Discussion started by: kilobyter
7 Replies

4. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

5. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

6. Shell Programming and Scripting

Find min.max value if matching columns found using AWK

Input_ File : 2 3 4 5 1 1 0 1 2 1 -1 1 2 1 3 1 3 1 4 1 6 5 6 6 6 6 6 7 6 7 6 8 5 8 6 7 Desired output : 2 3 4 5 -1 1 4 1 6 5 6 8 5 8 6 7 (3 Replies)
Discussion started by: vasanth.vadalur
3 Replies

7. Shell Programming and Scripting

Printing entire field, if at least one row is matching by AWK

Dear all, I have been trying to print an entire field, if the first line of the field is matching. For example, my input looks something like this. aaa ddd zzz 123 987 126 24 0.650 985 354 9864 0.32 0.333 4324 000 I am looking for a pattern,... (5 Replies)
Discussion started by: Chulamakuri
5 Replies

8. Shell Programming and Scripting

awk command : row by row merging of two files

I want to write a scrpit to merge files row wise (actually concatinating) main.txt X Y Z file 1 A B C file 2 1 2 3 now i want the script to check if the file1 is empty or not, if empty then make it like A B C 1 2 3 again to check if second file is empty if not do as done... (0 Replies)
Discussion started by: shashi792
0 Replies

9. UNIX for Dummies Questions & Answers

To find the Row number

Hi Can any one tell me what is the command to find out the row id or row number for a particular record Thanks sri (6 Replies)
Discussion started by: laxmi131
6 Replies

10. Shell Programming and Scripting

SED: delete matching row and 4 next rows?

Hi, Tried to look for solution, and found something similar but could not adapt the solution for my needs.. I'm trying to match a pattern (in this case "ProcessType")in a logfile, then delete that line and the 4 following lines. The logfile looks as follows: ProcessType: PROCESS_A... (5 Replies)
Discussion started by: Juha
5 Replies
Login or Register to Ask a Question