Looking for AWK Solution for column comparison in a single file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looking for AWK Solution for column comparison in a single file
# 1  
Old 05-25-2010
Looking for AWK Solution for column comparison in a single file

- I am looking for different kind of awk solution which I don't think is mentioned before in these forums.

Number of rows in the file are fixed
Their are two columns in file1.txt

1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10


I am looking for 3 scenarios.
- If column 1 and 2 are same then generate PASS in Third Column (I have figured out this).
- If a number in Column 1 is less than column 2 (which means a number is missing in column 2)then
--generate FAIL in Column 3.
-- Move the number in Column 2 of that row to next row. Move all the numbers in column 2 after that number in thier next rows.
-- generate MISSING In Column 2.

-- Below is the example

file1.txt
1 1
2 3
3 4
4 5
5 7
6 8
7 9
8 10
9
10

As per second scenario what I want is

file1.txt
1 1 PASS
2 MISSING FAIL
3 3 PASS
4 4 PASS
5 5 PASS
6 MISSING FAIL
7 7 PASS
8 8 PASS
9 9 PASS
10 10 PASS

- If a number in Column 1 is greater than column 2 (which means a number is missing in column 1)then
--generate FAIL in Column 3.
-- Move the number in Column 1 of that row to next row. Move all the numbers in column 1 after that row in their next rows.
-- generate MISSING in Column 1.
-- Below is the example

file1.txt
1 1
3 2
4 3
5 4
7 5
8 6
9 7
10 8
9
10

As per third scenario what I want is

file2.txt
1 1 PASS
MISSING 2 FAIL
3 3 PASS
4 4 PASS
5 5 PASS
MISSING 6 FAIL
7 7 PASS
8 8 PASS
9 9 PASS
10 10 PASS


- I have figured out PASS FAIL logic which is basically comparison between 2 columns. But I am not able to figure out moving rows logic.
- Any help for this is really appreciated.
# 2  
Old 05-25-2010
Try and adapt the following AWK program :
Code:
awk '
{ f1[NR] = $1 ; f2[NR] = $2 }
END {
   for (i=1; i<=NR; i++) {
      if (f1[i] == f2[i]) {
         f3 = "PASS"
      } else if (f1[i] < f2[i]) {
         for (j=NR; j>i; j--)
            f2[j] = f2[j-1];
         f2[i] = "MISSING";
         f3    = "FAIL";
     } else {
        for (j=NR; j>i; j--) {
           if (f2[j] == "")
              f2[j] = f1[j];
           f1[j] = f1[j-1];
        }
        f1[i] = "MISSING";
        f3    = "FAIL";
     }
     print f1[i], f2[i], f3
   }
}
' inputfile

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input "A","B","C,D","E","F" "S","T","U,V","W","X" "AA","BB","CC,DD","EEEE","FFF" required output: "A","B","C,D","C,D","F" "S", T","U,V","U,V","X" "AA","BB","CC,DD","CC,DD","FFF" tried using awk but double quotes not preserving for every field. any help to solve this is much... (5 Replies)
Discussion started by: khblts
5 Replies

2. Shell Programming and Scripting

Paste 2 single column files to a single file

Hi, I have 2 csv/txt files with single columns. I am trying to merge them using paste, but its not working.. output3.csv: flowerbomb everlon-jewelry sofft steve-madden dolce-gabbana-watchoutput2.csv: http://www1.abc.com/cms/slp/2/Flowerbomb http://www1.abc.com/cms/slp/2/Everlon-Jewelry... (5 Replies)
Discussion started by: ajayakunuri
5 Replies

3. Shell Programming and Scripting

Range Comparison Of Column Value in File1 with Other File

Hi, I have a file1 whose 17th column needs to be checked if it exists in between the values of column 2 & column 3 as mentioned in another file2. Output of the matched value to be put in separate file 3 & 4. File1: ... (10 Replies)
Discussion started by: siramitsharma
10 Replies

4. UNIX for Dummies Questions & Answers

awk solution to duplicate lines based on column

Hi experts, I have a tab-delimited file with one column containing values separated by a comma. I wish to duplicate the entire line for every value in that comma-delimited field. For example: $cat file 4444 4444 4444 4444 9990 2222,7777 6666 2222 ... (3 Replies)
Discussion started by: torchij
3 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

awk column comparison big file

Hi all, I would like to compare a column in one file to a column in another file and when there is a match it prints the first column and the corresponding second column. Example File1 ABA ABC ABE ABF File 2 ABA 123 ABB 124 ABD 125 ABC 126 So what I would like printed to a... (6 Replies)
Discussion started by: pcg
6 Replies

7. Shell Programming and Scripting

column value comparison in a file

Hi, Can any one help with my below requirement. i need to compare each line by line and in each line i have to compare some columns values with previous line column values in perl script. Can any one help me........! its very urgent. Thanks (3 Replies)
Discussion started by: jam_prasanna
3 Replies

8. Shell Programming and Scripting

grep/awk on a single column in a for loop

Hi I'm trying to loop through a small list of id's and then pull out a few columns if the id matches that found in column 2 of the larger file. I managed to get one command to work awk -F " " '{if ($2 == '154080196') print $2,$3,$4}' tst.txt | less However, when I try it in a for loop I... (3 Replies)
Discussion started by: flotsam
3 Replies

9. UNIX for Advanced & Expert Users

Solution for the Massive Comparison Operation

Hi We have 50 million records in mainframes DB2. We have a requirement to Record the Change Data Capture(CDC) records. i.e New Records or Updated Records that were added into the DB2. Unfortunately we dont have any column indicators to give the details of the changes made to the records. ... (8 Replies)
Discussion started by: raghav288
8 Replies

10. Shell Programming and Scripting

Single column to multiple columns in awk

Hi - I'm new to the awk programming language. I'm trying to print a single column of data to several columns, and I found an article on iTWorld.com (ITworld.com - Printing in columns). It looks like the mkCols2 script is very close to what I need to do, but it looks like the end of the code... (2 Replies)
Discussion started by: astroDave
2 Replies
Login or Register to Ask a Question