Wednesday, July 27, 2022

[SOLVED] How to use shell regex command to split input line into tokens of decimals and operators?

Issue

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?


Solution

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


Answered By - anubhava
Answer Checked By - Candace Johnson (WPSolving Volunteer)