Shell 将n*表达式替换为表达式的n倍
我想将n*表达式展开为表达式的n倍,例如:Shell 将n*表达式替换为表达式的n倍,shell,awk,sed,Shell,Awk,Sed,我想将n*表达式展开为表达式的n倍,例如: 3*2.5 3.4*1.5 到 2.5 2.5 2.5 3.4 1.5 1.5 1.5 1.5 我尝试了sed和awk,但远没有任何解决方案 我最近的审判是 cat test3times | awk-e'$1~/[0-9]\*/{print$0,$0,$0}' 但它仍然包含表达式前面的3*,并且对于4不变* 此外,它也不会打印其他部分,如3.4,请尝试以下内容 awk ' { for(i=1;i<=NF;i++){ split($i,
3*2.5 3.4*1.5
到
2.5 2.5 2.5 3.4 1.5 1.5 1.5 1.5
我尝试了sed和awk,但远没有任何解决方案
我最近的审判是
cat test3times | awk-e'$1~/[0-9]\*/{print$0,$0,$0}'
但它仍然包含表达式前面的3*,并且对于4不变*
此外,它也不会打印其他部分,如3.4,请尝试以下内容
awk '
{
for(i=1;i<=NF;i++){
split($i,arr,"*")
count=0
while(++count<=arr[1]){
if(count==1){val=""}
val=(val?val OFS:"")arr[2]
}
$i=(val?val:$i)
}
}
1
' Input_file
说明:添加上述内容的详细说明
awk ' ##Starting awk program from here.
{
for(i=1;i<=NF;i++){ ##Starting for loop till till value of NF(number of fields) here.
split($i,arr,"*") ##Splitting current field into array named arr with delimiter of * here.
count=0 ##Nullify count here.
while(++count<=arr[1]){ ##Running while loop from count 1 till value of arr first element value.
if(count==1){val=""}
val=(val?val OFS:"")arr[2] ##Creating val which has value of 2nd array element(which is digit after * actually in field)
}
$i=(val?val:$i) ##Saving value in current field itself.
}
}
1 ##Mentioning 1 will print current line here.
' Input_file ##Mentioning Input_file name here.
awk'##从这里启动awk程序。
{
对于(i=1;i和您展示的样品,请尝试以下内容
awk '
{
for(i=1;i<=NF;i++){
split($i,arr,"*")
count=0
while(++count<=arr[1]){
if(count==1){val=""}
val=(val?val OFS:"")arr[2]
}
$i=(val?val:$i)
}
}
1
' Input_file
说明:添加上述内容的详细说明
awk ' ##Starting awk program from here.
{
for(i=1;i<=NF;i++){ ##Starting for loop till till value of NF(number of fields) here.
split($i,arr,"*") ##Splitting current field into array named arr with delimiter of * here.
count=0 ##Nullify count here.
while(++count<=arr[1]){ ##Running while loop from count 1 till value of arr first element value.
if(count==1){val=""}
val=(val?val OFS:"")arr[2] ##Creating val which has value of 2nd array element(which is digit after * actually in field)
}
$i=(val?val:$i) ##Saving value in current field itself.
}
}
1 ##Mentioning 1 will print current line here.
' Input_file ##Mentioning Input_file name here.
awk'##从这里启动awk程序。
{
对于(i=1;你能想出一个“酷”的方法来更简洁地使用gawk的gensub
或类似的(没有循环)。我一直在尝试不同的事情:{print”[“gensub(/([:digit:][]+)\*([:digit:][.]+)/,“\\2”,“G”,sprintf(“%*s”,15“)”)}你能想出一个“酷”的方法来更简洁地表达gawk的gensub
或类似的(没有循环)。我一直在尝试不同的事情:{print“[”gensub(/([:digit:]+)\*([:digit:]+)/,“\\2”,“G”,“sprintf”(“%*s”,15“”)}
通过硬连线重复…无法让我的头绕过它…@vgersh99不。。。
$ awk -f tst.awk file
2.5 2.5 2.5 3.4 1.5 1.5 1.5 1.5