Compare Values between column in the same file


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Compare Values between column in the same file
# 22  
Old 09-30-2016
Quote:
Originally Posted by Nina2910
would it possible if code can add also "Bad" to value which is suppose to be zero but it is not.

Code:
 
 COLUMN1 COLUMN2 COLUMN3 COLUMN4 COLUMN5 COLUMN6 SMS Email AO Mail Post N Cell Newcolumn
VEGE Potato E W 396 12 0 384 0 0 0 0 0 GOOD
VEGE Onion S W 17 0 17 0 0 0 0 0 0 GOOD
FRUIT APPLE N W 549 61 0 0 0 0 0 488 0 GOOD
 FRUIT APPLE SE W 291 14 239 38 0 Bad10 0 0 0 WARNING
FRUIT APPLE EAMS W 397 32 309 56 309 309 0 0 0 GOOD
FRUIT APPLE SEA W 808 58 663 87 488 Bad20 0 0 0 WARNING

like I added bad in row number 5 and 7.
Of course it would be possible. Why don't you try modifying one or more of the suggestions that have been provided to you by the volunteers who have posted solutions to your original problem to meet your new requirements. If you get stuck, show us what you have done and we'll try to help you fill in the missing pieces.
# 23  
Old 09-30-2016
@Don ..I have tried multiple things but none of them seems working

one of those :-
Code:
 awk '
BEGIN { # Initialize skip array: S[fn] = char
# If field #3 contains the character specified by char, do NOT check
# the contents of field #fn.
S[7] = "S"
S[8] = "E"
S[9] = "A"
S[10] = "M"
S[11] = "P"
S[12] = "N"
S[13] = "C"
}
NR < 2 {# Add the requested heading field to the header line...
print $0, "Newcolumn"
# and skip to the next input line.
next
}
{ # For all other input lines, check fields 7-13 inclusive:
for(i = 7; i <= 13; i++)
# If the character corresponding to the field is not prsent in
# field #3 AND the field contains a non-zero value...
if($3 !~ S[i] && $i)
# break out of the loop.
break
# Print the input line followed by "WARNING" if a non-zero value was
# found in a field to be checked; otherwise, print the input line
# followed by "GOOD".
{ if ($i != 0) $i="Bad"$i; print $0 },(i <= 13) ? "WARNING" : "GOOD
}'


Last edited by Nina2910; 09-30-2016 at 01:15 PM..
# 24  
Old 09-30-2016
Guessing that you want to flag every non-ignored non-null field, try
Code:
awk '
NR == 1 {MX = split ("      SEAMPNC", CH, _)
         print $0, "NewColumn"
         next
        }
        {SUM = 0
         for (i = 7; i<=13; i++) if ($3 !~ CH[i])       {SUM += $i
                                                         if ($i) $i = "Bad" $i
                                                        }
         $(NF+1) = SUM?"WARNING":"GOOD"
        }
1
 ' file
COLUMN1 COLUMN2 COLUMN3 COLUMN4 COLUMN5 COLUMN6 SMS Email AO Mail Post N Cell NewColumn
VEGE Potato E W 396 12 0 384 0 0 0 0 0 GOOD
VEGE Onion S W 17 0 17 0 0 0 0 0 0 GOOD
FRUIT APPLE N W 549 61 0 0 0 0 0 488 0 GOOD
FRUIT APPLE SE W 291 14 239 38 0 Bad10 0 0 0 WARNING
FRUIT APPLE EAMS W 397 32 309 56 309 309 0 0 0 GOOD
FRUIT APPLE SEA W 808 58 663 87 488 Bad20 0 0 0 WARNING

