PDA

View Full Version : using expr command to add value from two file



aliahsan81
April 6th, 2009, 08:06 PM
How to add two number from two different file using expr command ??

Arndt
April 6th, 2009, 08:15 PM
How to add two number from two different file using expr command ??

Can you give some more information? Are the numbers in the filenames, or stored inside the files? Give an example.

aliahsan81
April 6th, 2009, 08:20 PM
Like i have file A and B

Both contain Numaric like

File A

34
45
56
78

File B

34
67
89

I want to add first number number from both file
34+45=Result in some other file
45+67=Result in some other file

and soo on

aliahsan81
April 6th, 2009, 08:21 PM
Both file have same number of numeric characters

Arndt
April 6th, 2009, 09:14 PM
Both file have same number of numeric characters

Here is a Perl one-liner:


$ cat n1
12
34
32
11
$ cat n2
56
677
99
88
$ perl -e 'open(F1,$ARGV[0]); open(F2,$ARGV[1]); while (<F1>) {print $_+<F2>,"\n"}' n1 n2
68
711
131
99


If you want to use 'expr', you will need a shell script to read the numbers from the files. Did you try writing such a script?

aliahsan81
April 6th, 2009, 09:31 PM
Its a small part of script.Actually i am processing access log of apache.I want to process access log from where it last time left.For this i am doing this

tail --bytes filename

which will give length of current access log

then when next time script run it will again do


tail --bytes filename

after then i have two values i can subtract one from and other then i will go the bytes value

then i do

tail -c number filename | grep mything

this ensure i will only pars that log part which is new from previous and thus enhancing performance.

Please can you help me in this i mean adding two value from file.using awk or any other mean.
the above i am using becaue i have many access-log for many domain.so keep trakc of each acclog log size.

ghostdog74
April 7th, 2009, 01:38 AM
Like i have file A and B

Both contain Numaric like

File A

34
45
56
78

File B

34
67
89

I want to add first number number from both file
34+45=Result in some other file
45+67=Result in some other file

and soo on



paste file1 file2| sed 's/[[:blank:]]/+/g'| bc


awk:


awk 'FNR==NR{_[++d]=$1;next}{_[FNR]+=$1}END{for(i in _) print _[i]}' file1 file2

aliahsan81
April 7th, 2009, 07:20 PM
Thanks you all.This is a great forum.I will use the awk code in my script.