Compare values of fields from same column with awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Compare values of fields from same column with awk
# 8  
Old 06-19-2012
Try

Code:
awk '
BEGIN{OFS=FS="|"} 
{
 if(NF>max) max=NF
 for(i=1;i<=NF;i++)
 {
  start=match(a[i],$i)
  if(start && substr(a[i],(start+RLENGTH),1) ~ /^[,]*$/)
   continue
  if(a[i])
   a[i]=a[i]","$i
  else
   a[i]=$i
 }
} 
END{for(i=1;i<=max;i++) $i=a[i];print}' inputfile

This User Gave Thanks to elixir_sinari For This Post:
# 9  
Old 06-19-2012
If using shell script is an option

Code:
#!/bin/ksh
cnt=$(awk -F'|' '{print NF;exit}' 1)
for i in {1..$(expr ${cnt} - 1)}
do
  field=$(cut -d'|' -f${i} 1 | sort -u | tr '\n' ',' | sed 's/,$//g')
  printf ${field}'|'
done
field=$(cut -d'|' -f${cnt} 1 | sort -u | tr '\n' ',' | sed 's/,$//g')
printf ${field}"\n"

# 10  
Old 06-20-2012
Thanks elixir !

However, i don't understand the point of
Code:
if(NF>max) max=NF

Why not just use
Code:
i<=NF

directly in the END section ?

AND

Code:
start=match(a[i],$i)
  if(start && substr(a[i],(start+RLENGTH),1) ~ /^[,]*$/)

It would not have been simpler to use something like
Code:
length(a)

?
# 11  
Old 06-20-2012
Quote:
Originally Posted by lucasvs
However, i don't understand the point of
Code:
if(NF>max) max=NF

Why not just use
Code:
i<=NF

directly in the END section ?
Just wanted to make the solution general. So, assumed that the number of columns in the lines might vary. Hence, the construct. If the number of columns in each line is fixed, NF will retain the number of fields in the last line read and you could use it.

Quote:
Originally Posted by lucasvs
Code:
start=match(a[i],$i)
  if(start && substr(a[i],(start+RLENGTH),1) ~ /^[,]*$/)

It would not have been simpler to use something like
Code:
length(a)

?
Again some generalization...to handle input such as:
Code:
reddish|green
blue|green   
red|green

# 12  
Old 06-20-2012
Ok I get it !

So, if I understood well, if I am sure that my input will contain only one character per field, with the same number of columns for every line, I can use this code instead:

Code:
BEGIN{OFS=FS="|"} 
{
    
         for(i=1;i<=NF;i++){
             start=match(a[i],$i)
                  if(start && substr(a[i],(start+RLENGTH),1) ~ /^[,]*$/)
                       continue
                  if(a[i])
                       a[i] = a[i]","$i
                  else
                       a[i] = $i
         }
}
 
END{for(i=1;i<=NF;i++) $i=a[i];print}

And how I could do to change the format of the output and put the fields that contain a "," between braces like:

Code:
1|(2,3)|4

instead of

Code:
1|2,3|4


Last edited by lucasvs; 06-21-2012 at 12:01 AM..
 
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. 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. 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

6. Shell Programming and Scripting

Compare two files based on values of fields.

Hi All, I have two files and data looks like this: File1 Contents #Field1,Field2 Dist_Center_file1.txt;21 Dist_Center_file3.txt;20 Dist_Center_file2.txt;20 File2 Contents (*** No Header ***) Dist_Center_file1.txt;23 Dist_Center_file2.txt;20 Dist_Center_file3.txt;20 I have... (4 Replies)
Discussion started by: Hangman2
4 Replies

7. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: yerruhari
3 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

How to read and compare multiple fields in a column at the same time

Hi, Currently I am coding up a nasty way of reading file input using *cat* rather than *read*. My text input looks like TextA 100 TextB 110 TextC 120 Currently I am using cat |while read line to read the first column and second column fields. cat foo.txt|while read line do ... (1 Reply)
Discussion started by: ahjiefreak
1 Replies
Login or Register to Ask a Question