Check for the existence and add them from 2 different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check for the existence and add them from 2 different files
# 1  
Old 12-11-2012
Check for the existence and add them from 2 different files

Hi,

I have two files

file1:
Code:
ALEX
DANY
GARY
TOM
MARY
HARRIS

file2:
Code:
ALEX 3
ALEX 5
ALEX 0
ALEX 1
ALEX 0
DANY 2
DANY 3
DANY 0
DANY 0
DANY 1
GARY 20
GARY 25
GARY 3
GARY 5
GARY 0
TOM 8
TOM 7
TOM 0
TOM 5
TOM 2
TOM 1
MARY 0
MARY 0
MARY 8
MARY 12
MARY 2
MARY 1
HARRIS 5
HARRIS 6
HARRIS 12
HARRIS 18
HARRIS 13
HARRIS 20
LILY 12
LILY 13
LILY 5
LILY 8
LILY 16

What i am trying to do is for each entry in file 1 I need to count the values for the same entry in column 2 and add them up.

output:
Code:
ALEX 9
DANY 6
GARY 53
TOM 23
MARY 23
HARRIS 74

Thanks,
# 2  
Old 12-11-2012
try:
Code:
awk '
NR==FNR { a[$1]=$1; next }
a[$1] { c[$1]+=$2;}
END { for (i in c) print i, c[i];}
' file1 file2 | sort

# 3  
Old 12-11-2012
Code:
awk 'FNR==NR{c[$1]+=$2;next}{print $1,c[$1]+0}' file2 file1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to check the files existence inside a directory.

Hello Folks, On Below Script, I want to apply condition. Condition that it check the directory for files if not found keep checking. Once directory have files and founded do the calculation and finish the code. Script to check the files existence inside a directory, If not then keep... (16 Replies)
Discussion started by: sadique.manzar
16 Replies

2. Shell Programming and Scripting

Check the Files existence

Hi I have a requirement to check whether the files exists, then it will call other steps in shell script. I did ls *.csv|wc -l if then checking the count of the files should be more than 1 then it will call other steps. I am getting the error that too many arguements as there n... (13 Replies)
Discussion started by: cnrj
13 Replies

3. Shell Programming and Scripting

check existence of files in a folder

Hi I am having a problem to verify existence of files. I need to know whether or not files in a folder that begins with a name. For example all files that start with The_File_ *. I was doing it this way, but gives me error. if text -f /work/The_File_* then ... else .. fi (5 Replies)
Discussion started by: Rodrih92
5 Replies

4. UNIX for Dummies Questions & Answers

To check for existence of a file

I need to check for the existence of a file *.log in a specific directory using a perl script. Presently am not in that particular directory. So i am using chdir ("/path/to/my/file) And then i am using the -e in an if statement to check if it exists. if (-e $File) {......} $File contains the... (1 Reply)
Discussion started by: manutd
1 Replies

5. Shell Programming and Scripting

Check existence of a number of files and call other scripts

Hi, I am new to unix scripting and am jus getting to learn about it.. I need to know on how to check for the existence of a number of files in a path..i.e the files are ftp'ed from several other servers, should check if all the files have arrived, if not wait till they arrive..can i use a flag... (5 Replies)
Discussion started by: yohasini
5 Replies

6. AIX

Check for File Existence

I have requirement where i need to search for files which start with SALESORDER and PURCHASEORDER. i need to process the files with SALESORDER first and then PURCHASEORDER. If SALESORDER files are not there i dont want to process PURCHASEORDER and i want to come out of script. I have written a code... (4 Replies)
Discussion started by: dsdev_123
4 Replies

7. AIX

check for file existence

Hello I am having a requirement like if there is no file in the directory then i need a message to pop on after the execution of the script. My script basically does for File in `ls -t $DIRECTORY | tail -1`; if there is no file the DIRECTORY then the script is simply exiting with out... (2 Replies)
Discussion started by: dsdev_123
2 Replies

8. UNIX for Advanced & Expert Users

Check existence of a login

Hi everybody, I need to check in C program wether a given login is known on the system. Is there any system function that could do this ? So far, all I could find is getpwnam(), which answers my problem by parsing the local password database. But won't work if a user is authenticated by... (10 Replies)
Discussion started by: xavier054
10 Replies

9. UNIX for Dummies Questions & Answers

Variable check for existence ?

Hi , I have a script wherein i have a For Loop. Within this for loop i create a variable and assign it a value. The script goes to a For Loop only if certain conditions are met , which means the variable may or may not exists. However down the line in the script i have to check if that... (2 Replies)
Discussion started by: samit_9999
2 Replies

10. Shell Programming and Scripting

check for FILES existence

hi, I have a list of filenames and I want to verify if they all exist. I know "if filename" would do the trick but how do I go about a list of files? thanks (3 Replies)
Discussion started by: mpang_
3 Replies
Login or Register to Ask a Question