Home > database >  How to use shell regex command to split input line into tokens of decimals and operators?
How to use shell regex command to split input line into tokens of decimals and operators?

Time:07-13

How to use grep/sed/awk to achieve this:

Input: a statement of decimals and numeric operators and parentheses, can have tab and spaces in between:

2   5* 61.2 -(32.5 7)/ 8

Output: a string, containing each token(either a decimal or an operator or parentheses), seperated by a single comma:

2, ,5,*,61.2,-,(,32.5, ,7,),/,8

Could regular expression grep/sed/awk achieve this?

CodePudding user response:

echo '2   5* 61.2 -(32.5 7)/ 8' |
sed -Ee 's:[0-9] (\.[0-9] )?|[- */()]:&,:g' \
    -e  's/[[:blank:]] //g' \
    -e  's/,$//'

Gives:

2, ,5,*,61.2,-,(,32.5, ,7,),/,8

Global replace on a regex matching integers, decimal numbers, or one of the six characters - */(). Then remove tabs and spaces, and trailing comma.

CodePudding user response:

You may use this sed solution:

s='2   5* 61.2 -(32.5 7)/ 8'
sed -E 's~[[:blank:]]*([0-9] (\.[0-9] )?|[ /*()-])[[:blank:]]*~\1,~g; s/,$//' <<< "$s"
2, ,5,*,61.2,-,(,32.5, ,7,),/,8

CodePudding user response:

$ sed -E 's/ |\t//g;s/([-\ \*\/)(])/,\1,/g;s/,,*/,/g' <<<"2   5* 61.2 -(32.5 7)/ 8"
2, ,5,*,61.2,-,(,32.5, ,7,),/,8
  • Related