Re: Command Question.. (220 Views)
Reply
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 1 of 7 (233 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.

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

Re: Command Question..

Hi Allan:

 

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

 

Regards!

 

...JRF...

Please use plain text.
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 3 of 7 (222 Views)

Re: Command Question..

Thanks JRF,

Tried but getting error -

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

Allan.
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 7 (220 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...

Please use plain text.
Honored Contributor
H.Merijn Brand (procura
Posts: 6,185
Registered: ‎10-13-1997
Message 5 of 7 (201 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
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,753
Registered: ‎03-06-2006
Message 6 of 7 (187 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.)

Please use plain text.
Honored Contributor
H.Merijn Brand (procura
Posts: 6,185
Registered: ‎10-13-1997
Message 7 of 7 (183 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
Please use plain text.
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