Divide number of lines by the size of the same file. And create relational table.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Divide number of lines by the size of the same file. And create relational table.
# 1  
Old 09-28-2013
Divide number of lines by the size of the same file. And create relational table.

I basically need to do what the title says.

I have my text file.

I'm still pretty new at this.

At the moment I know that:

1.
Code:
wc -l file.txt

To get the number of lines.
2.
Code:
 ls -lh file.txt

To get the file size.

But I need to divide both numbers. Then I need to save the output in a table named "table" with the follow structure:

date
lines
size
output

After that, I need to send the result of the query: select * from table, to user user2 with the command write.

Thank you all in advance!

Last edited by PainMaker101; 09-28-2013 at 01:07 AM..
# 2  
Old 09-28-2013
What have you tried so far, where are stuck?
# 3  
Old 09-28-2013
Quote:
Originally Posted by Scott
What have you tried so far, where are stuck?
So far I have tried with awk.

But since I'm still at the "hello world" stage I'm still constantly checking the man and Google. But none is successful. So I still don't have a clue what I should use to build this script. I'm more worried about how I can divide those two numbers once I know how to get them. I'm sure I can figure out how to create the relational table and sent it after that.
# 4  
Old 09-28-2013
Let us see how to go about it.
Code:
Last login: Sat Sep 28 15:14:29 on ttys000
AMIGA:barrywalker~> wc -l /tmp/txt
       1 /tmp/txt
AMIGA:barrywalker~> ls -lh /tmp/txt
-rw-r--r--  1 barrywalker  wheel    27B 28 Sep 14:18 /tmp/txt
AMIGA:barrywalker~> _

Saying you have used awk is meaningless without us seeing your attempt(s).
NOTE:- "/tmp/txt" in my example is a junk file only...

1) In the "ls -lh" line you have the date and time stamp and the file size in bytes.
2) You need to extract them somehow and this can just as easliy be done with
the standard bash tools as other tools at your disposal.
3) In the "wc -l" line extracting the number is just as easy as 2).
4) Once you have the numbers then the arithmetic is atandard.
5) However, ss the shell CANNOT handle floating point directly then there might be
a remainder which has to be considered.
6) Your "output" is vague as you have not specifed whether you want it as a file,
a variable, placed into an array or just a plain print to the terminal window/screen.
And finally 7) There are almost countless references to your needs in these archives.

I have certainly learnt a lot from the big guns on here but the vast majority I lerant
through trial and error from the command line, using the "man" files and searching
various archives...
# 5  
Old 09-28-2013
Hi,
Just trick for wc -l:
Code:
$ wc -l a1
3 a1

with use redirect:
Code:
$ wc -l <a1
3

Otherwise, wc -l to get number of <newline> Smilie
Regards.
This User Gave Thanks to disedorgue For This Post:
# 6  
Old 09-28-2013
Why would you use ls -lh if you want to perform arithmetic operations on the file size. The -h option will produce file sizes like:
Code:
-rw-r--r--   1 dwc  staff   460B Apr 24 10:43 action
-rw-r--r--   1 dwc  staff   205K Sep 28 10:46 data.log
-rw-r--r--   1 dwc  staff    41K Apr 24 10:43 dl.awk
-rw-r--r--   1 dwc  staff   118M Apr 24 10:43 intermediate
-rwxr-xr-x   1 dwc  staff   4.9K Sep 27 13:52 lunch

while ls -l for the same set of files produces:
Code:
-rw-r--r--   1 dwc  staff        460 Apr 24 10:43 action
-rw-r--r--   1 dwc  staff     209557 Sep 28 10:46 data.log
-rw-r--r--   1 dwc  staff      42086 Apr 24 10:43 dl.awk
-rw-r--r--   1 dwc  staff  123456792 Apr 24 10:43 intermediate
-rwxr-xr-x   1 dwc  staff       5040 Sep 27 13:52 lunch

The number of lines in data.log is 4,808. The code is much more complex if you want to compute 205k/4808 than it is to compute 209557/4808.

Also note that while wisecracker is correct in noting the bash shell won't directly perform floating point calculations, recent versions of the Korn shell (ksh) will. For instance:
Code:
printf "%.2f\n" $((209557 * 1.0 / $(wc -l < data.log) ))

produces:
Code:
43.59

or
Code:
echo $((209557 * 1.0 / $(wc -l < data.log) ))

produces:
Code:
43.5850665557404326

This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 09-28-2013
Quote:
Originally Posted by Don Cragun
Why would you use ls -lh if you want to perform arithmetic operations on the file size. The -h option will produce file sizes like:
Code:
-rw-r--r--   1 dwc  staff   460B Apr 24 10:43 action
-rw-r--r--   1 dwc  staff   205K Sep 28 10:46 data.log
-rw-r--r--   1 dwc  staff    41K Apr 24 10:43 dl.awk
-rw-r--r--   1 dwc  staff   118M Apr 24 10:43 intermediate
-rwxr-xr-x   1 dwc  staff   4.9K Sep 27 13:52 lunch

