awk - matching on 2 columns for differents lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - matching on 2 columns for differents lines
# 1  
Old 03-12-2015
awk - matching on 2 columns for differents lines

Given this file (I separated them in block to make my explanation clearer):

Code:
92157768877;Sof_deme_Fort_Email_am_%yyyy%%mm%%dd%;EMAIL;20/02/2015;1;0;0
92157768877;Sof_trav_Fort_Email_am_%yyyy%%mm%%dd%;EMAIL;20/02/2015;1;0;0

91231838895;Sof_deme_faible_Email_am;EMAIL;26/01/2015;1 0;0
91231838895;Sof_nais_faible_Email_am;EMAIL;26/01/2015;1 0;0
91231838895;Sof_deme_Faible_Email_Relance_am;EMAIL;28/01/2015;1;0;0
91231838895;Sof_nais_faible_Email_Relance_am;EMAIL;28/01/2015;1;0;0
91231838895;Sof_deme_Faible_Email_Relance_am;EMAIL;30/01/2015;1;0;0

92100709652;Sof_voya_Faible_Email_am_%yyyy%%mm%%dd%;EMAIL;11/02/2015;1;0;0
92100709652 Sof_voya_Fort_Email_am_%yyyy%%mm%%dd%;EMAIL;11/02/2015;1;0;0
92100709652;Export Voya_Fort Postal;EXPORT;13/02/2015;1;0;0

92100709634;Export Voya_Fort Postal;EXPORT;15/02/2015;1;0;0
92100709635;Deme_Voya_Fort Postal;EXPORT;16/02/2015;1;0;0

I want to get those lines that accomplish the following conditions:

For the first , second , third and 4th block
  • 1st field of 1st line and 1st field of 2nd line match
  • 4th field of 1st line and the 4th field of the 2nd line match
  • the remaining lines match with their 1st field to the 1st field of the 1st line.
  • So that the output is like this:

Code:
92157768877;Sof_deme_Fort_Email_am_%yyyy%%mm%%dd%;EMAIL;20/02/2015;1;0;0
92157768877;Sof_trav_Fort_Email_am_%yyyy%%mm%%dd%;EMAIL;20/02/2015;1;0;0
91231838895;Sof_deme_faible_Email_am;EMAIL;26/01/2015;1 0;0
91231838895;Sof_nais_faible_Email_am;EMAIL;26/01/2015;1 0;0
91231838895;Sof_deme_Faible_Email_Relance_am;EMAIL;28/01/2015;1;0;0
91231838895;Sof_nais_faible_Email_Relance_am;EMAIL;28/01/2015;1;0;0
91231838895;Sof_deme_Faible_Email_Relance_am;EMAIL;30/01/2015;1;0;0
92100709652;Sof_voya_Faible_Email_am_%yyyy%%mm%%dd%;EMAIL;11/02/2015;1;0;0
92100709652 Sof_voya_Fort_Email_am_%yyyy%%mm%%dd%;EMAIL;11/02/2015;1;0;0
92100709652;Export Voya_Fort Postal;EXPORT;13/02/2015;1;0;0

As you can see the 4th block is not taken into account as the 2nd line of the 4th block does not have a 1st identical column with the first line and also the 4th column of the second line is also not identical with the first line.

I tried with the awk solution below but something is wrong. I cannot add the fourth field condition. And how should I select the subsequent lines?

Code:
awk -F";" 'FNR==NR{a[$1]++; next} && FNR==NR{a[$4]++; next} a[$1]==2  a[$4]==2' filetestv2.txt filetestv2.txt


Last edited by vgersh99; 03-12-2015 at 01:57 PM.. Reason: code tags, please!
# 2  
Old 03-12-2015
How about:
Code:
awk -F\; '$1!=p{n=1; ln=$0; q=$4; p=$1; next} n{n=0; if($4==q)print ln}$1==p' file

