如何将文件追加到另一个tsv文件的第二列

时间:2018-10-23 11:12:32

标签: awk command

我有一个文件first.txt,如下所示:

2   a   ...
3   b   ...
5   c   ...
6   d   ...

我想将此文件附加到如下所示的second.tsv(有17列):

2   45   a   ...
3   56   b   ...
5   74   c   ...
6   62   d   ...

所需的输出是:

awk -F, '{getline f1 <"first.txt" ;print $1,f1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17}'  second.tsv 

如何添加到第二列? 我已经尝试过

    class ViewController: UIViewController {


    @IBOutlet weak var timerLabel: UILabel!
    @IBOutlet weak var goldCounter: UILabel!
    @IBOutlet weak var turnCounter: UILabel!

    var seconds = 15
    var timer = Timer()
    var gold = 1000
    var turns = 1



    func updatelabels () {
    goldCounter.text = String(gold)
    turnCounter.text = String(turns) }




    func runTimer() {
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimer),userInfo: nil, repeats: true)
    }

    @objc func updateTimer() {
        seconds -= 1
        timerLabel.text = String(seconds)
        if seconds == 0 {
            seconds = 15}
        }


    func increaseGold () {
        if seconds == 1 {
            gold = gold + 1000
            turns = turns + 1
        }}

override func viewDidLoad() {
        super.viewDidLoad()
        self.runTimer()
        goldCounter.text = String(gold)
        turnCounter.text = String(turns)
        // Do any additional setup after loading the view, typically from a nib.

    func increaseGold () {
        if seconds == 1 {
            gold = gold + 1000
            turns = turns + 1
        }}
   func updatelabels () {
        goldCounter.text = String(gold)
        turnCounter.text = String(turns) }

    }
}

但是没有用。这将first.txt的列添加到second.tsv的最后一列,并且没有制表符分隔。 谢谢。

2 个答案:

答案 0 :(得分:1)

如果删除-F,位,则您的代码有效。这告诉awk文件是逗号分隔的,不是。

另一种选择是使用paste的管道版本,例如:

paste first.tsv second.tsv | awk '{ t=$2; $2=$1; $1=t } 1' OFS='\t'

输出:

2   45  a   ...
3   56  b   ...
5   74  c   ...
6   62  d   ...

答案 1 :(得分:1)

$ awk 'NR==FNR{a[FNR]=$0;next} {$1=$1 OFS a[FNR]} 1' file1 file2
2 45 a ...
3 56 b ...
5 74 c ...
6 62 d ...

如果文件以制表符分隔,请在前面添加BEGIN{FS=OFS="\t"}