Showing results for 
Search instead for 
Do you mean 

Command Question..

SOLVED
Go to Solution
Frequent Advisor

Command Question..

Hi All,

 

I have a file which looks like this -

 

Allan M

Allan T

John H

Terry P

Harry P

 

I want to print the file as well as do a line count -

 

Allan M

Allan T

John H

Terry P

Harry P

5

 

Want to do this as fast as it can be done, for example if we are talking about 10 miliion lines.

Need to do this in one single command without ";".

 

Thanks,

Allan.

6 REPLIES
Acclaimed Contributor Acclaimed Contributor

Re: Command Question..

Hi Allan:

 

# perl -ple '$n++;END{print $n}' file

 

Regards!

 

...JRF...

Frequent Advisor

Re: Command Question..

Thanks JRF,

Tried but getting error -

Unrecognized character \xEF in column 19 at -e line 1

Allan.
Acclaimed Contributor Acclaimed Contributor

Re: Command Question..

[ Edited ]

Hi (again) Allan:

 


allanm77 wrote:
Tried but getting error -

Unrecognized character \xEF in column 19 at -e line 1

Well, re-type what I posted.  However, here's an even shorter variation given your requirement:

 

perl -ple 'END{print $.}' file

...or if you prefer:

 

awk '{print};END{print NR}' file

Regards!

 

...JRF...

Honored Contributor Honored Contributor

Re: Command Question..

Of course I like the perl solution, but what is wrong with good old plain fast default unix tools?

$ wc -l<file>>file

 

 

Enjoy, Have FUN! H.Merijn
Acclaimed Contributor Acclaimed Contributor

Re: Command Question..

>but what is wrong with good old plain fast default unix tools?

 

Because there is no tool the both prints and gives that count.

(Unless you use nl(1) or cat(1) to number each line.)

Highlighted
Honored Contributor Honored Contributor

Re: Command Question..

Ah, I misread the OP as that it was required to add the line count to the end of the file.

Mea culpa. 

Enjoy, Have FUN! H.Merijn