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:

wc -l file.txt

To get the number of lines.
2.

 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!

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.

Let us see how to go about it.

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

Hi,
Just trick for wc -l :

$ wc -l a1
3 a1

with use redirect:

$ wc -l <a1
3

Otherwise, wc -l to get number of <newline> :slight_smile:
Regards.

1 Like

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:

-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:

-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:

printf "%.2f\n" $((209557 * 1.0 / $(wc -l < data.log) ))

produces:

43.59

or

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

produces:

43.5850665557404326
1 Like

Did it with echo, I thought with that you could only divide integers.
THANK YOU SO MUCH

The standards only require $((expression)) to handle integer arithmetic; recent versions of ksh support floating point operations in expression when one of the operands in an expression is a floating point value as an upwards compatible extension to the standards.