1753952 Members
7851 Online
108811 Solutions
New Discussion

Re: Command Question..

 
SOLVED
Go to solution
allanm77
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 6
James R. Ferguson
Acclaimed Contributor

Re: Command Question..

Hi Allan:

 

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

 

Regards!

 

...JRF...

allanm77
Frequent Advisor

Re: Command Question..

Thanks JRF,

Tried but getting error -

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

Allan.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Command Question..

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

H.Merijn Brand (procura
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
Dennis Handly
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.)

H.Merijn Brand (procura
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