Merge 2 files and sum in ksh


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Merge 2 files and sum in ksh
# 1  
Old 12-22-2011
Merge 2 files and sum in ksh

file 1
Code:
SGNAME:dgfdhf
LUN 2
LUN 1
LUN 9
LUN 30
LUN 32
SGNAME:sadgddhdfab_cluster
LUN 13
LUN 11
LUN 15
SGNAME:asgdg (sgdg)
LUN 110
LUN 111
LUN 112
LUN 113
LUN 114
LUN 115
LUN 116

file 2
Code:
LUN 2 34 60:06:01:60:20:84:24:00:03:CA:53:2E:85:97
LUN 1 76 60:06:01:60:20:84:24:00:03:CA:53:2E:85:97
LUN 9 76 60:06:01:60:20:84:24:00:03:CA:53:2E:85:97
LUN 30 76 60:06:01:60:20:84:24:00:03:CA:53:2E:85:97
LUN 32 34 60:06:01:60:20:84:24:00:03:CA:53:2E:85:97
LUN 13 76 
LUN 11 76
LUN 15 76
LUN 110 34
LUN 111 76
LUN 112 76
LUN 113 34
LUN 114 76
LUN 115 34
LUN 116 34

Output
Code:
SGNAME:dgfdhf 
LUN 2 34 60:06:01:60:20:84:24:00:03:CA:53:2E:85:97
LUN 1 76 60:06:01:60:20:84:24:00:03:CA:53:2E:85:97
LUN 9 76 60:06:01:60:20:84:24:00:03:CA:53:2E:85:97
LUN 30 76 60:06:01:60:20:84:24:00:03:CA:53:2E:85:97
LUN 32 34 60:06:01:60:20:84:24:00:03:CA:53:2E:85:97
Total Capacity: 296 (sum of column 3 - 34+76+76+..)
SGNAME:sadgddhdfab_cluster
LUN 13 76 60:06:01:60:20:84:24:00:03:CA:53:2E:85:
LUN 11 76 60:06:01:60:20:84:24:00:03:CA:53:2E:85:97
LUN 15 76 60:06:01:60:20:84:24:00:03:CA:53:2E:85:97
Total Capacity: 228
SGNAME:asgdg (sgdg)
...
...

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by radoulov; 12-22-2011 at 08:06 AM..
# 2  
Old 12-22-2011
Moderator's Comments:
Mod Comment You should show your own work when asking for help.
# 3  
Old 12-22-2011
Code:
nawk '{i=$1" "$2}NR==FNR{A[i]=$3;B[i]=$4;next}{y=x;x=$1}x~/:/{if(y) {print z;s=0};print $0;next} {s+=A[i];z="Total Capacity= "s;print i,A[i],B[i]}END{print z}' file2 file1

# 4  
Old 12-22-2011
I used awk to merge but , SGNAME if file 1 is not displayed ;total i want to do later...

awk 'FNR == NR { arr[$0]=$2; next }
{ $(NF+1)=($1 in arr) ? arr[$1] : " "; print }' file1 file2
i get output like...

LUN 1997 LUN UID 60:06:01:60:20:84:24:00:BE:00:F9:CD:84:97SmilieE:11 LUN Cap 76800 MB
LUN 2015 LUN UID 60:06:01:60:20:84:24:00:9C:85:B8:5D:FD:B2SmilieE:11 LUN Cap 76800 MB
LUN 1946 LUN UID 60:06:01:60:35:84:24:00:FD:56:6D:1F:85:97SmilieE:11 LUN Cap 76800 MB
LUN 2002 LUN UID 60:06:01:60:35:84:24:00:CD:2C:F1:BE:84:97SmilieE:11 LUN Cap 76800 MB

I need SGNAME and the respective LUNS under SGNAME ;again SGNAME LUNs kind of pattern... tried using Join again the same missing SGNAME....

nawk is not working ...
# 5  
Old 12-23-2011
Did you try :

Code:
awk '{i=$1" "$2}NR==FNR{A[i]=$3;B[i]=$4;next}{y=x;x=$1}x~/:/{if(y) {print z;s=0};print $0;next} {s+=A[i];z="Total Capacity= "s;print i,A[i],B[i]}END{print z}' file2 file1

?
This User Gave Thanks to ctsgnb For This Post:
# 6  
Old 12-27-2011
This is the output I am getting all capacity 0. I dint understand if the awk is rading the values to A[i] and B[i] here...please help me undertsandging what exactly is being done here...
SGNAME:dgfdhf
LUN 2
LUN 1
LUN 9
LUN 30
LUN 32
Total Capacity= 0
SGNAME:sadgddhdfab_cluster
LUN 13
LUN 11
LUN 15
Total Capacity= 0
SGNAME:asgdg (sgdg)
LUN 110
LUN 111
LUN 112
LUN 113
LUN 114
LUN 115
LUN 116
Total Capacity= 0
Thanks
# 7  
Old 12-28-2011
I tested the code on an ubuntu machine as well as on a FreeBSD one and it works fine... (see below)

1) what OS do you run ?
2) Please check that your files are formatted the same way (columns separated by single space) than those you provided in your example

Code:
$ cat f1
SGNAME:dgfdhf
LUN 2
LUN 1
LUN 9
LUN 30
LUN 32
SGNAME:sadgddhdfab_cluster
LUN 13
LUN 11
LUN 15
SGNAME:asgdg (sgdg)
LUN 110
LUN 111
LUN 112
LUN 113
LUN 114
LUN 115
LUN 116

