Operation on multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Operation on multiple files
# 1  
Old 10-01-2012
Operation on multiple files

I have four files that look like this:

file_1:
Code:
a b c
d e f

file_2:
Code:
g h i
j k l

file_3:
Code:
m n o
p q r

file_4:
Code:
s t u
v w x

I would like to write the sum of the 3rd column in each line of every file such that my new output files has 4 columns (4th column similar for all files) as follows:

file_1:
Code:
a b c c+i+o+u
d e f f+l+r+x

file_2:
Code:
g h i c+i+o+u
j k l f+l+r+x

file_3:
Code:
m n o c+i+o+u
p q r f+l+r+x

file_4:
Code:
s t u c+i+o+u
v w x f+l+r+x

Could someone help me with this problem. It's giving me a real hard time.

Thanks
# 2  
Old 10-01-2012
Could you please let us know what have you tried so far? we can build or help correcting that code for you..
# 3  
Old 10-01-2012
Well, I tried an awk stuff but didn't seem to catch it. Was thinking someone here has an idea I could work with
# 4  
Old 10-01-2012
Code:
 
#/bin/sh
awk '{a[FNR]+=$3;FNR>t?t=FNR:0;}END{for (i=1; i<=t; i++) print a[i];}' file_* > t_file
awk 'FILENAME ~ /t_file/ {a[NR]=$0;} FILENAME ~ /^file_/ {print $0 " " a[FNR] >> FILENAME ".tmp"}' t_file file_*
for file in file_*.tmp ; do mv $file ${file%.tmp} ; done
rm -f t_file

Note the last two lines move tmp files into original filenames. Comment out if not needed.
# 5  
Old 10-01-2012
thanks a million..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Do replace operation and awk to sum multiple columns if another column has duplicate values

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (12 Replies)
Discussion started by: as7951
12 Replies

2. Shell Programming and Scripting

Multiple Replacement in a Text File in one operation (sed/awk) ?

Hi all, Saying we have two files: 1. A "Reference File" whose content is "Variable Name": "Variable Value" 2. A "Model File" whose content is a model program in which I want to substitute "VariableName" with their respective value to produce a third file "Program File" which would be a... (4 Replies)
Discussion started by: dae
4 Replies

3. Shell Programming and Scripting

Grep strings on multiple files and output to multiple files

Hi All, I want to use egrep on multiple files and the results should be output to multiple files. I am using the below code in my shell script(working in Ksh shell). However with this code I am not attaining the desired results. #!/bin/ksh ( a="/path/file1" b="path/file2" for file in... (4 Replies)
Discussion started by: am24
4 Replies

4. UNIX for Dummies Questions & Answers

Run one script on multiple files and print out multiple files.

How can I run the following command on multiple files and print out the corresponding multiple files. perl script.pl genome.gff 1.txt > 1.gff However, there are multiples files of 1.txt, from 1----100.txt Thank you so much. No duplicate posting! Continue here. (0 Replies)
Discussion started by: grace_shen
0 Replies

5. Shell Programming and Scripting

awk math operation on two files

Hi, I need your help. I've got two files and i need to add 2nd line after occurrence of "Group No X" from data2.txt to 3rd line (after occurrence of "Group No X") from data1.txt. There is the same number of "Groups" in both files and the numbers of groups have the same pattern. data1.txt Group... (2 Replies)
Discussion started by: killerbee
2 Replies

6. Shell Programming and Scripting

[Solved] Mathematical operation in multiple files

Hi experts, I need to do a mathematical calculation between each data in 3 different files. Output is using formula (A11+B11)/(1+C11). INPUT : File A.txt A11 A12 A21 A22 File B.txt B11 B12 B21 B22 File C.txt C11 C12 C21 C22 OUTPUT: (A11+B11)/(1+C11) (A12+B12)/(1+C12)... (3 Replies)
Discussion started by: guns
3 Replies

7. UNIX for Dummies Questions & Answers

How to delete original files after using a tar operation.

I have a list of log files in a directory. Once i tar them I need to remove the original log files. How do i do it? (4 Replies)
Discussion started by: manutd
4 Replies

8. 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

9. Shell Programming and Scripting

Column operation : cosne and sine operation

I have a txt file with several columns and i want to peform an operation on two columns and output it to a new txt file . file.txt 900.00000 1 1 1 500.00000 500.00000 100000.000 4 4 1.45257346E-07 899.10834 ... (4 Replies)
Discussion started by: shashi792
4 Replies

10. Shell Programming and Scripting

multiple operation

Fellows I ran in to a problem this morning and could not figure out a solution to it. I have a file like OBJECT="ABC" GFT="JHU" DESCRIPTION="ABC MNCL JHDG " OBJECT="ABC" GFT="JHU" DESCRIPTION="ABC MNCL JHDG " OBJECT="ABC" GFT="JHU" DESCRIPTION="ABC MNCL JHDG " DESCRIPTION="ABC MNCL JHDG "... (2 Replies)
Discussion started by: ajnabi
2 Replies
Login or Register to Ask a Question