Awk Multiple Files & Aggregate


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk Multiple Files & Aggregate
# 1  
Old 11-12-2009
MySQL Awk Multiple Files & Aggregate

file 1:
Code:
70|236|PPS|0501011818|mms|20090706|001452|00000024|2|0000000000000000|00000|0000000000|0000000000|40948000|1
70|236|PPS|0501020076|mms|20090705|204408|00000019|2|0000000000000000|00000|0000000000|0000000000|40947930|1
70|236|PPS|0501020076|mms|20090705|213619|00000027|2|0000000000000000|00000|0000000000|0000000000|40947945|1
70|236|PPS|0501026938|mms|20090705|235453|00000600|2|0000000000000912|00000|0000000000|0000000000|40947985|1
70|236|PPS|0501026938|mms|20090706|000453|00000600|3|0000000000000000|00000|0000000000|0000000000|40948000|1

file 2:
Code:
2|
3|
18|
0|
1357|


I want to add file1.$8 to File2.$1 using awk reading both files

Result:
Code:
70|236|PPS|0501011818|mms|20090706|001452|26|2|0000000000000000|00000|0000000000|0000000000|40948000|1

aggregation is in field 8 (00000024 + 2)

Thanks

Last edited by radoulov; 11-12-2009 at 12:08 PM.. Reason: Use code tags, please!
# 2  
Old 11-12-2009
Code:
awk -F"|" -v OFS="|" 'NR==FNR {A[NR]=$1; next} {$8+=A[FNR]} 1' file2 file1
70|236|PPS|0501011818|mms|20090706|001452|26|2|0000000000000000|00000|0000000000|0000000000|40 948000|1
70|236|PPS|0501020076|mms|20090705|204408|22|2|0000000000000000|00000|0000000000|0000000000|40 947930|1
70|236|PPS|0501020076|mms|20090705|213619|45|2|0000000000000000|00000|0000000000|0000000000|40 947945|1
70|236|PPS|0501026938|mms|20090705|235453|600|2|0000000000000912|00000|0000000000|0000000000|40 947985|1
70|236|PPS|0501026938|mms|20090706|000453|1957|3|0000000000000000|00000|0000000000|0000000000|40 948000|1

# 3  
Old 11-12-2009
Use gawk, nawk or /usr/xpg4/bin/awk on Solaris:

Code:
awk -F\| 'getline f2 < "file2" && $8 += f2' OFS=\| file1

# 4  
Old 11-13-2009
Code:
paste -d"|" file1 file2 | awk '
        BEGIN{FS=OFS="|"}
        {
        $8+=$(NF-1)
        NF=NF-2
        print
        }'

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search & Replace: Multiple Strings / Multiple Files

I have a list of files all over a file system e.g. /home/1/foo/bar.x /www/sites/moose/foo.txtI'm looking for strings in these files and want to replace each occurrence with a replacement string, e.g. if I find: '#@!^\&@ in any of the files I want to replace it with: 655#@11, etc. There... (2 Replies)
Discussion started by: spacegoose
2 Replies

2. Shell Programming and Scripting

awk, multiple files input and multiple files output

Hi! I'm new in awk and I need some help. I have a folder with a lot of files and I need that awk do something in each file and print a new file with the output. The input file name should be modified when I print the outpu files. Thanks in advance for help! :-) ciao (5 Replies)
Discussion started by: gabrysfe
5 Replies

3. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

4. UNIX for Dummies Questions & Answers

Using AWK: Extract data from multiple files and output to multiple new files

Hi, I'd like to process multiple files. For example: file1.txt file2.txt file3.txt Each file contains several lines of data. I want to extract a piece of data and output it to a new file. file1.txt ----> newfile1.txt file2.txt ----> newfile2.txt file3.txt ----> newfile3.txt Here is... (3 Replies)
Discussion started by: Liverpaul09
3 Replies

5. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

6. UNIX for Advanced & Expert Users

AWK aggregate records

Hy all, I have a problem...can some one help me... I have a file of records sort: 30|239|ORD|447702936929 |blackberry.net |20080728|141304|00000900|2|0000000000000536|28181|0000000006|0000000001|10|1 30|239|ORD|447702936929 |blackberry.net ... (4 Replies)
Discussion started by: anaconga
4 Replies

7. UNIX for Dummies Questions & Answers

Aggregate values in a file & compare with sql output

Hi, I have a file containing the following data: junk123junk723itemcode001qty01price10total10junkjunk junk123junk723itemcode002qty02price10total20junkjunk .. .. .. could be 5000+ lines I have an algo and need a code to implement this: 1. Linecount = wc -l (should give 5000) 2. For i... (1 Reply)
Discussion started by: shiroh_1982
1 Replies

8. Shell Programming and Scripting

sort & match multiple files

Hi, I have some question and need some guidance how to sort and match multiple files. 1. all the data in the files are numbers e.g. 1234567 1584752 2563156 2. each sorted file have their own ouput. e.g. test.csv -> test_sorted.csv 3. Then, I need to match all... (4 Replies)
Discussion started by: nazri76
4 Replies
Login or Register to Ask a Question