No. of lines in a file (For scripting)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting No. of lines in a file (For scripting)
# 1  
Old 03-31-2006
No. of lines in a file (For scripting)

I'm trying to get the number of lines in a file. I know their are a number of ways of doing this like wc -l or nl which lists the file with the line number. The problem is that I want to use the number of lines a certain program has as a variable in my script. So i can't use wc -l as it returns the number of lines and the name of the file. I tryed to use sed to edit this but all I could come up with was something like this:

linenum=`wc -l $2 | sed -e 's/ /::/5;s/::.*//'`

What is does is places a :: after the fifth space (which is just after the number lines) and deletes after this. So before i ran this i got this:
135 myfile
But after i get this:
135

Which would be fine except that when a file has say 35 lines the no. of spaces change. I'm sure their is a pretty simple way of doing this, does anyone have any ideas.
# 2  
Old 03-31-2006
Quote:
So i can't use wc -l as it returns the number of lines and the name of the file.
why dont u try it as,

wc -l < filename
# 3  
Old 03-31-2006
cheers

Cheers mate
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all CONSECUTIVE text lines from file shell scripting

Hi I have a text file like below. THe content of the text will vary. Entire text file have four consecutive lines followed with blank line. I want to delete the occurrence of the two consicutive lines in the text file. I don't have pattern to match and delete. Just i need to delete all... (5 Replies)
Discussion started by: RJSKR28
5 Replies

2. Shell Programming and Scripting

Deleting specific lines from text file via scripting

Hi, I'm trying to search for some number and from that line, i need to delete the 5th line exactly. Eg: Consider below as text file data: 10000 a b c d e . . . 10000 w q t (8 Replies)
Discussion started by: Gautham
8 Replies

3. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

4. Shell Programming and Scripting

Scripting Question - getting lines above ane below certain enteries

Hello - I have a file in the followig format LINE TEXT 1 Line TEXT 2 TIMESTARTED=Fri Aug 16 15:20:23 EDT 2013 START-OF-DATA 123 123444 23232323 END-OF-DATA Line TEXT 9 I need to get all the lines between the Start of Data and End of Data and pipe it to a file so output should be ... (5 Replies)
Discussion started by: mnassiri
5 Replies

5. Shell Programming and Scripting

Need to remove first 6 lines and last line in a array ---- perl scripting

Hi I have stored a command output in an array like below @a = `xyz`; actually xyz comnad will give the output like this tracker date xxxxxxx xxxxxxx --------------------- 1 a 2 b ---------------------- i have stored the "xyz" output to an... (3 Replies)
Discussion started by: siva kumar
3 Replies

6. Shell Programming and Scripting

Bash Scripting Help to automate replacing multiple lines

Background: I am writing a script to help me automate tweaks and things I apply to a custom Android rom I developed. I am on the very last part of my script, and I am stuck trying to find the right command to do what I seek. When I build roms from source, a file called updater-script is... (8 Replies)
Discussion started by: Silverlink34
8 Replies

7. Shell Programming and Scripting

Scripting to Duplicate Lines Based on Variable

Greeting all! I could use some assistance please. :) I've been searching for the best way to duplicate a line based on a variable in the next line. Sample Data: Nov 22 00:00:19 10.10.10.1 "%ASA-4-313005: No matching connection for ICMP error message: icmp src Outside:1.2.3.4 dst... (3 Replies)
Discussion started by: sjrupp
3 Replies

8. Shell Programming and Scripting

Scripting help to identify words count in lines

Hi everybody, i have this biological situation to fix: > Id.1 ACGTACANNNNNNNNNNNACGTGCNNNNNNNACTGTGGT >Id.2 ACGGGT >Id.3 ACGTNNNNNNNNNNNNACTGGGGG >Id.4 ACGTGCGNNNNNNNNGGTCANNNNNNNNCGTGCAAANNNNN ........ .... These are nucleotidic sequences with some "NNNN..." always of the same... (4 Replies)
Discussion started by: Giorgio C
4 Replies

9. Shell Programming and Scripting

Scripting help: Split a file into equal number of lines.

Experts, I have a file datafile.txt that consists of 1732 Line, I want to split the file into equal number of lines with 10 file. (The last file can have 2 line extra to match 1732) Please advise how to do that, Thanks in advance.. (2 Replies)
Discussion started by: rveri
2 Replies

10. Shell Programming and Scripting

concatenate lines using shell scripting

i have a mega file in this format: a, b, c, d, = a2, b2, c2, d2, = a3, b3 = i want to combine lines until = meet. the result should be : a,b,c,d, a2,b2,c3,d2 a3,b3 (13 Replies)
Discussion started by: dtdt
13 Replies
Login or Register to Ask a Question