how to get the no. of lines in a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to get the no. of lines in a text file
# 1  
Old 08-14-2009
how to get the no. of lines in a text file

Hi,

I want to count the no. of lines in a text file and print only the count(without file name) in another file with the prefix 'Count:'.

I'm able to count the no. of lines present in the file using wc -l <filename> but i have to subtract one from the count and print as i dont need to include the header. Please guide to do so.

TIA.
# 2  
Old 08-14-2009
Code:
echo -n "Count:" > file2.txt; wc -l file.txt | cut -d' ' -f1 >> file2.txt

# 3  
Old 08-14-2009
Another approach:

Code:
awk 'END{print "Count: " NR-1}' file > newfile

necroman08,

The OP needs the number of lines minus one!

Regards

Last edited by Franklin52; 08-14-2009 at 10:53 AM..
# 4  
Old 08-14-2009
Code:
#!/bin/bash
x=-1
while read line;do let ++x;done < file
echo Count: $x >> new_file


Last edited by danmero; 08-14-2009 at 03:34 PM.. Reason: Use shell build in only
# 5  
Old 08-14-2009
Quote:
Originally Posted by Franklin52
Another approach:

necroman08,

The OP needs the number of lines minus one!

Regards
O yes, I forgot about that that. So:
Code:
 
echo -n "Count: " > file2.txt; echo $( expr `wc -l file.txt | cut -d' ' -f1` - 1) >> file2.txt

Ok, itīs not so elegant as yourīs Smilie

Last edited by necroman08; 08-14-2009 at 11:14 AM..
# 6  
Old 08-14-2009
Code:
echo "Count: `sed -n '2,$ p' file | wc -l`" > newfile


Last edited by methyl; 08-14-2009 at 01:42 PM.. Reason: Forgot Count:
# 7  
Old 08-14-2009
Code:
perl -lne '$.>=2 and $i++; END{print "Count: ",$i}' file > newfile

perl -nE 'push @x,$_; END{say "Count: ",$#x}' file > newfile
 
perl -lne '$i++; END{print "Count: ",$i-1}' file > newfile

perl -lne 'END{print "Count: ",$.-1}' file > newfile

perl -nE 'END{say "Count: ",$.-1}' file > newfile

tyler_durden

Last edited by durden_tyler; 08-14-2009 at 01:42 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. UNIX for Dummies Questions & Answers

Add strings from one file at the end of specific lines in text file

Hello All, this is my first post so I don't know if I am doing this right. I would like to append entries from a series of strings (contained in a text file) consecutively at the end of specifically labeled lines in another file. As an example: - the file that contains the values to be... (3 Replies)
Discussion started by: gus74
3 Replies

3. Shell Programming and Scripting

Read n lines from a text files getting n from within the text file

I dont even have a sample script cause I dont know where to start from. My data lookes like this > sat#16 #data: 15 site:UNZA baseline: 205.9151 0.008 -165.2465 35.8109 40.6685 21.9148 121.1446 26.4629 -18.4976 33.8722 0.017 -165.2243 48.2201 40.6908 ... (8 Replies)
Discussion started by: malandisa
8 Replies

4. UNIX for Dummies Questions & Answers

Get the no. of lines in a text file

Is there any work around to get no. lines in a text file and cut by total of 2 lines per file or whatever input parameter of command. so for ei. this file cat file1 1 2 3 4 5 6 7 filename output is incremental look like this: linefile.txt 1_linefile.txt 2_linefile.txt... (4 Replies)
Discussion started by: lxdorney
4 Replies

5. Shell Programming and Scripting

How to delete lines of a text file based on another text file?

I have 2 TXT files with with 8 columns in them(tab separated). First file has 2000 entries whereas 2nd file has 300 entries. The first file has ALL the lines of second file. Now I need to remove those 300 lines (which are in both files) from first file so that first file's line count become... (2 Replies)
Discussion started by: prvnrk
2 Replies

6. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

7. UNIX for Dummies Questions & Answers

How to grep multiple lines from a text file using another text file?

I would like to use grep to select multiple lines from a text file using a single-column text file. Basically I want to only select lines from the first text file where the second column of the first text file matches the second text file. How do I go about doing that? Thanks! (5 Replies)
Discussion started by: evelibertine
5 Replies

8. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

9. UNIX for Dummies Questions & Answers

get only some lines from a text file

Hi, I would like to know how can I get to a new file, some lines from an existing file. Like, I want to get the lines between 100 and 150. Best regards. (1 Reply)
Discussion started by: fadista
1 Replies

10. UNIX for Dummies Questions & Answers

Deleting lines in text file

Hi everyone, I have text files that I want to delete lines from. I have searched through this forum for quite some time and found examples of both awk and sed. Unfortunately, I was not able to successfully do what I want. Well to some extent. I did manage to delete the first 15 lines from each... (5 Replies)
Discussion started by: hern14
5 Replies
Login or Register to Ask a Question