AWK print number of records, divide this number


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK print number of records, divide this number
# 1  
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
Code:
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?

Last edited by Franklin52; 06-29-2012 at 05:10 AM.. Reason: Please use code tags for data and code samples
# 2  
That wouldn't do what you think it does, since a file with 5 lines would print
Code:
1
2
3
4
5


How about:
Code:
awk 'NR==1{F=FILENAME}; FILENAME != F { A[++FNUM]=LNR; F=FILENAME } { LNR=FNR } END { A[++FNUM]=LNR; print A[1]/A[2] }' file1 file2

# 3  
Hi.

With some help from bash:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate process substitution, awk arithmetic.

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C awk

pl " Input data files data1 data2:"
paste data1 data2

pl " Results:"
awk -v n1=$(wc -l <data1) -v n2=$(wc -l <data2) 'BEGIN	{print n1/n2; exit}'

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
awk GNU Awk 3.1.5

-----
 Input data files data1 data2:
foo	grault
bar	garble
baz	warg
qux	fred
quux	plugh
corge	xyzzy
	thud

-----
 Results:
0.857143

See man pages for details.

Best wishes .... cheers, drl
# 4  
Code:
 
#!/bin/ksh
record_count_1=`wc -l < file1`
record_count_2=`wc -l < file2`
 
((result= ${record_count_1} / ${record_count_2} ))

# 5  
Hi.

The experience I had with ksh suggests that I needed to declare variables to be other than default types to obtain useful results:
Code:
#!/usr/bin/env ksh

# @(#) user1	Demonstrate ksh (( arithmetic )), typeset.

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && . $C 

pl " Input data files data1 data2:"
paste data1 data2

# record_count_1=`wc -l < file1`
# record_count_2=`wc -l < file2`
record_count_1=`wc -l < data1`
record_count_2=`wc -l < data2`
 
pl " Results of $record_count_1 / $record_count_2:"
((result=${record_count_1} / ${record_count_2} ))
printf "%d\n" "$result"
printf "%f\n" "$result"

typeset -F3 t1 t2 ratio
t1=$record_count_1
t2=$record_count_2

pl " Results of $t1 / $t2:"
((ratio=$t1 / $t2))
printf "%f\n" "$ratio"
printf "%s\n" "$ratio"

exit 0

producing:
Code:
% ./user1 

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
ksh 93s+

-----
 Input data files data1 data2:
foo	grault
bar	garble
baz	warg
qux	fred
quux	plugh
corge	xyzzy
	thud

-----
 Results of 6 / 7:
0
0.000000

-----
 Results of 6.000 / 7.000:
0.857000
0.857

See man ksh for details.

Best wishes ... cheers, drl
# 6  
Another one:
Code:
awk 'NR==FNR{n=NR}END{print n/FNR}' file1 file2

# 7  
Hi.

This variation may be slightly faster in real time:
Code:
#!/usr/bin/env bash

# @(#) s2    Demonstrate process substitution, awk arithmetic.

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C awk

pl " Input data files data1 data2:"
paste data1 data2

pl " Results:"
n1=$(wc -l <data1 &)
n2=$(wc -l <data2 &)
wait

awk -v n1="$n1" -v n2="$n2" 'BEGIN    {print n1/n2; exit}'

exit 0

producing:
Code:
% ./s2

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
awk GNU Awk 3.1.5

-----
 Input data files data1 data2:
foo	grault
bar	garble
baz	warg
qux	fred
quux	plugh
corge	xyzzy
	thud


-----
 Results:
0.857143

The 2 counting processes are done simultaneously in the background ( as far as can be done in parallel on any particular system ), but the same computational time is used ( possibly adding a bit for overhead ).

Best wishes ... cheers, drl
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #802
Difficulty: Medium
The CSS @font-face rule is not supported across the main browsers, Internet Explorer, Firefox, Opera, Chrome, and Safari.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

2. UNIX for Dummies Questions & Answers

Make all records with the same number of fields (awk)

Hi, input: AA|BB|CC DD|EE FF what I am trying to get: AA|BB|CC DD|EE| FF|| I tried to create first an UDF for printing repeats, but I think I have an issue with my END section or my array: function repeat(str, n, rep, i) { for(i=1 ;i<n;i++) rep=rep str return rep } ... (6 Replies)
Discussion started by: beca123456
6 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

Perl : print the sequence number without missing number

Dear Perl users, I need your help to solve my problem below. I want to print the sequence number without missing number within the range. E.g. my sequence number : 1 2 3 4 5 6 7 8 11 12 13 14 my desired output: 1 -8 , 11-14 my code below but still problem with the result: 1 - 14 1 -... (2 Replies)
Discussion started by: mandai
2 Replies

5. Shell Programming and Scripting

AWK: Cannot read Number of records greater than 1(NR>1)

Hi all, I have a tab-delimited text file of size 10Mb. I am trying to count the number of lines using, grep -c . sample.txtor wc -l < sample.txt or awk 'END {print NR}' sample.txtAll these commands shows the count as 1, which means they are reading only the first header line of the file.... (3 Replies)
Discussion started by: mehar
3 Replies

6. Shell Programming and Scripting

Print records which do not have expected number of fields in a comma delimited file

Hi, I have a comma (,) delimited file, in which few fields are enclosed with in double quotes " ". I have to print the records in the file which donot have expected number of field with the line number. File1 ==== name,desgnation,doj,project #header#... (7 Replies)
Discussion started by: machomaddy
7 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. Shell Programming and Scripting

Please help. How to print number with 'awk'?

Dear all, I have to transform a bunch of files into a new format. how to do it with bash script, especially with 'awk' command. the original data O,0,0.2839896442,-1.1628392411,-1.5976515479 Cr,0,-0.0502858628,-0.1725974837,-0.380627632 C,0,2.4506658279,-0.476964918,0.2299298249 ... (3 Replies)
Discussion started by: liuzhencc
3 Replies

9. Shell Programming and Scripting

awk - Number of records

Hi, Is it possible to find the total number of records processed by awk at begining. NR gives the value at the end. Is there any variable available to find the value at the begining? Thanks ---------- Suman (1 Reply)
Discussion started by: suman_jakkula
1 Replies

10. Shell Programming and Scripting

How to print number of lines with awk ?

Can some body tell me how to print number of line from a particular file, with sed. ? Input file format AAAA BBBB CCCC SDFFF DDDD DDDD Command to print line 2 and 3 ? BBBB CCCC And also please tell me how to assign column sum to variable. I user the following command it... (1 Reply)
Discussion started by: maheshsri
1 Replies

Featured Tech Videos