Code:
$ cat f2
LUN 2 34 60:06:01:60:20:84:24:00:03:CA:53:2E:85:97
LUN 1 76 60:06:01:60:20:84:24:00:03:CA:53:2E:85:97
LUN 9 76 60:06:01:60:20:84:24:00:03:CA:53:2E:85:97
LUN 30 76 60:06:01:60:20:84:24:00:03:CA:53:2E:85:97
LUN 32 34 60:06:01:60:20:84:24:00:03:CA:53:2E:85:97
LUN 13 76
LUN 11 76
LUN 15 76
LUN 110 34
LUN 111 76
LUN 112 76
LUN 113 34
LUN 114 76
LUN 115 34
LUN 116 34

Code:
$ awk '{i=$1" "$2}NR==FNR{A[i]=$3;B[i]=$4;next}{y=x;x=$1}x~/:/{if(y) {print z;s=0};print $0;next} {s+=A[i];z="Total Capacity= "s;print i,A[i],B[i]}END{print z}' f2 f1
SGNAME:dgfdhf
LUN 2 34 60:06:01:60:20:84:24:00:03:CA:53:2E:85:97
LUN 1 76 60:06:01:60:20:84:24:00:03:CA:53:2E:85:97
LUN 9 76 60:06:01:60:20:84:24:00:03:CA:53:2E:85:97
LUN 30 76 60:06:01:60:20:84:24:00:03:CA:53:2E:85:97
LUN 32 34 60:06:01:60:20:84:24:00:03:CA:53:2E:85:97
Total Capacity= 296
SGNAME:sadgddhdfab_cluster
LUN 13 76
LUN 11 76
LUN 15 76
Total Capacity= 228
SGNAME:asgdg (sgdg)
LUN 110 34
LUN 111 76
LUN 112 76
LUN 113 34
LUN 114 76
LUN 115 34
LUN 116 34
Total Capacity= 364
$

This User Gave Thanks to ctsgnb For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get CRC check sum of files in java EAR file without extracting .jar/.war files to disk.?

unzip -v gives CRC info of each file in a zip(in my case .EAR) file. # unzip -v my-application.ear Archive: my-application.ear Length Method Size Cmpr Date Time CRC-32 Name -------- ------ ------- ---- ---------- ----- -------- ---- 197981 Defl:N 183708 7%... (1 Reply)
Discussion started by: kchinnam
1 Replies

2. Shell Programming and Scripting

Merge files and generate a resume in two files

Dear Gents, Please I need your help... I need small script :) to do the following. I have a thousand of files in a folder produced daily. I need first to merge all files called. txt (0009.txt, 0010.txt, 0011.txt) and and to output a resume of all information on 2 separate files in csv... (14 Replies)
Discussion started by: jiam912
14 Replies

3. Shell Programming and Scripting

Checking in a directory how many files are present and basing on that merge all the files

Hi, My requirement is,there is a directory location like: :camp/current/ In this location there can be different flat files that are generated in a single day with same header and the data will be different, differentiated by timestamp, so i need to verify how many files are generated... (10 Replies)
Discussion started by: srikanth_sagi
10 Replies

4. Shell Programming and Scripting

Sum Numbers from different files

Hi All, I need to print the sum of numbers from different files. Input files: file1.out 10 20 30 file2.out 10 20 30 (5 Replies)
Discussion started by: saint2006
5 Replies

5. Shell Programming and Scripting

sum the columns of files

I have several csv files like this: file1.csv 1 12 1 2 8 9 3 9 2 4 5 9 ... file2.csv 1 0 1 2 2 3 3 4 1 ... file3.csv 1 0 1 2 4 0 ... I want the result like this 1 12 3 2 14 12 3 13 3 (10 Replies)
Discussion started by: frewise
10 Replies

6. Shell Programming and Scripting

How to sum 2 files?

i have arq1.txt , like this: 1 2 3 4 5 and another arq2.txt 1 2 3 4 5 how to make this: 2 4 (8 Replies)
Discussion started by: beandj
8 Replies

7. Shell Programming and Scripting

Merge lines if pattern matches in ksh

I have a file like this. Pls help me to solve this . (I should look for only Message : 111 and need to print the start time to end time Need to ignore other type of messages. Ex: if first message is 111 and second message is 000 or anything else then ignore the 2nd one and print start time of the... (1 Reply)
Discussion started by: mnjx
1 Replies

8. Shell Programming and Scripting

SUM of Backup Files

Hello Everyone, I'm struggiling with backup issues and need to sum up sizes of backup files monthly and add the result to the next month's sum recursively. For this i have a well working script that i modified as i showed below and this part gives the sum of the file sizes under working... (2 Replies)
Discussion started by: EAGL€
2 Replies

9. Shell Programming and Scripting

Merge files of differrent size with one field common in both files using awk

hi, i am facing a problem in merging two files using awk, the problem is as stated below, file1: A|B|C|D|E|F|G|H|I|1 M|N|O|P|Q|R|S|T|U|2 AA|BB|CC|DD|EE|FF|GG|HH|II|1 .... .... .... file2 : 1|Mn|op|qr (2 Replies)
Discussion started by: shashi1982
2 Replies

10. Shell Programming and Scripting

how to sum values from 2 different files?

Hi I am trying to add count values from two different files into one file. Could any body please suggest me best command to do this? My problem was as follows: a.txt b.txt c.txt 10 20 30(needed) i tried cat a.txt b.txt > c.txt (its not adding the values) Thanks in advance.. Praveen (8 Replies)
Discussion started by: npk2210
8 Replies
Login or Register to Ask a Question