This User Gave Thanks to RudiC For This Post:
# 25  
Old 09-30-2016
Or, if you want a modified version of my commented script, you could try:
Code:
awk '
BEGIN {	# Initialize skip array: S[fn] = char
	# If field #3 contains the character specified by char, do NOT check
	# the contents of field #fn.
	S[7] = "S"
	S[8] = "E"
	S[9] = "A"
	S[10] = "M"
	S[11] = "P"
	S[12] = "N"
	S[13] = "C"
}
NR < 2 {# Add the requested heading field to the header line...
	print $0, "Newcolumn"
	# and skip to the next input line.
	next
}
{	# Clear warning flag.
	w = 0
	# For all other input lines, check fields 7-13 inclusive:
	for(i = 7; i <= 13; i++)
		# If the character corresponding to the field is not prsent in
		# field #3 AND the field contains a non-zero value...
		if($3 !~ S[i] && $i) {
			# Mark the field bad and set the warning flag.
			w = 1
			$i = "Bad" $i
		}
	# Print the input line followed by "WARNING" if a non-zero value was
	# found in a field to be checked; otherwise, print the input line
	# followed by "GOOD".
	print $0, w ? "WARNING" : "GOOD"
}' file2

which, if file2 contains:
Code:
COLUMN1 COLUMN2 COLUMN3 COLUMN4 COLUMN5 COLUMN6 SMS Email AO Mail Post N Cell
VEGE Potato E W 396 12 0 384 0 0 0 0 0
VEGE Onion S W 17 0 17 0 0 0 0 0 0
FRUIT APPLE N W 549 61 0 0 0 0 0 488 0
FRUIT APPLE SE W 291 14 239 38 0 10 0 0 0
FRUIT APPLE EAMS W 397 32 309 56 309 309 0 0 0
FRUIT APPLE SEA W 808 58 663 87 488 20 0 0 0
TREE Lemon S W 17 0 1 2 3 4 5 6 7
TREE Orange N W 17 0 10 20 30 40 50 60 70
TREE Lime NS W 17 0 100 200 300 400 500 600 700
TREE Tangerine SEAMPC W 17 0 100 200 300 400 500 600 700

produces the output:
Code:
COLUMN1 COLUMN2 COLUMN3 COLUMN4 COLUMN5 COLUMN6 SMS Email AO Mail Post N Cell Newcolumn
VEGE Potato E W 396 12 0 384 0 0 0 0 0 GOOD
VEGE Onion S W 17 0 17 0 0 0 0 0 0 GOOD
FRUIT APPLE N W 549 61 0 0 0 0 0 488 0 GOOD
FRUIT APPLE SE W 291 14 239 38 0 Bad10 0 0 0 WARNING
FRUIT APPLE EAMS W 397 32 309 56 309 309 0 0 0 GOOD
FRUIT APPLE SEA W 808 58 663 87 488 Bad20 0 0 0 WARNING
TREE Lemon S W 17 0 1 Bad2 Bad3 Bad4 Bad5 Bad6 Bad7 WARNING
TREE Orange N W 17 0 Bad10 Bad20 Bad30 Bad40 Bad50 60 Bad70 WARNING
TREE Lime NS W 17 0 100 Bad200 Bad300 Bad400 Bad500 600 Bad700 WARNING
TREE Tangerine SEAMPC W 17 0 100 200 300 400 500 Bad600 700 WARNING

This User Gave Thanks to Don Cragun For This Post:
# 26  
Old 09-30-2016
@Rudi.Thank you code worked as expected.

@Don it is adding Bad to all the values from column 7 to 13 if there is warning. I just want to add to the value which is not suppose to be zero.

Code:
COLUMN1 COLUMN2 COLUMN3 COLUMN4 COLUMN5 COLUMN6 SMS Email AO Mail Post N Cell NewColumn
VEGE Potato E W 396 12 0 384 0 0 0 0 0 GOOD
VEGE Onion S W 17 0 17 0 0 0 0 0 0 GOOD
FRUIT APPLE N W 549 61 0 0 0 0 0 488 0 GOOD
FRUIT APPLE SE W 291 14 239 38 0 Bad10 0 0 0 WARNING
FRUIT APPLE EAMS W 397 32 309 56 309 309 0 0 0 GOOD
FRUIT APPLE SEA W 808 58 663 87 488 Bad20 0 0 0 WARNING

# 27  
Old 09-30-2016
Quote:
Originally Posted by Nina2910
...

@Don it is adding Bad to all the values from column 7 to 13 if there is warning. I just want to add to the value which is not suppose to be zero.

