mtdewcmu 12 years ago

At one time, I became interested in the Turing-completeness of sed, and I started working on primitives for flow control and function calls. Then, I came to my senses.

gwu78 12 years ago

There's an error in line 1024. The transform strings are not the same length.

I'd like to see more sed programs on HN instead of the usual Python, Ruby or Go offerings.

I wrote a sed version of the the Python script "youtube-dl". This way I don't need Python installed to download YouTube videos. And it's easy to edit if YouTube suddenly changes their "API"; no need to wait for someone else to fix their Python or Lua script. In truth, it's not pure sed though. It uses sh (not bash), sed and ftp (or any other http client that handles 302's).

  • mkohlmyr 12 years ago

    Are you frequently on a unix platform without python? Just out of interest.

    I agree its nice to see a variety of languages. Gives a fresh perspective on how to do things.

    • gwu78 12 years ago

      Yes.

      I never need to install Python permanently.[1] In my experience, there is almost always a way to do what needs to be done with what UNIX provides, without installing more software. This keeps the system smaller, more standardized and hence more portable than one where various dependencies are haphazardly added.

      1. Sometimes I have to install it temporarily, for "reverse engineering" a non-Python solution, e.g., when someone decides to use Python as a substitute for make.

      • retr0h 12 years ago

        Ya totally! I still telnet to my servers... Don't want to fill my system with extra software. :P

        • gwu78 12 years ago

          Not sure I understand your comment.

          Like sed, all UNIX systems provide ssh. It's not "extra software". There's no need to use telnet.

          On the other hand, not all UNIX systems have Python pre-installed. Moreover, as in the case of embedded products, not all systems may have the space for it.

  • testtest12345 12 years ago

    You have to use Unicode sed.

    • gwu78 12 years ago

      Ah. Thanks. I just assumed a strictly POSIX sed would work. My mistake.

  • andrewcooke 12 years ago

    either it's fixed or you are having unicode issues. looks fine to me.

thelibrarian 12 years ago

This is a bit like that fellow who did pixel art by manually writing coloured table cells in HTML in Notepad - very, very impressive, but it does make one wonder a little about the person who has done it.

eddington 12 years ago

There appears to be a bug. Here is the state I reached (notice that "white" now has two bishops of the same color).

      a b c d e f g h
    8 ♖ ♘ ♗ ♕ ♔   ♘ ♖ 
    7 ♙ ♙ ♙ ♙ ♙ ♗     
    6                 
    5             ♙   
    4     ♝ ♟ ♟       
    3               ♙ 
    2 ♟ ♟ ♟     ♟ ♟ ♟ 
    1 ♜ ♞ ♝   ♚   ♞ ♜ 

Someone want to submit a pull request? :-)

edit: For anyone who wants to reproduce ("black" went first, since black appears as white on the board)

    1. e4 h5
    2. d4 h4
    3. Bb4 h3
    4. Qf3 g5
    5. Qxf7 Bxf7??
  • foobarbazqux 12 years ago

    I just tried this in real life and got the same bug. Hmmm.

  • wskinner 12 years ago

    It also seems to allow some illegal moves. The computer moved its bishop like a rook.

  • Moto7451 12 years ago

    On my first game I got an even better bug:

      a b c d e f g h
    

    8 ♔       ♛ ♝ ♘

    7 ♙ ♙ ♙ ♙

    6

    5         ♙

    4       ♟ ♟     ♕

    3   ♟ ♞

    2 ♟   ♟     ♟   ♟

    1 ♜     ♛ ♚ ♝   ♜

    After performing en passant the computer went crazy and the King started taking all the pieces between down row 8. I ended up with a very easy checkmate as I walked my pawn the remaining three rows, unfettered even when it was under double pressure from the night and queen.

    I wonder if this is perhaps an issue caused by the fact I used OS X's BSD sed.

    Edit: Hmm, can't seem to format it right. Anyways, the bug seems to be present in gnu sed as well. Really the whole game was screwy. I started with e2 e4 and it all went down hill from their for my opponent :).

    • jonmrodriguez 12 years ago

      The readme says passant isn't implemented, and there's no validation of the player's moves. So you did an illegal move.

      • Moto7451 12 years ago

        Interesting. Either way my opening seemed to be more problematic as the computer still went out of its way to checkmate itself without the en passant. Still a cool achievement.

