I am trying to split of letter and number boundaries, but the solution with lookarounds fails:
echo 50cats30dogs100squirrels | awk '{split($0,a,/(?<=\D)(.*)(?=\d)/); print a[1],a[2],a[3]}'
awk: illegal primary in regular expression (?<=\D)(.*)(?=\d) at <=\D)(.*)(?=\d)
source line number 1
context is
>>> {split($0,a,/(?<=\D)(.*)(?=\d)/) <<<
Run Code Online (Sandbox Code Playgroud)
Is there a way to do this in Awk in other way?
Edit:
Sorry for not being clear. The expected output is to just add spaces like this:
50 cats 30 dogs 100 squirrels
Run Code Online (Sandbox Code Playgroud)
仅使用您展示的样品。如果这是您正在寻找的内容,请尝试以下操作。在 GNU 中编写和测试awk(应该适用于任何awk我相信的)。
echo "50cats30dogs100squirrels" | awk '{gsub(/[^0-9]+/," & ")} 1'
Run Code Online (Sandbox Code Playgroud)
显示示例的输出如下:
50 cats 30 dogs 100 squirrels
Run Code Online (Sandbox Code Playgroud)
有没有办法以其他方式在 awk 中做到这一点?
我将AWK按照以下方式使用 GNU完成此任务,让file.txt内容成为
50cats30dogs100squirrels
Run Code Online (Sandbox Code Playgroud)
然后
awk 'BEGIN{FPAT="([[:alpha:]]+)|([[:digit:]]+)"}{$1=$1;print}' file.txt
Run Code Online (Sandbox Code Playgroud)
输出
50 cats 30 dogs 100 squirrels
Run Code Online (Sandbox Code Playgroud)
说明:我指示 AWK 使用FPAT. 然后我做$1=$1导致字符串重建(没有$1=$1;输出将与输入相同)和print它。
(在 gawk 4.2.1 中测试)