Compare fields within a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare fields within a file
# 1  
Old 09-16-2011
Question 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 with AWK script? Basically, I am a one-line AWK,sed programmer; finding difficult to resolve.

Thank you.
# 2  
Old 09-16-2011
Welcome to the form.

Seems you know awk. Why not just try to write more than one liner.
You have already written most of the part of your condition. Its simple.
This User Gave Thanks to clx For This Post:
# 3  
Old 09-16-2011
Hi Anchal,

Thanks for your care on my learning curve..

I tried last two days and finally decided that some one needs to guide me on reading record levels such as NF,NR & etc., It would be grateful if anyone SME's just guide me how to proceed further, it would really helpful to finish my current assignment.

Thank you.
# 4  
Old 09-16-2011
Can you post the sample data (input and output)?
# 5  
Old 09-16-2011
Input
a b c d e f g
1 2 3 1 5 6 2
. 3 4 5 8 9 3

Output:
Since a have null that line needs to be removed.
And, a=d and b=g
it should print the first line in the output
1 2 3 1 5 6 2
# 6  
Old 09-16-2011
Based on the conditions and the sample data you have shown,


Code:
$ cat f7
a b c d e f g h
1 2 3 4 5 6 2 4
. 9 10 11 12 13 14 15
$ 
$ 
$ awk '$2 != "." && $4 != "." && $7 != "." && $8 != "." && $2 == $7 && $4 == $8 {print}' f7
1 2 3 4 5 6 2 4
$

I have modified the data a bit to meet the conditions.
Please note $7 is equivalent to $14 and $8 is equivalent to $16 from your original post.
# 7  
Old 09-19-2011
Hi Rachel,

Thank you so much.

Works Perfect.

Regards,
Jerald Nathan.
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

Compare fields in two files

Hi, I am trying to check two files based on certain string and field. cat f1 source=\GREP\" hi this \\ source=\SED\" skdmsmd dnksdns source=\PERL\" cat f2 source=\SED\" source=\GREP\" vlamskds amdksk m source=\AWK\" awk \here\" (3 Replies)
Discussion started by: greet_sed
3 Replies

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

5. UNIX for Advanced & Expert Users

Nawk coding trying to compare two fields in a file

I have a file. We can call file1. It has these contents. STOPLOSS Control_file 0000000010.000 STOPLOSS Audit_file_Signoff +0000000010.00 nawk '{ fmt="%3s %15s %15s %15s %21s\n"; if ($3==$6) { tpy="Successful Match"; jnme=$1;... (4 Replies)
Discussion started by: wawa
4 Replies

6. UNIX for Dummies Questions & Answers

Compare 2 fields in 2 files

I am trying to compare two files (separted by a pipe) using 2 fields (field 1,3 from fileA and 1,2 from fileB) if the two files match i want the whole record of fileA adding the extra fields left from fileB. 1. A.txt cat|floffy|12|anything|anythings cat|kitty|15|lala|lalala... (6 Replies)
Discussion started by: sabercats
6 Replies

7. Shell Programming and Scripting

compare fields in different files

HI I'm having some troubles to compare and permut diffrent fields indexed with another filed like the following example `: file1 1 1 2 2 3 3 file2 7 1 9 2 10 3 result------------------- (6 Replies)
Discussion started by: yassinegoth
6 Replies

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

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

10. Shell Programming and Scripting

Compare two arrays in sh or compare two fields

I want a soultion to compare two arrays in sh with an easy way.I want a solution to synchrose users between different AIX servers where no NIS is available. All users are meant to be same on all 10 servers. So the approach is to consider first server as master user repository and whatever the users... (0 Replies)
Discussion started by: rijeshpp
0 Replies
Login or Register to Ask a Question