Reading several files and summing their content line-by-line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading several files and summing their content line-by-line
# 1  
Old 06-30-2010
Reading several files and summing their content line-by-line

Hey,

I am gettin a bit crazy with my script. I have several input datas with the same name (5.ill) in different folders (daysim_01, daysim_02, etc.). The 4. column of each of the data has to be summed with each other and then hass to be written in one new file.

So file1:
Code:
1 1 0 1 
2 1 1 2 
3 1 2 3 
4 1 3 3 
5 1 4 6

file2:
Code:
1 1 0 4 
2 1 1 7 
3 1 2 9 
4 1 3 2 
5 1 4 4

The result should be:
Code:
1 1 0 5 
2 1 1 9 
3 1 2 12 
4 1 3 5 
5 1 4 10

But I am getting strange errors. Here is my skript:
Code:
for i in daysim_*
do
    cd $i/5/
    cat 5.ill | awk '{sum=$sum+$4; a=$1; b=$2, c=$3 }'
    cd ../..
    print $a, $b, $c, $sum >>ds_erg_ill_mean.dat
done

But it doesn't work. the error is always: Warning: unknown mime-type for "," -- using "application/octet-stream"
Does someone has an idea?

Thanks to any suggestion, Sam

Last edited by radoulov; 06-30-2010 at 12:06 PM.. Reason: Code tags, please!
# 2  
Old 06-30-2010
Code:
for i in daysim_*
 do
 cd $i/5/
 touch /tmp/temp;
 awk 'FILENAME=="/tmp/temp"{a[++i]=$4;next}FILENAME!="/tmp/temp" && FNR==1{system ("rm -f /tmp/temp; touch /tmp/temp")}\
{print $1,$2,$3,$4+a[FNR] >> "/tmp/temp"}' /tmp/temp 5.ill
done

Afetr that your sum should be in /tmp/temp
# 3  
Old 06-30-2010
hmmm, thanks first of all for your reply. What is the touch for?
And unfortunately there are errors. He is not able to find the file:

./skript.awk: line 3: cd: daysim_29/5/: No such file or directory

Secondly, I don't get it correctly: When are you reading the input file 5.ill?
Thanks and sorry for the stupid questions.

Sam
# 4  
Old 06-30-2010
You wanted to "cd" into "/5/" subdirectory, not me Smilie. I just copied that part from your code. And "touch /tmp/temp" is to create empty temp file, otherwise AWK will display error on the first run, becouse as you can see here:
Code:
...temp"}' /tmp/temp 5.ill

it reads temp file first, and then it reads 5.ill file.
# 5  
Old 06-30-2010
yes, and right: I wnated to go to /5/.... Smilie

Thanks a lot and it works perfectly.
# 6  
Old 07-13-2010
hi. what if I wanted to add ALL columns of file1 and 2 (add to that file3 and file4 with the same format)? how will the code look like?

appreciate your help.
# 7  
Old 07-13-2010
to add the columns of the files use below command:

paste file1 file2 file3 file4 ..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing two files and list the difference with common first line content of both files

I have two file as given below which shows the ACL permissions of each file. I need to compare the source file with target file and list down the difference as specified below in required output. Can someone help me on this ? Source File ************* # file: /local/test_1 # owner: own #... (4 Replies)
Discussion started by: sarathy_a35
4 Replies

2. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

3. Shell Programming and Scripting

Summing columns in line

I have a file with the following format AAAAA 1.34B 0.76B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.90B 0.00B 0.00B 0.46B 0.00B 0.03B 0.00B ... (4 Replies)
Discussion started by: ncwxpanther
4 Replies

4. Shell Programming and Scripting

Using awk to summing from a given line

My file is something like this : 03.097 03.094 03.093 03.095 03.091 04.089 06.093 07.225 08.196 06.097 06.094 05.096 04.086 I'd like to sum it from a given line to another one , e.g.: from line 10 until line 20 What s the awk way solving this ? (1 Reply)
Discussion started by: firelink
1 Replies

5. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

6. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

7. Shell Programming and Scripting

renaming multiple files with first line of content

Hi , I want to rename multiple files with their first line bar the first character + the extension .qual. For the example below the filename should read 7180000000987.qual. I have trawled through different threads for 2 days and I don't seem to find anything I can adopt for this task :confused: ... (7 Replies)
Discussion started by: Bruno
7 Replies

8. Shell Programming and Scripting

Reading a file line by line and processing for each line

Hi, I am a beginner in shell scripting. I have written the following script, which is supposed to process the while loop for each line in the sid_home.txt file. But I'm getting the 'end of file' unexpected for the last line. The file sid_home.txt gets generated as expected, but the script... (6 Replies)
Discussion started by: sagarparadkar
6 Replies

9. Shell Programming and Scripting

reading from two files line by line

hi am having two files more file1 a b c d more file2 a b c d i need a loop to read the 1st line from file1 and 1st line from file 2 and grep them from file3. Then go read the 2nd line and go on... i tried to do it but it was a mess. (2 Replies)
Discussion started by: windows
2 Replies
Login or Register to Ask a Question