arrays - 将每一行重复成新的一行

标签 arrays linux bash loops

我有一个如下所示的测试文件:

Hello 2
Bye 3
Tango 4

(真实文件有3万行)

我想得到一个看起来像这样的新文件:

Hello
Hello
Bye
Bye
Bye
Tango
Tango
Tango
Tango

我试过了,但是没用:

#!/bin/bash

Mywords=( $(awk '{ print $1 }' test) )
MyInteger=( $(awk '{ print $2 }' test) )
Countline=$(awk '{ print $1 }'  test | wc -l)

for ((i=0; i<$Countline ;i=i+1))
do
    for ((y=0; y<${MyInteger[$i]}  ;y=y+1))
        echo -e ${Sequences[$i]} > mynewfile
    do
    done
done

Mywords 数组包含我所有的话(ByeHelloTango)和 MyInteger 数组包含我希望每个单词重复的次数。

最佳答案

使用 awk:

awk '{for (i=0; i<$2; i++) print $1}' file > output

关于arrays - 将每一行重复成新的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17557131/

相关文章:

arrays - 时间复杂度 : Findning the mode of an array

Python numpy 数组合并操作

java - json响应迭代android studio/retrofit

linux - 如果不同文件中相同位置的字段值为 "zero",则修改一个文件的字段值

c - 如何在 C 语言中按 ASCII 顺序对命令行参数进行排序

javascript - 在 JS 中计算直方图的相似值的最佳方法是什么?

linux - git 更改权限在下一次提交之前无法持续

c - 如何确定 EXE 进行的所有系统和库调用

bash - 如何在 bash 脚本中进行字符比较?

linux - 将我当前的 IP 添加到 iptables 的白名单中?