mersenneforum.org

mersenneforum.org (https://www.mersenneforum.org/index.php)
-   Linux (https://www.mersenneforum.org/forumdisplay.php?f=39)
-   -   Shell script puzzle #1 (https://www.mersenneforum.org/showthread.php?t=1975)

Xyzzy 2004-01-23 04:44

Shell script puzzle #1
 
You have a file called a.a

It contains this:

aaaaabbbbb
aaabbb

Make a shell script to count the total numbers of a's in the file...

My answer:

[spoiler][FONT=Courier New]sed 's/a/a\n/g' a.a|grep a|wc -l[/spoiler][/FONT]

My answer is 32 bytes long, can anyone make an equivalent command that is shorter?

PS - You can only use tools that are ordinarily shipped with Linux...

gowen72 2004-01-23 05:22

Minor variation with 29 bytes

[SPOILER]sed 's/a/a\n/g' a.a|grep -c a[/SPOILER]

Xyzzy 2004-01-23 05:35

Very nice!

Who can beat 29 bytes?

aaronl 2004-01-25 13:15

Minor variation with 28 bytes

[spoiler]sed s/a/a\\n/g a.a|grep -c a[/spoiler]

xilman 2004-01-26 09:32

[QUOTE=Xyzzy]Very nice!

Who can beat 29 bytes?[/QUOTE]
[spoiler]tr -dc a<a.a|wc -c[/spoiler]

18 bytes, not including terminal newline.

Tested on RedHat 7.2, FreeBSD 4.9 and SFU 3.5

Yes, there are Microsofties who know how to program Unix-like systems. :wink:

Paul

xilman 2004-01-26 10:31

[QUOTE=Xyzzy]Very nice!

Who can beat 29 bytes?[/QUOTE]

Six bytes, not including terminal newline.

[spoiler]echo 8[/spoiler]

Yes, it [b]is[/b] cheating and it [b]is[/b] meant as a joke. Don't get too upset.

Paul


All times are UTC. The time now is 07:11.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.