ams6110 12 years ago

sed, and also awk, and ed, are worth learning. I mean coding a chess game in those tools is just a novelty, but you can do so much really useful text processing with just those tools, and they are pretty much guaranteed to be available on almost any *nix machine.

  • foobarbazqux 12 years ago

    What's an example of something non-interactive that ed is good for that you can't easily do with other tools?

    • gwu78 12 years ago

      OK I'll bite. For this answer I'll assume "easily" to mean "with less typing".

      ed has a more concise syntax for joining arbitrary lines of a file (by line number or pattern) and then saving the file (by way of a temp file in $TMPDIR). You could do this with awk and perhaps with other standard tools (note: I don't consider perl to be "standard"). But I think it's safe to say the ed script would always be shorter. ed's syntax for deleting and moving arbitrary lines is similarly concise. For these specific tasks, it's not going to be easy to win at code golf using awk or other tools... and in some cases it may be impossible.

      • foobarbazqux 12 years ago

        Hey cool, thanks. I've always overlooked it.

    • dmlorenzetti 12 years ago

      I use sed in makefiles, to scrape the source code for a version number, and then embed that version number in the name of the executable to be built. That would be easy enough in, say, Python, but as a sed script it's lightweight, decently readable, and puts everything you need to know right there in the makefile.

      That said, I've recommended learning Python to a few people, but have never even suggested anybody learn sed or awk.

      • foobarbazqux 12 years ago

        Oh, yeah, sed and awk are great, I meant ed in particular.

  • techtalsky 12 years ago

    That's true... but I haven't had to do real work on a system that didn't already have Python installed on it in a long time.

  • paraiuspau 12 years ago

    novelty yes, but let's not detract from the man's prowess; writing a chess game in sed is entirely non-trivial.

jaryd 12 years ago

Would be interested for someone to fork this and translate the cyrillic to english

  • shortstuffsushi 12 years ago

    Was going to comment the same thing. This seems like it could be really neat, but with all of the comments in a language I can't read, I'm not entirely sure what to think, haha.

munificent 12 years ago

Of course it was done by somebody Russian.

  • zerr 12 years ago

    Actually, Russians are quite pragmatic, as most other xUSSR people. So this is a rare counter-example, I believe.

comex 12 years ago

For bonus points, guess what this sed script does:

http://pastie.org/8261668

(hint: see line 14.)

  • pestaa 12 years ago

    Just a wild guess: would it be the asteroids game?

    • comex 12 years ago

      Nope, try again. ;p

  • anonymoushn 12 years ago

    It looks like it doesn't implement the input-accepting comma operator!

eeeeaaii 12 years ago

I love how he felt compelled to excuse his omission of castling and en passant moves by explaining that they were added to chess "later" (14th and 15th century)... maybe he started working on this project before those rules were added? :)

stormbrew 12 years ago

I don't want to detract from how absolutely awesome this is, but there are definitely some bugs in this.

- It routinely moves bishops vertically.

- It lets you make illegal moves yourself (not a huge deal).

- I got a square turning black for no reason.

Might also be nice if it told you what move it just made.

Demagog 12 years ago

First thing that come to my mind when I read the title.

Programmer who did something like that is mad. Or he/she is from Russia.

totalbs 12 years ago

I love that anything new was done with sed.

bananaoomarang 12 years ago

Hurrah for novelty usage of unix command line tools.

robjones 12 years ago

It's, like, really really bad at chess.

  • bch 12 years ago

    "The marvel is not that the bear dances well, but that the bear dances at all."

  • robjones 12 years ago

    and if you make illegal moves, it just moves again

  • primelens 12 years ago

    This is more about being very good at sed than bad at chess, really :)

agumonkey 12 years ago

This is almost more exciting than church numerals