Bash 根据数值插入行
如何使用bash和脚本工具从此文件开始:Bash 根据数值插入行,bash,file,text,lines,Bash,File,Text,Lines,如何使用bash和脚本工具从此文件开始: apples 2 oranges 3 bananas 1 pears 2 apples apples oranges oranges oranges bananas pears pears 要删除此文件,请执行以下操作: apples 2 oranges 3 bananas 1 pears 2 apples apples oranges oranges orange
apples 2
oranges 3
bananas 1
pears 2
apples
apples
oranges
oranges
oranges
bananas
pears
pears
要删除此文件,请执行以下操作:
apples 2
oranges 3
bananas 1
pears 2
apples
apples
oranges
oranges
oranges
bananas
pears
pears
我尝试了一些awk,但没有成功这将有效:
awk '{ for (i=0; i<$2; ++i) print $1 }' file
或者是一点Perl:
perl -ane 'print "$F[0]\n" x $F[1]' file
使用-a
将每行拆分为列,并打印第一列(+换行),打印次数与第二列的值相同。这将起作用:
awk '{ for (i=0; i<$2; ++i) print $1 }' file
或者是一点Perl:
perl -ane 'print "$F[0]\n" x $F[1]' file
使用
-a
将每行拆分为列,并打印第一列(+换行),打印次数与第二列的值相同。您甚至可以在纯bash中执行此操作:
#!/bin/bash
while read -r fruit count; do
for ((i = 0; i < count; i++)); do
echo "$fruit"
done
done
#/bin/bash
而read-r计数;做
对于((i=0;i
您甚至可以在纯bash中执行此操作:
#!/bin/bash
while read -r fruit count; do
for ((i = 0; i < count; i++)); do
echo "$fruit"
done
done
#/bin/bash
而read-r计数;做
对于((i=0;i
如果您已经尝试了某件事,则应将其包含在问题中如果您已经尝试了某件事,则应将其包含在问题中。我喜欢perl one.Tks的简单性。喜欢perl的简单性。