counting the number of lines


 
Thread Tools Search this Thread
Operating Systems Linux counting the number of lines
# 1  
Old 01-19-2005
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 this "Name ' word.

wc -l gives me the total number of lines while i want onlly the number of lines after the line having "Name " in it.

I would appreciate a response.

Nayeem
# 2  
Old 01-19-2005
Without knowing the file first hand, I can only suggest you use grep with options.

$ grep -c "Name" myfile

Will give the number of lines that the word "Name" is found.

Re-read your post - my grep suggestion won't help.

Try awk - see the man page for awk
You can look for a begining statement and ending statement - and then pipe it to wc -l

Example (you will have to try this to see if it or something like it will work)
awk /"Name"/,/"Ending statement"/ myfile |wc -l
# 3  
Old 01-19-2005
counting the number of lines

Google the command you suggested will give me the number of lines having the word Name in it.

I want the number of lines in the file follwing the line having name in it.
Name occurs only in one line.

So I want to count the number of lines in this file after this line having Name in it

Thanks

Nayeem
# 4  
Old 01-19-2005
I tried using awk the way you suggested but this also gives me only one line which is the one having the word Name in it.
I wanted to know the number of lines after this line
# 5  
Old 01-19-2005
You have to change the ending statement to something that is in the file -

Example:
cat myfile
First line
Name line
another line
even more lines
ending line

$ cat myfile |awk /"Name"/,/"even"/ |wc -l
3
So it's going to count the Name line, another line, and the last line - set the output of wc to a variable and minus 1 - but you need to put an ending statement in the awk parameters that is in your file or put something that won't ever be there (in this next example "neverfindthisword" ) and it will count all lines after (including) the Name line

$ cat myfile |awk /"Name"/,/"neverfindthisword"/ |wc -l
4

You might want to post the OS and version - there may be differences in the awk program you use.
# 6  
Old 01-19-2005
Yes, sorry. I posted that then quickly deleted that after I realized it was flat out wrong. You probably should use sed. Use Name as the first address and the end of the file as the second (Not sure how to do that - maybe others here know). Then count the lines in between.
# 7  
Old 01-19-2005
How about:
sed '1,/Name/d' datafile | wc -l
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

[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,... (2 Replies)
Discussion started by: VagabondGold
2 Replies

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

4. Shell Programming and Scripting

Counting the number of characters

Hi all, Can someone help me in getting the following o/p I/p:... (7 Replies)
Discussion started by: Sri3001
7 Replies

5. Shell Programming and Scripting

counting number of sentence

Hi all I want to count total numbers of sentences separated by fullstop (.) in different files under a directory at one go. Any help is appreciated. (3 Replies)
Discussion started by: my_Perl
3 Replies

6. Shell Programming and Scripting

counting the number of occurences

say i've got a text file with >10million sequences: ssss ssss tttttt uuuuuu uuuuuu uuuuuu ... I'd like to convert the file so that the output will report the number of occurence right by each sequence: 2 ssss 2 ssss 1 tttttt 3 uuuuuu 3 uuuuuu 3 uuuuuu .... (3 Replies)
Discussion started by: johjoh
3 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. UNIX for Dummies Questions & Answers

Counting number of occurences

Hi All, I have to count the number of occurences of the character " ; " in a given line. I had used the following awk command to achieve the same echo $KOP.dat|awk '{split($1,my,";"); for(i in my)c++ }END{print c-1}' My file KOP.dat had the following data ... (1 Reply)
Discussion started by: kingofprussia
1 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