Compare rows using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare rows using awk
# 1  
Old 09-24-2018
Compare rows using awk

Hi,

i want to compare all the rows of columns A and if it same then need to add its corresponding value column B and print it as a new column.

Code:
eg.
/vol/_1ACIFS02,482259,
/vol/_1BCIFS01,122451,
/vol/_2CCIFS02,2310,
/vol/_2CCIFS04,11508,
/vol/_2CCIFS04,8461,
/vol/_525e0e05_56e6_4acb_b655_eb9b2324d839_t1000_snap_1,20529,
/vol/_7201_SPBSVM1_A1_SSD_15_HI,425,
/vol/_7201_SPBSVM1_A1_SSD_15_HI,89265,
/vol/_7201_SPBSVM1_A1_SSD_15_HI,50770,
/vol/_7201_SPBSVM1_A1_SSD_21,0,
/vol/_7201_SPBSVM1_A1_SSD_22,0,
/vol/_7201_SPBSVM1_A2_SSD_16_HI,44061,
/vol/_7201_SPBSVM1_A2_SSD_16_HI,75787,
/vol/_7201_SPBSVM1_A2_SSD_17_V,1013,
/vol/_7201_SPBSVM1_A2_SSD_19_CDEFP,145740,
/vol/_7201_SPBSVM1_A2_SSD_19_CDEFP,773594,

output:
/vol/_1ACIFS02,482259,482259
/vol/_1BCIFS01,122451,122451
/vol/_2CCIFS02,2310,2310
/vol/_2CCIFS04,11508,19969
/vol/_2CCIFS04,8461,19969
/vol/_525e0e05_56e6_4acb_b655_eb9b2324d839_t1000_snap_1,20529,20529
/vol/_7201_SPBSVM1_A1_SSD_15_HI,425,140460
/vol/_7201_SPBSVM1_A1_SSD_15_HI,89265,140460
/vol/_7201_SPBSVM1_A1_SSD_15_HI,50770,140460
/vol/_7201_SPBSVM1_A1_SSD_21,0,0
/vol/_7201_SPBSVM1_A1_SSD_22,0,0
/vol/_7201_SPBSVM1_A2_SSD_16_HI,44061,119848
/vol/_7201_SPBSVM1_A2_SSD_16_HI,75787,119848
/vol/_7201_SPBSVM1_A2_SSD_17_V,1013,1013
/vol/_7201_SPBSVM1_A2_SSD_19_CDEFP,145740,919334
 /vol/_7201_SPBSVM1_A2_SSD_19_CDEFP,773594,919334


Moderator's Comments:
Mod Comment New Users: Please Read forum Rules and Use Code Tags when Posting Code
# 2  
Old 09-24-2018
Your logical description does not seem to fit. For example:
Code:
/vol/_7201_SPBSVM1_A1_SSD_15_HI,425,140460
/vol/_7201_SPBSVM1_A1_SSD_15_HI,89265,140460
/vol/_7201_SPBSVM1_A1_SSD_15_HI,50770,140460

140460 does not appear in the input. But per your logic those rows should have 425, 89265, and 50770 at the end.
# 3  
Old 09-24-2018
Basically it is a summation of all the values if column A is same.

eg.)
Code:
/vol/_7201_SPBSVM1_A1_SSD_15_HI,425,
/vol/_7201_SPBSVM1_A1_SSD_15_HI,89265,
/vol/_7201_SPBSVM1_A1_SSD_15_HI,50770,

425+89265+50770 = 140460 which needs to be printed in all the rows which are considered.

Last edited by RudiC; 09-24-2018 at 04:53 PM..
# 4  
Old 09-24-2018
Moderator's Comments:
Mod Comment karan123: Please Read forum Rules and Use Code Tags when Posting Code


Users from Bangalore need to use code tags and follow forum rules:


Code:
route:          202.3.121.0/24
descr:          NETAPP BANGALORE IN
origin:         AS24477
mnt-by:         MAINT-IN-NETAPP
last-modified:  2016-09-15T08:15:56Z
source:         APNIC

# 5  
Old 09-24-2018
Is the input file sorted? Does output order matter? Any attempts / ideas / thoughts from your side?


And - did you consider searching these fora as this problem has been solved umpteen times in here.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 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 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

3. Shell Programming and Scripting

[Solved] Compare column data in all the rows

Hi.. In the below sorted input file.. I am comparing the first 3 columns of data one by one row and it is a pipeline delimitter file.. AA|BB|CC|line1 AA|BB|CC|ine4 AA|BB|CC|line2 BB|CC|DD|line3 BB|CC|DD|line5 If first 3 columns of data matches with any record in the file the... (4 Replies)
Discussion started by: NareshN
4 Replies

4. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 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

AWK help. how to compare a variable with a data array in AWK?

Hi all, i have a data array as follows. array=ertfgj2345 array=456ttygkd . . . array=errdjt3235 so number or elements in the array can varies depending on how big the data input is. now i have a variable, and it is $1 (there are $2, $3 and so on, i am only interested in $1). ... (9 Replies)
Discussion started by: usustarr
9 Replies

7. Shell Programming and Scripting

Compare columns and rows with template, and fill empty slots.

Hi, I'm working on a script that will take the contents of a file, that is in a row and column format, and compare it to a arrangment file. Such that if there is any or all blanks in my content file, the blank will be filled with a flag and will retain the row and column configuration. Ex. ... (2 Replies)
Discussion started by: hizzle
2 Replies

8. Shell Programming and Scripting

Compare 200,000 of rows in two text files

Friends, I have two very large plain text files with pipe delimited as below. Both files are not sorted. Both files have 200,000 of rows. FName|LName|Address|HPhNumber Is perl or shell script feasible for this task? Thanks, Prashant (1 Reply)
Discussion started by: ppat7046
1 Replies

9. Shell Programming and Scripting

How to compare contents of two CSV rows

I have been trying to find the answer to this but can't seem to get it. I have two rows, both are CSV files. I want to iteratively compare each field value to the corresponding value in the second file. Example: File 1.txt 1|hello|3.2|AMB||||B File 2.txt 1|hi|3,2|ABC||||C The... (1 Reply)
Discussion started by: axwack
1 Replies
Login or Register to Ask a Question