![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Adding columns to excel files using Perl | dolo21taf | Shell Programming and Scripting | 1 | 02-20-2008 04:13 AM |
| Adding columns of two files | chandra321 | Shell Programming and Scripting | 6 | 04-06-2007 06:36 AM |
| Adding files of numerical data | Boucho | High Level Programming | 17 | 02-07-2006 01:29 PM |
| Question on files - adding commas at the end | jingi1234 | UNIX for Dummies Questions & Answers | 5 | 09-27-2005 01:06 PM |
| Adding 3 Lines to Multiple c and h files with perl | Lazzar | Shell Programming and Scripting | 2 | 10-28-2003 10:30 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
adding two files
Dear Experts,
I am facing some problem. I have two files, every field is separated by comma "," separator. And the value is in numeric FILEA 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 FILE B 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 So the output to be shown like this COLUMN 1+COLUMN1,COULMUN2+COLUMN 2,…….ETC. I want to add two files and the output should be save in to the third file FIANAL OUTPUT. FINAL OUTPUT 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34 Regards, SHARY [/b] |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Code:
awk -F"," 'BEGIN{ while( getline < "file_one" ) { split($0, th, ","); for(j=1; j<=NF; j++) { arr[i++]=$j } } }{ for(j=1; j<=NF; j++ ) { printf "%d ", arr[x++]+$j } printf "\n"; }' file_two
|
|
#3
|
||||
|
||||
|
Another approach:
Code:
awk 'NR == FNR {
for (i=1; i<=NF; i++)
f1[FNR SUBSEP i] = $i
next
}
{
for (j=1; j<=NF; j++)
$j = $j + f1[FNR SUBSEP j]
}1' FS="," OFS="," fileA fileB
Last edited by radoulov; 11-24-2007 at 11:35 AM. Reason: adjusted |
|
#4
|
|||
|
|||
|
awk 'NR == FNR {
for (i=1; i<=NF; i++) f1[FNR SUBSEP i] = $i next } { for (j=1; j<=NF; j++) $j = $j + f1[FNR SUBSEP j] }1' FS="," OFS="," fileA fileB Thanks alot it really works very fine.but i have never used SUBSEP and next. if you dont mind can you please explain me exactly what its doing and how the scripts work. i searched alot but unable to understan it. Regards, SHARY |
|
#5
|
||||
|
||||
|
Hi,
1. SUBSEP: From Effective AWK Programming: Quote:
Quote:
2. The next statement: From Effective AWK Programming: Quote:
Code:
[...] for (j=1; j<=NF; j++) $j = $j + f1[FNR SUBSEP j] [...] The scope is to read the first file and populate an array (f1) whose indices are the FNR (current record number in the current file) and the number of the fields like: Code:
record 1 field 1 == 1 record 1 field 2 == 2 ... record n field n == n with those matching the elements of the f1 array: Code:
[...] for (j=1; j<=NF; j++) $j = $j + f1[FNR SUBSEP j] [...] so after that we're free to set OFS. Last edited by radoulov; 11-25-2007 at 08:19 AM. |
|
#6
|
||||
|
||||
|
Hi.
See Gawk: Effective AWK Programming - GNU Project - Free Software Foundation (FSF) for many formats of the book on-line ... cheers, drl |
|
#7
|
|||
|
|||
|
awk
HI,
code: Code:
nawk -v l=3 'BEGIN{
FS=","
}
{
if (NR<=l)
{
for (i=1;i<=NF;i++)
sum[NR,i]=$i
}
else
{
for (i=1;i<=NF;i++)
{
sum[NR-l,i]=sum[NR-l,i]+$i
printf("%s,",sum[NR-l,i])
}
print ""
}
}' file1 file2
|
|||
| Google The UNIX and Linux Forums |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|