Nawk coding trying to compare two fields in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Nawk coding trying to compare two fields in a file
# 1  
Old 06-23-2013
Nawk coding trying to compare two fields in a file

I have a file. We can call file1. It has these contents.
Code:
STOPLOSS  Control_file        0000000010.000       STOPLOSS    Audit_file_Signoff  +0000000010.00

Code:
nawk '{ fmt="%3s %15s %15s %15s %21s\n"; 
                     if ($3==$6) {
                       tpy="Successful Match"; jnme=$1; sts="File will be loaded"; cntrl=$3; audit=$6; 
                     }
                    else {
                        tpy="Mis-Match"; jnme=$1; sts="File will NOT be loaded"; cntrl=$3; audit=$6;
                     
                     printf (fmt, tpy,jnme,sts,cntrl,audit) }'

I'm new with nawk and I'm trying to compare the following two decimal fields in the file. If they match output comment in new file match with comment on what file it is. If it doesn't a "Mis-Match" comment with same output. I'm not getting any output from the above code. It creates the file, but it is empty

Last edited by jim mcnamara; 06-23-2013 at 11:02 PM..
# 2  
Old 06-23-2013
When you run code into a huge long line humans often have trouble reading it.
I whacked it down.

Please give me a bit more to go on. I think you want output as two files, "okay", "mismatch" for example. So file1 gets split into okay and mismatch - correct?
The names in quotes are not important, just there for clarity.
# 3  
Old 06-23-2013
yes output in two file is preferred, but could work with one

---------- Post updated at 10:15 PM ---------- Previous update was at 10:13 PM ----------

sorry not sure how this Interface works. Did you see my response? I could use either one file or two. two would be preferred
# 4  
Old 06-23-2013
This creates 2 files - "okay" "mismatch" from file1

Code:
nawk '{ fmt="%3s %15s %15s %15s %21s\n"; 
             if ($3==$6) {
                tpy="Successful Match"; 
                jnme=$1; 
                sts="File will be loaded"; 
                cntrl=$3;             
                audit=$6; 
                printf (fmt, tpy,jnme,sts,cntrl,audit) > "okay"
              }
              else {
                   tpy="Mis-Match"; 
                   jnme=$1; 
                   sts="File will NOT be loaded";                     
                   cntrl=$3; 
                   audit=$6;                     
                   printf (fmt, tpy,jnme,sts,cntrl,audit) > "mismatch"}'  file1

There is a lot of repeated code that could be cleaned up - cntrl=$3 (audit jnme) can happen before the if {} block
This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 06-24-2013
so explain something to me. Where you have "okay" is that the first output file? And where you have the "mismatch" is that the second file? and right after that is the input file?

---------- Post updated at 11:34 PM ---------- Previous update was at 11:22 PM ----------

your { } seem to be unbalanced
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is there a UNIX command that can compare fields of files with differing number of fields?

Hi, Below are the sample files. x.txt is from an Excel file that is a list of users from Windows and y.txt is a list of database account. $ head -500 x.txt y.txt ==> x.txt <== TEST01 APP_USER_PROFILE USER03 APP_USER_PROFILE TEST02 APP_USER_EXP_PROFILE TEST04 APP_USER_PROFILE USER01 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. UNIX for Advanced & Expert Users

Script to parse and compare information in two fields of file

Hello, I am working parsing a large input file1(field CFA) I have to compare the the file1 field(CFA byte 88-96) with the content of the file2(It contains only one field) and and insert rows equal in another file. Here is my code and sample input file: ... (7 Replies)
Discussion started by: GERMANOS
7 Replies

3. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

4. Shell Programming and Scripting

Compare fields within a file

Hi Folks, I have a file with 22 columns. In which, I need to remove nulls if found at $2, $4, $14 & $16. Then, needs to compare the fields such as $2 == $14 && $4 == $16. The final output will print all the fields where the above conditions are satisfied. Could you please anyone help me... (6 Replies)
Discussion started by: Jerald
6 Replies

5. Shell Programming and Scripting

Nawk script to compare records of a file based on a particular column.

Hi Gurus, I am struggling with nawk command where i am processing a file based on columns. Here is the sample data file. UM113570248|24-AUG-11|4|man1|RR211 Alert: Master Process failure |24-AUG-11 UM113570624|24-AUG-11|4|man1| Alert: Pattern 'E_DCLeDAOException' found |24-AUG-11... (7 Replies)
Discussion started by: usha rao
7 Replies

6. Shell Programming and Scripting

awk /nawk :: print the everything except the first and the last fields

format of file1 "file1.txt" 1 2 3 4 A B C XX YY ZZ AA WWW The output must contain except the first and last column the output must be 2 3 B YY ZZ AA (8 Replies)
Discussion started by: centurion_13
8 Replies

7. Shell Programming and Scripting

Need awk script to compare 2 fields in fixed length file.

Need a script that manipulates a fixed length file that will compare 2 fields in that file and if they are equal write that line to a new file. i.e. If fields 87-93 = fields 119-125, then write the entire line to a new file. Do this for every line in the file. After we get only the fields... (1 Reply)
Discussion started by: Muga801
1 Replies

8. Shell Programming and Scripting

compare fields in a file with duplicate records

Hi: I've been searching the net but didnt find a clue. I have a file in which, for some records, some fields coincide. I want to compare one (or more) of the dissimilar fields and retain the one record that fulfills a certain condition. For example, on this file: 99 TR 1991 5 06 ... (1 Reply)
Discussion started by: rleal
1 Replies

9. Shell Programming and Scripting

I need help counting the fields and field separators using Nawk

I need help counting the fields and field separators using Nawk. I have a file that has multiple lines on it and I need to read the file 1 at a time and then count the fields and field separators and then store those numbers in variables. I then need to delete the first 5 fields and the blank... (3 Replies)
Discussion started by: scrappycc
3 Replies

10. UNIX for Dummies Questions & Answers

using nawk to concate the fields

I have the the following question: # cat report.lis | nawk -F: '{ if($0 ~ "^BE NO:") print "report_"$2".lis"}' :confused: .lisrt_ 111 .lisrt_ 111 .lisrt_ 222 .lisrt_ 222 .lisrt_ 333 .lisrt_ 333 # cat report.lis | nawk -F: '{ if($0 ~ "^BE NO:") print "report_" $2}' report_ 111 report_... (2 Replies)
Discussion started by: raychu65
2 Replies
Login or Register to Ask a Question