[Solved] Counting The Number of Lines Between Values with Multiple Variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Counting The Number of Lines Between Values with Multiple Variables
# 1  
Old 01-17-2014
[Solved] Counting The Number of Lines Between Values with Multiple Variables

Hey everyone,

I have a bunch of lines with values in field 4 that I am interested in.

If these values are between 1 and 3 I want it to count all these values to all be counted together and then have the computer print out

LOW and the number of lines with those values in between 1 and 3, so if there were 13 then it would be

LOW 13

Similarly, between values of 4 and 6 count the number of values and print out MEDIUM and the number of values, so if there were 50 then

MEDIUM 50

And again, between 7 and 10, HIGH and the number of lines, if there were 20 values

HIGH 20

I need all of these to happen together on one file and print out

LOW 13
MEDIUM 50
HIGH 20

I'm using awk to run these commands, but haven't really gotten anywhere. I know how to count patterns, but for some reason can't figure out what to do in this case.
# 2  
Old 01-17-2014
Try something like:
Code:
awk '
  $4>0 && $4<4 {
    L++
  }
  $4>=4 && $4<7 {
    M++
  }
  $4>=7 && $4<=10 {
    H++
  }
  END{
    print "LOW",L+0 RS "MEDIUM", M+0 RS "HIGH",H+0
  }
' file

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 01-17-2014
Thank you, I was easily able to adapt this to my data and it works perfectly.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help: Counting values less than a number

So I have several files (35000, to be exact) in the format rmsd_protein_*.dat each with 2 columns and 35000 rows. I would like to count how many values in the second column are less than 3 for each file, and output it into a new file so that it ultimately appears as: 1 14057 2 ... (12 Replies)
Discussion started by: Alexandryne
12 Replies

2. Shell Programming and Scripting

Reading multiple values from multiple lines and columns and setting them to unique variables.

Hello, I would like to ask for help with csh script. An example of an input in .txt file is below, the number of lines varies from file to file and I have 2 or 3 columns with values. I would like to read all the values (probably one by one) and set them to independent unique variables that... (7 Replies)
Discussion started by: FMMOLA
7 Replies

3. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

4. Shell Programming and Scripting

[Solved] How do i deal with values on multiple lines?

Hi, I have a file which has the contents: sh-4.2# pwd /tmp sh-4.2# cat servernfiles server1 /var/tmp/file server2 /var/tmp/file1 I want to manage each line one after the other. I have this basic script : #!/bin/sh HOST=`cat /tmp/servernfiles | awk '{print $1}'` CMD=`cat... (6 Replies)
Discussion started by: chandika_diran
6 Replies

5. Shell Programming and Scripting

Running sed and counting number of lines processed

/bin/sed -n ';4757335,$ p' | wc -l /bin/sed -n ';4757335,$ p' | egrep "Failed" | egrep -c "PM late arrrival" how can i combine the above two sed commands into one? i want to count the number of lines between the specified line number and the end of the file. AND and i want to count how many... (5 Replies)
Discussion started by: SkySmart
5 Replies

6. Shell Programming and Scripting

counting lines containing two column field values with awk

Hello everybody, I'm trying to count the number of consecutive lines in a text file which have two distinctive column field values. These lines may appear in several line blocks within the file, but I only want a single block to be counted. This was my first approach to tackle the problem (I'm... (6 Replies)
Discussion started by: origamisven
6 Replies

7. Shell Programming and Scripting

counting the number of lines - again

Hi all, I use bash shell and I have a problem with wc. I would like to determine the number of lines in a file so I do wc -l filename but I don't want to get the filename again I just would like to have the number of lines and use it in a variable. Can anybody help? Thank you, (7 Replies)
Discussion started by: f_o_555
7 Replies

8. Shell Programming and Scripting

awk - Counting number of similar lines

Hi All I have the input file OMAK_11. OMAK 000002EXCLUDE 1341 OMAK 000002EXCLUDE 1341 OMAK 000002EXCLUDE 1341 OMAK 000003EXCLUDE 1341 OMAK 000003EXCLUDE 1341 OMAK 000003EXCLUDE ... (8 Replies)
Discussion started by: dhanamurthy
8 Replies

9. Linux

counting the number of lines

Hello, I have afile which begins with a few urls on multiple lines and then there is listing of some information on separate lines. The listing begins with the word Name on a given line followed by teh actual list. I want to count the number of lines in this file after the line having... (6 Replies)
Discussion started by: nayeemmz
6 Replies

10. UNIX for Dummies Questions & Answers

Counting The Number Of Duplicate Lines In a File

Hello. First time poster here. I have a huge file of IP numbers. I am trying to output only the class b of the IPs and rank them by most common and output the total # of duplicate class b's before the class b. An example is below: 12.107.1.1 12.107.9.54 12.108.3.89 12.109.109.4 12.109.6.3 ... (2 Replies)
Discussion started by: crunchtime
2 Replies
Login or Register to Ask a Question