wc -l


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting wc -l
# 8  
Old 02-19-2008
a sed one

Code:
$ sed -n '$=' <filename>

# 9  
Old 02-19-2008
Quote:
Originally Posted by fristy_guy
Hi,

This might be a very basic question but i am begineer with UNIX. The output of wc -l gives the line count along with the filename.

$ wc -l compare_output.dat > test.dat
$ more test.dat
10 compare_output.dat

I just want the digit 10 in this sceniro. Can anyone plez help me on this

Thanks in advance
Code:
wc -l compare_output.dat | cut -d " " -f 1

# 10  
Old 02-19-2008
Using Awk

wc -l [filename] | awk '{print $1}'

replacing [filename] with the name of the file the output will give you the line count.

Hope that helps
# 11  
Old 02-19-2008
I'm having a similar problem. I'm trying to assign the results of a line count to a variable so it can be used in an arithmetic expression which in turn will split a document in half (on the basis of the a halved line count). No such luck. Any hints?
# 12  
Old 02-19-2008
The output from "wc" usually has some embedded whitespace, which can prevent the shell from treating it like a number:
Code:
# num=$(wc -l < /etc/hosts)
# echo "NUM='$num'"        
NUM='    1605'        # whoops!

# ((num=$(wc -l < /etc/hosts)))
# echo "NUM='$num'"
NUM='1605'            # that's better

Of course, there are other ways to get the number:

Code:
# wc -l /etc/hosts | read num garbage
# num=$(awk 'END{print NR}' /etc/hosts)
# num=$(grep -c . /etc/hosts)      # this excludes blank lines!

Choose your poison.
# 13  
Old 02-19-2008
Split file wc -l

Here's a stab at it.

==========Script follows ===========
#!/bin/ksh
filename=$1
tline=$(wc -l $filename | awk '{print $1}')
let hline=$tline/2
split -l $hline $filename
=========== End of script ==========

This will output 2 files, call xaa and xab by default, check the man page for split to see how to make it what you want.

Syntax to execute it is scriptname [file_to_be_split]

Variable definitions
filename - file to be split
tline - number of lines in the file
hline - 1/2 of the number of lines in the file

Hope this helps, sure there are other ways.

Last edited by olenkline; 02-19-2008 at 05:52 PM.. Reason: Use of obsolete ` in Ksh script, and forgot a '
# 14  
Old 02-19-2008
beware odd number of lines for the split

I'd modify olenkline's script slightly ... mainly because an odd value of hline would have split the file into three pieces!

==========Script follows ===========
#!/bin/ksh
filename=$1
tline=$(wc -l $filename | awk '{print $1}')
hline=$(((1+tline)/2))
split -l $hline $filename
=========== End of script ==========

numerous other ways to add the one so that hline=7 (rather than 6) when tline=13
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question