while ls -l for the same set of files produces:
Code:
-rw-r--r--   1 dwc  staff        460 Apr 24 10:43 action
-rw-r--r--   1 dwc  staff     209557 Sep 28 10:46 data.log
-rw-r--r--   1 dwc  staff      42086 Apr 24 10:43 dl.awk
-rw-r--r--   1 dwc  staff  123456792 Apr 24 10:43 intermediate
-rwxr-xr-x   1 dwc  staff       5040 Sep 27 13:52 lunch

The number of lines in data.log is 4,808. The code is much more complex if you want to compute 205k/4808 than it is to compute 209557/4808.

Also note that while wisecracker is correct in noting the bash shell won't directly perform floating point calculations, recent versions of the Korn shell (ksh) will. For instance:
Code:
printf "%.2f\n" $((209557 * 1.0 / $(wc -l < data.log) ))

produces:
Code:
43.59

or
Code:
echo $((209557 * 1.0 / $(wc -l < data.log) ))

produces:
Code:
43.5850665557404326

Did it with echo, I thought with that you could only divide integers.
THANK YOU SO MUCH
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create a pivot table from CSV file

Gents, Can you please help me to create a pivot table from a csv file. ( I have zip the csv file) Using the file attached, columns 1,28 and 21 i would like to get something like this output JD Val 1 2 3 4 5 6 7 8 9 10 11 12 Total... (4 Replies)
Discussion started by: jiam912
4 Replies

2. Shell Programming and Scripting

Create a control file from Table definition

Hi Team, I need to create a control file with a pre-defined structure for a given table name. The table is in teradata. Ex: Table Name: TBL1 Table structure: create multiset table tbl1, no fallback, no before journal, no after journal, checksum = default, default mergeblockratio... (7 Replies)
Discussion started by: unankix
7 Replies

3. Shell Programming and Scripting

Is it possible to Divide a negative number in bash script

I am using a small script to divide some numbers in a given file and display the output in another file. I am getting the following error basename: invalid option -- '5' Try `basename --help' for more information. (standard_in) 1: syntax error The script is : #!/bin/bash for i in `cat... (4 Replies)
Discussion started by: kmnr877
4 Replies

4. Shell Programming and Scripting

How to get the file size and count of a table using shell scripting?

Hi there, im a beginner to the shell scripting.i trying to extract a table from a db(IMD) and i have to get the count of that table and size of the file. can you help me out how to write the shall scriping for the above query. (2 Replies)
Discussion started by: pawanmamidi
2 Replies

5. Shell Programming and Scripting

AWK print number of records, divide this number

I would like to print the number of records of 2 files, and divide the two numbers awk '{print NR}' file1 > output1 awk '{print NR}' file2 > output2 paste output1 output2 > output awl '{print $1/$2}' output > output_2 is there a faster way? (8 Replies)
Discussion started by: programmerc
8 Replies

6. UNIX for Dummies Questions & Answers

create table file from different files with index

Hi, I've several files with two collumns, where first collumn can be used as index. filename1 and filename2 how to create a file I should start with cat all files and extract first collumn to create an index? (4 Replies)
Discussion started by: sargotrons
4 Replies

7. Shell Programming and Scripting

Add and divide each numbers with the added number

Hi All, I am stuck with this problem. I have some 100000 (.dat) 1.dat, 2.dat,3.dat etc until 100000.dat files which look like this: 1.dat 1 2 3 4 0.99 4.54 All my files 1.dat until 100000.dat look the same but with different numbers. I have to first add all the numbers in each... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

8. UNIX and Linux Applications

create table via stored procedure (passing the table name to it)

hi there, I am trying to create a stored procedure that i can pass the table name to and it will create a table with that name. but for some reason it creates with what i have defined as the variable name . In the case of the example below it creates a table called 'tname' for example ... (6 Replies)
Discussion started by: rethink
6 Replies

9. Shell Programming and Scripting

to create an output file as a table

Hi, I have four input files and would like to create an output file as a table. Please check the example below. File 1. 111111 222222 333333 444444 File 2. 555555 666666 777777 888888 File 3. aaaaa bbbbb ccccc ddddd (2 Replies)
Discussion started by: marcelus
2 Replies

10. Shell Programming and Scripting

Create n number of files of size x

What is the best way to create 'n' number of files of size 'x' lets say n and x are given as arguments to the program.. and lets say we can simply fill the files with 0s or *'s Thanks !! (2 Replies)
Discussion started by: the_learner
2 Replies
Login or Register to Ask a Question