![]() |
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... |
Minor variation with 29 bytes
[SPOILER]sed 's/a/a\n/g' a.a|grep -c a[/SPOILER] |
Very nice!
Who can beat 29 bytes? |
Minor variation with 28 bytes
[spoiler]sed s/a/a\\n/g a.a|grep -c a[/spoiler] |
[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 |
[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.