Re: Command Question.. (306 Views)
Reply
Frequent Advisor
Posts: 88
Registered: ‎06-27-2011
Message 1 of 7 (356 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
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 7 (348 Views)

Re: Command Question..

Hi Allan:

 

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

 

Regards!

 

...JRF...

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

Re: Command Question..

Thanks JRF,

Tried but getting error -

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

Allan.
Acclaimed Contributor
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 7 (343 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
Posts: 6,191
Registered: ‎10-13-1997
Message 5 of 7 (324 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
Posts: 25,711
Registered: ‎03-06-2006
Message 6 of 7 (310 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
Posts: 6,191
Registered: ‎10-13-1997
Message 7 of 7 (306 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.