I assumed that on the second line in the third block a semicolon is missing between field 1 and 2 and that in the actual input file the ar no blank lines between blocks..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk Matching Columns - Am I missing something?

I am using awk to match columns and output based on those matches. For some reason it is not printing matching columns, am I missing something? Operating system - windows with cygwin. Command that I am using: sed 's/]*,]*/,/g' $tempdir/file1 > $tempdir/file1.$$ && awk -F, 'FNR==NR{f2=$2... (7 Replies)
Discussion started by: dis0wned
7 Replies

2. Shell Programming and Scripting

How do I select certain columns with matching pattern and rest of the lines?

I want to select 2nd, 3rd columns if line has "key3" and print rest of the lines as is. # This is my sample input key1="val1" key2="val2" key3="val3" key4="val4" some text some text some text some text key1="val1" key2="val2" key3="val3" key4="val4" some text some text some text some... (3 Replies)
Discussion started by: kchinnam
3 Replies

3. Shell Programming and Scripting

awk merge matching columns

I know I'm not the first one asking this but my code still does not work: File 1: gi|1283| tRNAscan exon 87020 88058 . - . transcript_id "Parent=tRNA-Tyr5.r01"; gi|3283| tRNAscan exon 97020 97058 . + . transcript_id "Parent=tRNA-Tyr6.r01"; gi|4283| rRNAscan exon 197020 197058 . - . transcript_id... (5 Replies)
Discussion started by: 0sMoses
5 Replies

4. Shell Programming and Scripting

Merging multiple lines to columns with awk, while inserting commas for missing lines

Hello all, I have a large csv file where there are four types of rows I need to merge into one row per person, where there is a column for each possible code / type of row, even if that code/row isn't there for that person. In the csv, a person may be listed from one to four times... (9 Replies)
Discussion started by: RalphNY
9 Replies

5. Shell Programming and Scripting

awk to copy previous line matching a particular columns

Hello Help, 2356798 7689867 999 000 123678 20385907 9797 666 17978975 87468976 968978 98798 I am trying to have out put which actually look for the third column value of 9797 and then it insert line there after with first, second column value exactly as the previous line and replace the third... (3 Replies)
Discussion started by: Indra2011
3 Replies

6. Programming

Query SQL get two values differents from the same columns

Hi, I have 2 different values in the same column and two different values in other column Query 1 ins name value 1 Test 12345 1 TestV1 12/10/2014 8 Test 85435 8 TestV1 11/11/2005 9 Test 42232 9 TestV1 19/10/2000 6 Test 54321... (6 Replies)
Discussion started by: faka
6 Replies

7. Shell Programming and Scripting

Help with awk Matching columns from two files

Hello, I have two files as following: #bin chrom chromStart chromEnd name score strand observed 585 chr2 29442 29443 rs4637157 0 + C/T 585 chr2 33011 33012 rs13423995 0 + A/G 585 chr2 34502 34503 rs13386087 0 + ... (2 Replies)
Discussion started by: Homa
2 Replies

8. 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

9. Shell Programming and Scripting

awk - Matching columns between 2 files and reordering results

I am trying to match 4 colums (first_name,last_name,dob,ssn) between 2 files and when there is an exact match I need to write out these matches to a new file with a combination of fields from file1 and file2. I've managed to come up with a way to match these 2 files based on the columns (see below)... (7 Replies)
Discussion started by: ambroze
7 Replies

10. Shell Programming and Scripting

awk/sed search lines in file1 matching columns in file2

Hi All, as you can see I'm pretty new to this board. :D I'm struggling around with small script to search a few fields in another file. Basically I have file1 looking like this: 15:38:28 sz:10001 pr:14.16 15:38:28 sz:10002 pr:18.41 15:38:29 sz:10003 pr:19.28 15:38:30 sz:10004... (1 Reply)
Discussion started by: floripoint
1 Replies
Login or Register to Ask a Question