plus values from two files in differient rows


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting plus values from two files in differient rows
# 1  
Old 06-17-2009
Lightbulb plus values from two files in differient rows

Hi Guys Smilie,

I have two files.

filea:
12:20:23 4
12:21:23 3
12:22:23 2
12:25:23 3

fileb:
11:20:23 4
12:21:23 3
12:22:23 2
12:26:23 3
12:30:23 3

The result will be:
11:20:23 4
12:20:23 4
12:21:23 6
12:22:23 4
12:25:23 3

it means:
1. if the 1st column same in two files, then plus the value in the 2nd value.
2. the date in fileb if greater than the last line in filea, then ignore.

Thanks
# 2  
Old 06-17-2009
use awk
Code:
awk '{A[$1]+=$2}END{for (i in A){print i" "A[i]}}' filea fileb

# 3  
Old 06-17-2009
Quote:
Originally Posted by vidyadhar85
use awk
Code:
awk '{A[$1]+=$2}END{for (i in A){print i" "A[i]}}' filea fileb

Smilie just one line awk? it works, i do not understand how to read this awk, but it works so nice. Smilie

-----Post Update-----

How about i have a file a.txt.

Thu Jun 11 23:59:58 2009 115
Thu Jun 11 23:59:59 2009 115
Fri Jun 12 00:00:00 2009 115
Fri Jun 12 00:00:01 2009 115

i would like to grep those day jun 11 lines to another file.
i can think about using:
cat a.txt | cut -f3 -d' ' | grep '11' | wc -l

But i feel this is not efficient Smilie, as my a.txt contains not only 4 lines. Smilieany way can do more efficient by using perl, or others?

Thanks

-----Post Update-----

Quote:
Originally Posted by vidyadhar85
use awk
Code:
awk '{A[$1]+=$2}END{for (i in A){print i" "A[i]}}' filea fileb


Maybe below for multiple columns:
awk '{A[$1" "$2" "$3" "$4" "$5"\t"]+=$6}END{for (i in A){print i" "A[i]}}' a.txt b.txt | sort
# 4  
Old 06-18-2009
Code:
nawk 'NR==FNR{
        _[$1]=$2
        gsub(/:/,"",$1)
        max=$1
        next
}
{
        tmp=$1
        gsub(/:/,"",tmp)
        if ( tmp <= max )
                _[$1]+=$2
}
END{
        for (i in _)
                print i" "_[i]
}' f1 f2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Pivoting values from column to rows

I/P: I/P: 2017/01/01 a 10 2017/01/01 b 20 2017/01/01 c 40 2017/02/01 a 10 2017/02/01 b 20 2017/02/01 c 30 O/P: a b c 2017/01/01 10 20 40 2017/02/01 10 20 30 (18 Replies)
Discussion started by: Booo
18 Replies

2. Shell Programming and Scripting

Extract rows with different values at 2 columns

Hallo, I would need to extract only rows which has different value in the second and third column. Thank you very much for any advices Input: A 0 0 B 0 1 C 1 1 D 1 3 Output B 0 1 D 1 3 (4 Replies)
Discussion started by: kamcamonty
4 Replies

3. Shell Programming and Scripting

How to convert values in a line to rows?

hi, I am basically running a sql that returns me values and I have stored them to a variable for example value of the variable will be: 123 124 345 now I want to write values stored in the variable into a file as 123 124 345 thanks in advance (3 Replies)
Discussion started by: babom
3 Replies

4. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

5. Shell Programming and Scripting

Duplicate rows in CSV files based on values

I am new to this forum and this is my first post. I am looking at an old post with exactly the same name. Can not paste URL because I do not have 5 posts My requirement is exactly opposite. I want to get rid of duplicate rows and try to append the values of columns in those rows ... (10 Replies)
Discussion started by: vbhonde11
10 Replies

6. Shell Programming and Scripting

printing 3 files side by side based on similar values in rows

Hi I'm trying to compare 3 or more files based on similar values and outputting them into 3 columns. For example: file1 ABC DEF GHI file2 DEF DER file3 ABC DER The output should come out like this file1 file2 file3 ABC ABC (4 Replies)
Discussion started by: zerofire123
4 Replies

7. Shell Programming and Scripting

Deleting specific rows in large files having rows greater than 100000

Hi Guys, I need help in modifying a large text file containing more than 1-2 lakh rows of data using unix commands. I am quite new to the unix language the text file contains data in a pipe delimited format sdfsdfs sdfsdfsd START_ROW sdfsd|sdfsdfsd|sdfsdfasdf|sdfsadf|sdfasdf... (9 Replies)
Discussion started by: manish2009
9 Replies

8. Shell Programming and Scripting

Determine differient IP in a very long record.

Hi Everyone, I have a txt file with 40k lines. 111 1.1.1.1 111 1.1.1.1 111 2.2.2.2 111 1.1.1.1 111 1.1.1.1 How would use perl to easy have IP list, the final result will be: 1.1.1.1,2.2.2.2, I can only think using if to determine one by one through all 40k, this is working, but... (10 Replies)
Discussion started by: jimmy_y
10 Replies

9. Shell Programming and Scripting

plus values from two files differient fields.

Hi Everyone, I have two files: filea: Sun Jun 21 14:37:56 2009 1 2 3 Sun Jun 21 11:47:16 2009 2 3 4 fileb: Sun Jun 21 14:37:56 2009 1 2 3 Sun Jun 21 11:47:17 2009 33 44 55 The output is filec: Sun Jun 21... (4 Replies)
Discussion started by: jimmy_y
4 Replies

10. Shell Programming and Scripting

Duplicate rows in CSV files based on values

I want to duplicate a row if found two or more values in a particular column for corresponding row which is delimitted by comma. Input abc,line one,value1 abc,line two, value1, value2 abc,line three,value1 needs to converted to abc,line one,value1 abc,line two, value1 abc,line... (8 Replies)
Discussion started by: Incrediblian
8 Replies
Login or Register to Ask a Question