...
Hi Nina2910,
No, it is not.

If you look at post #25 again, you will see that it adds "Bad" to every value in columns 7 through 13 that would cause the final field to contain" WARNING" and does not modify any value in columns 7 through 13 that would not cause the final field to be set to "WARNING".
# 28  
Old 09-30-2016
Sorry about that yes I see it now and tested it thank you so much.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare values in multiple rows in one column using awk

I would like to compare values in column 8, and grep the ones where the different is > 1, columns 1 and 2 are the key for array. Every 4 rows the records values in columns 1 and 2 changed. Then, the comparison in the column 8 need to be done for the 4 rows everytime columns 1 and 2 changed ... (4 Replies)
Discussion started by: jiam912
4 Replies

2. Shell Programming and Scripting

Compare two files column values using awk

Judi # cat File1 judi /export/home 76 judi /usr 83 judi # judi # cat File2 judi /export/home 79 judi /usr 82 judi # if COLUMN3 of File2 is greater that COLUMN3 of File1, then print File2's lines juid /export/home 79 Code tags please (2 Replies)
Discussion started by: judi
2 Replies

3. Shell Programming and Scripting

How to compare the values of a column in a same file using awk?

Dear Unix experts, I have got a file where I would like to compare the values of second column if first column is same in such a way that the difference between the values is >50. If not, I would like to discard both values. For example, my input file looks like - comp275_c0_seq2 73... (7 Replies)
Discussion started by: utritala
7 Replies

4. UNIX for Dummies Questions & Answers

Compare values of fields from same column with awk

Hi all ! If there is only one single value in a column (e.g. column 1 below), then return this value in the same output column. If there are several values in the same column (e.g. column 2 below), then return the different values separated by "," in the output. pipe-separated input: ... (11 Replies)
Discussion started by: lucasvs
11 Replies

5. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

6. Shell Programming and Scripting

Take values from a column and put it in a variable and compare

Hi, I have a table in unix from which i want to read the contents line by line, then filter out the values from 6th column one by one and compare it a fixed value. How to do this? (7 Replies)
Discussion started by: arijitsaha
7 Replies

7. Shell Programming and Scripting

How to compare the values of a column in awk in a same file and consecutive lines..

I would like to compare the values of 2nd column of consecutive lines of same file in such a way so that if the difference between first value and second value is more than 100 it should print complete line else ignore line. Input File ========== PDB 2500 RTDB 123 RTDB-EAGLE 122 VSCCP 2565... (4 Replies)
Discussion started by: manuswami
4 Replies

8. UNIX for Dummies Questions & Answers

Compare two files using awk or sed, add values in a column if their previous fields are same

Hi All, I have two files file1: abc,def,ghi,5,jkl,mno pqr,stu,ghi,10,vwx,xyz cba,ust,ihg,4,cdu,oqw file2: ravi,def,kishore ramu,ust,krishna joseph,stu,mike I need two output files as follows In my above example, each row in file1 has 6 fields and each row in file2 has 3... (1 Reply)
Discussion started by: yerruhari
1 Replies

9. UNIX for Advanced & Expert Users

Compare two files using awk or sed, add values in a column if their previous fields are same

Hi All, I have two files file1: abc,def,ghi,5,jkl,mno pqr,stu,ghi,10,vwx,xyz cba,ust,ihg,4,cdu,oqw file2: ravi,def,kishore ramu,ust,krishna joseph,stu,mike I need two output files as follows In my above example, each row in file1 has 6 fields and each row in file2 has 3... (1 Reply)
Discussion started by: yerruhari
1 Replies

10. Shell Programming and Scripting

I need to extract last column of a file and compare the values

Hi, I am new to unix and I need help in solving below mentioned issue, really appreciate ur help. I have a file sam, john, 2324, 07142007 tom, thomson, 2343, 07142007 john, scott, 2478, 07142007 its a comma delimited file, I need to extract the last column from each line and this... (4 Replies)
Discussion started by: vukkusila
4 Replies
Login or Register to Ask a Question