Split file and do the math


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split file and do the math
# 1  
Old 11-23-2011
Split file and do the math

Hi

I have a file with 2 columns with n rows


Code:
11873,12594,13402,      12227,12721,14409,

14362,14969,15795,16606,16857,17232,17914,18267,18500,18912,      14829,15038,15947,16765,17055,17742,18061,18369,18554,19759,
14406,16857,17232,17914,18267,24737,29320,	   16765,17055,17742,18061,18366,24891,29370,
34611,35276,35720,	35174,35481,36081,
69090,	70008,

What i want is for each value in column 1 subtract with value in column 2

o/p
Code:
11873-12227,12594-12594,13402-14409

so on..

Thanks,
# 2  
Old 11-23-2011
Try this:

Code:
awk '
    {
        gsub( ",$", "", $1 );
        gsub( ",$", "", $2 );
        na = split( $1, a, "," )
        split( $2, b, "," );
        for( i = 1; i <= na; i++ )
            printf( "%d%c", a[i] - b[i], i == na ? "" : "," );
        printf( "\n" );
    }
' input-file

# 3  
Old 11-23-2011
Thanks,

it worked..

Also can I get the numbers between each comma for values within column 1 and 2

Code:
11873,12594,13402,      12227,12721,14409,

o/p

Code:
12594-11873,13402-12594    12721-12227,14409-12721

Thanks,

Diya
# 4  
Old 11-23-2011
This prints a-b=c
Code:
awk '
    {
        gsub( ",$", "", $1 );
        gsub( ",$", "", $2 );
        na = split( $1, a, "," )
        split( $2, b, "," );
        for( i = 1; i <= na; i++ )
            printf( "%d-%d=%d%c", a[i], b[i], a[i] - b[i], i == na ? "" : "," );
        printf( "\n" );
    }
' input-file

This prints just a-b
Code:
awk '
    {
        gsub( ",$", "", $1 );
        gsub( ",$", "", $2 );
        na = split( $1, a, "," )
        split( $2, b, "," );
        for( i = 1; i <= na; i++ )
            printf( "%d-%d%c", a[i], b[i], i == na ? "" : "," );
        printf( "\n" );
    }
' input-file

# 5  
Old 11-23-2011
Thanks,

I am sorry if I was not clear.. But what i want in the 2nd question is subtract the values within each column instead of between columns

example in column one there are three values

11873,12594,13402,

what I want is split by comma and subtract (2)-(1), (3)-(2)

In other words 12594-11873, 13402-12594

This has to be done individually for both columns 1 and 2

Thanks,
# 6  
Old 11-23-2011
Yep, I completely misunderstood...

Code:
awk '
    {
        gsub( ",$", "", $1 );
        gsub( ",$", "", $2 );
        for( j = 1; j <= NF; j++ )
        {
            na = split( $1, a, "," )
            for( i = 2; i <= na; i++ )
                printf( "%d%c",  a[i] - a[i-1], i == na ? "" : "," );
            printf( "   " );
        }

        printf( "\n" );

    }
' input-file


Last edited by agama; 11-23-2011 at 07:51 PM.. Reason: typo.
# 7  
Old 11-23-2011
Thanks,

There is a bug in this..b is not defined..its giving a wrong o/p
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Copy last few lines of a file, perform math operation and iterate further

Hi, I am trying to generate a data of following order: 4 0 1 642 643 4 642 643 1283 1284 4 1283 1284 1924 1925 4 1924 1925 2565 2566 4 2565 2566 3206 3207 4 3206 3207 3847 3848 4 3847 3848 4488 4489 4 4488 4489 5129 5130 ---------------------- 4 1 2 643 644 4 643 644 1284... (6 Replies)
Discussion started by: SaPa
6 Replies

2. UNIX for Beginners Questions & Answers

sed awk: split a large file to unique file names

Dear Users, Appreciate your help if you could help me with splitting a large file > 1 million lines with sed or awk. below is the text in the file input file.txt scaffold1 928 929 C/T + scaffold1 942 943 G/C + scaffold1 959 960 C/T +... (6 Replies)
Discussion started by: kapr0001
6 Replies

3. Shell Programming and Scripting

awk --> math-operation in data-record and joining with second file data

Hi! I have a pretty complex job - at least for me! i have two csv-files with meassurement-data: fileA ...... (2 Replies)
Discussion started by: IMPe
2 Replies

4. Shell Programming and Scripting

How to split file into multiple files using awk based on 1 field in the file?

Good day all I need some helps, say that I have data like below, each field separated by a tab DATE NAME ADDRESS 15/7/2012 LX a.b.c 15/7/2012 LX1 a.b.c 16/7/2012 AB a.b.c 16/7/2012 AB2 a.b.c 15/7/2012 LX2 a.b.c... (2 Replies)
Discussion started by: alexyyw
2 Replies

5. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 Replies

6. Shell Programming and Scripting

Split File by Pattern with File Names in Source File... Awk?

Hi all, I'm pretty new to Shell scripting and I need some help to split a source text file into multiple files. The source has a row with pattern where the file needs to be split, and the pattern row also contains the file name of the destination for that specific piece. Here is an example: ... (2 Replies)
Discussion started by: cul8er
2 Replies

7. Shell Programming and Scripting

math calculation for a txt file

Hi All, I have a text file which is only one column. I want to multiply all these values in file1.txt by 0.01 and get the output.txt file1.txt 65 85 90 ... output.txt 0.65 0.85 0.90 ... Thanks. Please use code tags when posting data and code samples! (2 Replies)
Discussion started by: senayasma
2 Replies

8. Shell Programming and Scripting

How to split a data file into separate files with the file names depending upon a column's value?

Hi, I have a data file xyz.dat similar to the one given below, 2345|98|809||x|969|0 2345|98|809||y|0|537 2345|97|809||x|544|0 2345|97|809||y|0|651 9685|98|809||x|321|0 9685|98|809||y|0|357 9685|98|709||x|687|0 9685|98|709||y|0|234 2315|98|809||x|564|0 2315|98|809||y|0|537... (2 Replies)
Discussion started by: nithins007
2 Replies

9. Shell Programming and Scripting

Math operations with file columns values.

Hello everybody, I have some large files containing statistical data. The data is stored in the following generic format: >cat my_file 1, 2, 3 1, 2, 3 1, 2, 3 > The values of columns no.2 and 3 are expressed in bytes. I would like to transform them in Megabytes, by dividing them with... (3 Replies)
Discussion started by: fabian23
3 Replies

10. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies
Login or Register to Ask a Question