wc -l

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

wc -l <compare_output.dat >test.dat

Vino,

I am not quite getting what you are trying to say i have already redirected the output to test.dat. When we do a more of test.dat it has the line count followed by the filename/filepath. What i desire is only the count and excluding the trailing filename/filepath since i want it to be assigned to a varaible for further operation

Hi,

Have you tried the above code? It will give you what you expect

wc -l < filename

will give only the count of lines in the file.

Regards,
Chella

Look carefully and see the file redirection <

wc -l <compare_output.dat >test.dat
> wcl=$(cat find_file.txt | wc -l)
> echo $wcl
4

The below code also works

a sed one

$ sed -n '$=' <filename>
wc -l compare_output.dat | cut -d " " -f 1

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

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?

The output from "wc" usually has some embedded whitespace, which can prevent the shell from treating it like a number:

# 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:

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

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.

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

Thanks Gneen for the correction, hadn't thought about that.

Believe

let hline=$((1+($tline/2)))

is what you meant to type. Since adding one to the tline causes the issue to move from odd lines to even lines, and I think what you pointed out was that the line to be split (hline) would have to be 1 more than the true half to get only 2 files.

Again thanks for finding the error in logic.

thanks for the advice guys. unfortunately im not allowed to use SED or AWK to complete this script.