Results 1 to 3 of 3

Thread: help with bash regexp

Hybrid View

  1. #1
    Join Date
    Aug 2011
    Beans
    56

    help with bash regexp

    #!/bin/bash

    filename=$1

    rm -f output.ir

    one=`cat $filename`
    echo -e ${one//[()]/'\n'parens'\n'} >> output.ir
    thats my code so far, I'm trying to make a lisp -> bash compiler. this is what i have so far for the tokenizer part of the parser written in bash unfortunately my regexp knowledge is quite weak, i need help, so far this works by changing every ( and ) into a "parens". I want to change the ( into a "start_parens'\n'" and the ) into a "'\n'end_parens" in one pass/regexp. is this possible?

    edit: meanig can i do conditional substring replacement?

  2. #2
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: help with bash regexp

    I'd use sed for this:
    Code:
    $ echo '(())'|sed 's/(/start_parens\n/g;s/)/end_parens\n/g'
    start_parens
    start_parens
    end_parens
    end_parens
    Applying this to your code
    Code:
    #!/bin/sh
    exec sed 's/(/\nstart_parens\n/g;s/)/\nend_parens\n/g' $1 >output.ir
    Last edited by schragge; March 18th, 2013 at 03:01 PM.

  3. #3
    Join Date
    Aug 2011
    Beans
    56

    Re: help with bash regexp

    Quote Originally Posted by schragge View Post
    I'd use sed for this:
    Code:
    $ echo '(())'|sed 's/(/start_parens\n/g;s/)/end_parens\n/g'
    start_parens
    start_parens
    end_parens
    end_parens
    Applying this to your code
    Code:
    #!/bin/sh
    exec sed 's/(/\nstart_parens\n/g;s/)/\nend_parens\n/g' $1 >output.ir
    thanks!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •