I have recently started learning shell scripting (BASH)in my school ,now we must write a shell script that does the following :
The problem statement: it takes a directory name as an argument and handles the files in that directory according to the following rule:
• The files that end with extension .csv (comma-separated values) are moved to subfolder CSV. However, before doing the move, the columns of each file have to be summed and a new row containing the totals should be appended to each .csv file. Assume that the .csv files have 4 columns each with the following column
formatting: string, value, value, value.
The attempts:
I already did the part that takes the file and moves it to the new subfolder, I also managed to sum all the columns using awk , it may not be the best way ,but I'm still learning , the problem is in printing the result in the file , that is to add the new row that contains the sums. I thought that I could pass variables to awk to store the total of each column but I couldnt understand how to pass variables to awk ! , or may be there is a better way to do that.
Off course I'm working on each task alone ,then I'll combine them in one script , so this is a part of the script I wrote so far:
could you please help ?!
Birziet University ,West Bank ,Palestinian Territory Linux Laboratory ENCS313 Dr. Hanna Bullata
The problem statement:
it takes a directory name as an argument and handles the files in that directory according to the following rule:
The files that end with extension .csv (comma-separated values) are moved to subfolder CSV. However, before doing the move, the columns of each file have to be summed and a new row containing the totals should be appended to each .csv file. Assume that the .csv files have 4 columns each with the following column.
Ok, lets start with some plan: your program layout will have to look something like
Quote:
Originally Posted by RGD
I already did the part that takes the file and moves it to the new subfolder, I also managed to sum all the columns using awk , it may not be the best way ,but I’m still learning
Good. lets go over your solution:
First off, some explanation how awk works: basically it is a rule-based language. A file is read, line by line, and one rule after the other is applied to each line (which may or may not alter the lines contents). After application of all the rules the next line is read in and the process starts anew. A "rule" usually consists of following: a regexp and some commands. If the line matches the regexp, the commands accompanying it are executed, otherwise they aren't. No regexp means the commands are executed for all lines.
There are three special rules, named "BEGIN", "END" and one with no name at all. "BEGIN" is executed before any lines are read from input, "END" is executed after all the lines are being read. The rule with no name is executed for every line of the input file.
What does that mean for your script?
First off, you could do it all in one pass. Btw., it is good style to initialize variables you are going to use, instead of taking them for granted:
It should be easy for you now to modify the script according to your requirement.
Another point is: Might there be lines which don't need processing? Lets consider the following:
You don't want to process the lines 1 and 4 in this case. Do you have an idea how to achieve this, from what i told you?
Still, there is a more subtle point I'd like to raise - one, which isn't explicitly covered by your requirement, but is best learned from the very beginning of ones programming career: If you get user input you should validate it! You write:
From where do you know that "$1" is a legitimate directory (or a legitimate file, for that purposes)? Lets say i enter "yourscript /foo/bar/gnarble/furble/isnodirorfileatall". What would your script do?
You might want to read the man page for "test" (which is handy Unix utility and comes under two names: "test" and "[") to understand the following:
first off , I would like to thank you ,,your post was more than helpful .
Quote:
Another point is: Might there be lines which don't need processing? Lets consider the following:
I think I missed this point , I'll do something about it.
Quote:
Still, there is a more subtle point I'd like to raise - one, which isn't explicitly covered by your requirement, but is best learned from the very beginning of ones programming career: If you get user input you should validate it!
Yes , I know its an important point , and I'am going to make sure to add this to my script, it just about that my problem was in awk , now I understand what to do .
HI Guys,
I gave Input file F.Txt
ID H1 H2 H3 H4 H5
A 5 6 7 8 9
B 4 65 4 4 7
C 4 4 4 4 4
D 4 4 4 4 4
Output :-
ID H1 H2 H3 H4 H5
Total 17 79 19 20 24
Sum of Each Columns (8 Replies)
Hi Friends,
I have a file with fields separated with comma. How to print sum of each field of the file?
Eg:
input file
1,3,6,7
2,1,2,1
0,1,1,0
I want to sum each field separately.
Output file
3,5,9,8
Thanks,
Suresh (2 Replies)
I have a list of values ( in Kb) I have the following code to sum up the values and convert the total to GB
cat list
701368101370
101370101370
801554101370
701636101370
101757101370
101876101370
901951101370
And this is the output of my script
awk '{ s += $1 } END {... (3 Replies)
Hi, Unix Gurus,
I need sum values from a file. file format like:
0004004
0000817
0045000
0045000
0045000
0045000
0045000
0045000
0045000
0045000
0045000
0045000
0004406
the result should be 459227 (817+45000 ... + 4406)
anybody can help me out (7 Replies)
I am trying to get the sum of the first column of a file. When I use the same method for other files it works just fine... for some reason for the file below it gives me an error that I don't understand... I tried looking at different lines of the file and tried different things, but I still... (7 Replies)
Hi
I need to incorporate a 'sum' as follows into a script and not sure how. I have a variable per line and I need them to be summed, e.g below
1
23
1,456
1
1
34
46
How do I calculate the sum of all these numbers to ouptut the answer ( 1,562)
Thanks in advance (3 Replies)
Hi
i data looks like this:
student 1
Subject1 45 55
Subject2 44 55
Subject3 33 44
//
student 2
Subject1 45 55
Subject2 44 55
Subject3 33 44
i would like to sum $2, $3 (marks) and divide each entry in $2 and $3 with their respective sums and print for each student as $4 and... (2 Replies)
Hello everyone I need to write a script that sums numbers passed to it as arguments on the command line and displays the results. I must use a for loop and then rewrite it using a while loop. It would have to output something like 10+20+30=60
this is what I have so far
fafountain@hfc:~$ vi sum... (1 Reply)
Hi
I want to sum of 3 columns in file.
Example: I want to sum of 3 ,6,8 th columns in file(SUM(3,6,8)).
Using awk can sum of single column
awk '{a+=$3} END {printf ("%f\n",a)' file_name
Thanks inadvance
MR (2 Replies)