Re: Command Question.. (336 Views)
Reply
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 1 of 7 (349 Views)
Accepted Solution

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.

Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 7 (341 Views)

Re: Command Question..

Hi Allan:

 

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

 

Regards!

 

...JRF...

Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 3 of 7 (338 Views)

Re: Command Question..

Thanks JRF,

Tried but getting error -

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

Allan.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 7 (336 Views)

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
H.Merijn Brand (procura
Posts: 6,188
Registered: ‎10-13-1997
Message 5 of 7 (317 Views)

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
Dennis Handly
Posts: 25,198
Registered: ‎03-06-2006
Message 6 of 7 (303 Views)

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

Honored Contributor
H.Merijn Brand (procura
Posts: 6,188
Registered: ‎10-13-1997
Message 7 of 7 (299 Views)

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
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.