Number of lines smaller than specified value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Number of lines smaller than specified value
# 1  
Old 12-22-2011
Number of lines smaller than specified value

Hi All,

I have a problem to find number of lines per column smaller than the values given in a different file. In example, compare the 1st column of file1 with the 1st line of the file2, 2nd column of file1 with the 2nd line of the file2, etc

Code:
 
cat file1
 
0.2 0.9 0.8 0.5 ...
0.6 0.5 0.7 0.9 ...
0.1 0.6 0.4 0.8 ...

Code:
 
cat file2
 
0.1
0.7
0.6
0.95
...

the output should look like

Code:
 
cat output
0
2
1
2
..

Thanks in advance,
# 2  
Old 12-22-2011
Code:
$ count=0;while read a; do count=$(expr $count + 1); awk -v b="$a" -v count="$count" 'NR==count{c=0;for(i=1;i<=NF;i++){if($i<b){c++}}print c}' file1; done < file2

# 3  
Old 12-22-2011
Try this...
Code:
awk 'NR==FNR{a[++j]=$0;next} {c=0;++k;for(i=1;i<=NF;i++){$i<a[k]?++c:NULL} print c}' file2 file1

Homework?

--ahamed
# 4  
Old 12-22-2011
Hi Ahamed,

Thanks for helping me. No it is not a homework. These are two files to get the permutated p-values in GWAS.

That script is pretty fast, however it giving the number of columns compared to the value. However i need number of rows per column which is smaller than the value given in the corresponding raw in file2. So, if there is 5000 column and 100 rows. The output should have 5000 values.

Question is howmany values in the 1st column of the file1 are smaller than the 1st value given in file2, etc.

Example.
file1 has 5 column and 3 rows

Code:
 
cat file1
 
0.6 0.4 0.9 0.8 0.53 
0.7 0.3 0.4 0.3 0.1
0.9 0.6 0.2 0.1 0.84

file 2 will have 5 rows.

Code:
 
cat file2
0.5
0.7
0.3
0.7
0.4

there are no values smaller than 0.5 in file1 so output will be 1 for the first row of the output

Code:
 
cat output
 
0
3
1
2
0


Last edited by senayasma; 12-22-2011 at 01:08 PM..
# 5  
Old 12-22-2011
I didn't get you, can you explain with an example?

--ahamed
# 6  
Old 12-22-2011
Question is howmany values in the 1st column of the file1 are smaller than the 1st value given in file2, etc.

Example.
file1 has 5 column and 3 rows

Code:
 
cat file1
 
0.6 0.4 0.9 0.8 0.53 
0.7 0.3 0.4 0.3 0.1
0.9 0.6 0.2 0.1 0.84

file 2 will have 5 rows.

Code:
 
cat file2
0.5
0.7
0.3
0.7
0.4

there are no values smaller than 0.5 in file1 so output will be 1 for the first row of the output

Code:
 
cat output
 
0
3
1
2
1

# 7  
Old 12-22-2011
output should be

0
3
1
3

---------- Post updated at 10:42 PM ---------- Previous update was at 10:41 PM ----------

ok, i got it....we need to compare with column wise...

---------- Post updated at 10:51 PM ---------- Previous update was at 10:42 PM ----------

Code:
$ count=0;while read a; do count=$((count + 1)); awk -v count="$count" '{printf("%s ",$count)}END{printf("\n")}' file1 >> file3 && awk -v b="$a" -v count="$count" 'NR==count{c=0;for(i=1;i<=NF;i++){if($i<b){c++}}print c}' file3; done < file2
0
3
1
2
1

This User Gave Thanks to itkamaraj For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print number of lines for files in directory, also print number of unique lines

I have a directory of files, I can show the number of lines in each file and order them from lowest to highest with: wc -l *|sort 15263 Image.txt 16401 reference.txt 40459 richtexteditor.txt How can I also print the number of unique lines in each file? 15263 1401 Image.txt 16401... (15 Replies)
Discussion started by: spacegoose
15 Replies

2. UNIX for Beginners Questions & Answers

Number of lines in vi

Is there a limit on the number of lines that can be copied into vi editor from another file? When I copy some data items from a file into the vi, I see a limit of 1780 lines can be pasted. After that there seems to be a limit. How can this be increased? As an example:, I open an excel file which... (3 Replies)
Discussion started by: ggayathri
3 Replies

3. Shell Programming and Scripting

How to write bash script to subdivide a given subnet into a pre-defined number of smaller subnets?

Bash script to subdivide a given subnet into a pre-defined number of smaller subnets. Show network/broadcast address, number of hosts and assign gateway. For example: Input: ./subnetter.sh 192.168.0.0/24 3 Output: 192.168.0.0/128 subnet 192.168.0.0 broadcast 192.168.0.127 gateway... (1 Reply)
Discussion started by: mail2rias
1 Replies

4. Shell Programming and Scripting

How to write bash script to subdivide a given subnet into a pre-defined number of smaller subnets?

Bash script to subdivide a given subnet into a pre-defined number of smaller subnets. Show network/broadcast address, number of hosts and assign gateway. For example: Input: ./subnetter.sh 192.168.0.0/24 3 Output: 192.168.0.0/128 subnet 192.168.0.0 broadcast 192.168.0.127 gateway... (1 Reply)
Discussion started by: mail2rias
1 Replies

5. Shell Programming and Scripting

Comparing floating number and replace the smaller one with awk?

Hi guys, I tried to write a script for replacing some number in a bunch of files. However, I failed because my limited bash scripting knowledge. Here I will explain the expected function: I have many data files need to be processed. Here shows one part of a file as an example: a list with... (7 Replies)
Discussion started by: liuzhencc
7 Replies

6. Shell Programming and Scripting

Grep lines for number greater than given number

Hello, I am newbie to bash scripting. Could someone help me with the following. I have log file with output as shown below **************************LOG************************* 11/20/2013 9:11:23.64 Pinging xx.xx.xx.xx with 32 bytes of data: 11/20/2013 9:11:23.64 Reply from xx.xx.xx.xx:... (4 Replies)
Discussion started by: meena_2013
4 Replies

7. Shell Programming and Scripting

count the number of lines that start with the number

I have a file with contents similar to this. abcd 1234 4567 7666 jdjdjd 89289 9382 92 jksdj 9823 298 I want to write a shell script which count the number of lines that start with the number (disregard the lines starting with alphabets) (1 Reply)
Discussion started by: grajp002
1 Replies

8. Shell Programming and Scripting

Number lines of file and assign variable to each number

I have a file with a list of config files numbered on the lefthand side 1-300. I need to have bash read each lines number and assign it to a variable so it can be chosen by the user called by the script later. Ex. 1 some data 2 something else 3 more stuff which number do you... (1 Reply)
Discussion started by: glev2005
1 Replies

9. Shell Programming and Scripting

Appending line number to each line and getting total number of lines

Hello, I need help in appending the line number of each line to the file and also to get the total number of lines. Can somebody please help me. I have a file say: abc def ccc ddd ffff The output should be: Instance1=abc Instance2=def Instance3=ccc Instance4=ddd Instance5=ffff ... (2 Replies)
Discussion started by: chiru_h
2 Replies
Login or Register to Ask a Question