PDA

View Full Version : help with bash regexp



tehcavil
March 18th, 2013, 12:29 PM
#!/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?

schragge
March 18th, 2013, 02:45 PM
I'd use sed for this:

$ 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


#!/bin/sh
exec sed 's/(/\nstart_parens\n/g;s/)/\nend_parens\n/g' $1 >output.ir

tehcavil
March 19th, 2013, 01:14 AM
I'd use sed for this:

$ 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


#!/bin/sh
exec sed 's/(/\nstart_parens\n/g;s/)/\nend_parens\n/g' $1 >output.ir


thanks!