【问题标题】:Using bash to copy contents of row in one file to specific character location in another file使用 bash 将一个文件中的行内容复制到另一个文件中的特定字符位置
【发布时间】:2018-10-25 21:57:45
【问题描述】:

我是 bash 新手,需要帮助将第 2 行从一个文件向前复制到另一个文件的特定位置(150 个字符)。通过浏览论坛,我找到了一种方法来包含此位置列出的特定文本:

sed -i 's/^(.{150})/\1specifictextlisted/'destinationfile.txt

但是,我似乎找不到将内容从一个文件复制到此文件的方法。

基本上,我正在处理这两个起始文件,需要以下输出:

文件1内容:

顺序
AAAAAAAAAGGGGGGGGGGGCCCCCCCCCTTTTTTTTT

文件2内容:

chr2
tccccagcccagccccggccccatccccagcccagcctatccccagcccagcctatccccagcccagccccggccccagccccagccccggccccagccccagccccggccccagccccggccccatccccggccccggccccatccccggccccggccccggccccggccccggccccatccccagcccagccccagccccatccccagcccagccccggcccagccccagcccagccccagccacagcccagccccggccccagccccggcccaggcccagcccca P>

想要的输出内容:

chr2 tccccagcccagccccggccccatccccagcccagcctatccccagcccagcctatccccagcccagccccggccccagccccagccccggccccagccccagccccggccccagccccggccccatccccggccccggccccatccccgAAAAAAAAAGGGGGGGGGGGCCCCCCCCCTTTTTTTTTgccccggccccggccccggccccggccccatccccagcccagccccagccccatccccagcccagccccggcccagccccagcccagccccagccacagcccagccccggccccagccccggcccaggcccagcccca P>

谁能让我走上实现这一目标的正确轨道?

【问题讨论】:

    标签: linux bash sed pipe


    【解决方案1】:

    如果文件真的很大,而不是只有 327 个字符,您可能需要使用 dd:

    dd if=chr2 bs=1 count=150 status=none of=destinationfile.txt
    tr -d '\n' < Sequence >> destinationfile.txt
    dd if=chr2 bs=1 skip=150 seek=189 status=none of=destinationfile.txt
    

    189 是 150+Sequence 的长度。

    【讨论】:

    • 最后的参数应该是of=destinationfile.txt还是你的意思是有两个不同的输出文件?
    • @tripleee 当然是的,感谢您发现它,那只猫变得不必要了。
    • tr 无法读取文件参数,您需要使用输入重定向。但是,是的,单个文件上的cat 总是没用的。
    【解决方案2】:

    您可以为此使用awk

    awk 'NR==FNR{a=$2;next}{print $1, substr($2, 0, 149) "" a "" substr($2, 150)}' file1 file2
    

    解释:

    # Total row number == row number in file
    # This is only true when processing file1
    NR==FNR {
        a=$2 # store column 2 in a variable 'a'
        next # do not process the block below
    }
    # Because of the 'next' statement above, this
    # block gets only executed for file2
    {
        # put 'a' in the middle of the second column and print it
        print $1, substr($2, 0, 149) "" a "" substr($2, 150)
    }
    

    我假设两个文件都只包含一行,就像你的例子一样。


    编辑:在 cmets 中,您说文件实际上分布在两行,在这种情况下,您可以使用以下 awk 脚本:

    # usage: awk -f this_file.awk file1 file2
    
    # True for the second line in each file
    FNR==2 {
        # Total line number equals line number in file
        # This is only true while we are processing file1
        if(NR==FNR) {
            insert=$0 # Store the string to be inserted in a variable
        } else {
            # Insert the string in file1
            # Assigning to $0 will modify the current line
            $0 = substr($0, 0, 149) "" insert "" substr($0, 150)
        }
    }
    
    # Print lines of file2 (line 2 has been modified above)
    NR!=FNR
    

    【讨论】:

    • 谢谢你的例子。抱歉,我复制并粘贴的文件的格式最初将文件 1 的内容显示为单行,但实际上是 2 行(在主帖中编辑)。同样,文件 2 在下面的序列行之前包含一个带有“Chr2”的初始行。我已经用我的文件名代替 file1 和 file2 输入了您的上述代码,但输出保持不变。我是否应该修改您代码中的其他任何内容以使其正常运行?
    • 假设序列头后只有一行,只需在最后一个块的左大括号前添加!/^&gt;/之类的条件,并在右大括号后添加/^&gt;/即可通过并打印序列头。
    • 早上好! :) 对不起,我刚刚在睡觉。我上班时会解决这个问题
    【解决方案3】:

    您可以使用 bash 并一次从文件中读取一个字符:

    i=1
    while read -n 1 -r; do
        echo -n "$REPLY"
        let i++
        if [ $i -eq 150 ]; then
            echo -n "AAAAAAAAAGGGGGGGGGGGCCCCCCCCCTTTTTTTTT"
        fi
    done < chr2 > destinationfile.txt
    

    这只是读取一个字符,回显它并增加计数器。如果计数器是 150,它会呼应您的序列。您可以用cat file | tr -d '\n' 替换回声。只要确保删除任何换行符,就像这里的tr。这也是我使用echo -n 的原因,所以它不会添加任何内容。

    【讨论】:

    • 感谢您的回复。我在这里部分成功地测试了您的代码:i=1; while read -n 1 -r; do echo -n $REPLY; let i++; if [ $i -eq 150 ]; then echo -n "AAAAAAAAAGGGGGGGGGGGCCCCCCCCCTTTTTTTTT"; fi;done &lt; file2 &gt; file1 是否可以在不包含序列“AAAAAAAAAGGGGGGGGGGGCCCCCCCCCTTTTTTTTT”的情况下实现这一目标?就目前而言,我可以使用sed -i 's/^(.{150})/\1AAAAAAAAAGGGGGGGGGGGCCCCCCCCCTTTTTTTTT/' file2 实现相同的目的,但重点是从单独文件的第 2 行获取它,而不是在代码中指定,因为我将在许多具有不同序列的文件中使用它。
    • 您可以通过在其后添加| tr -d '\n' 来去除换行符。我的其他答案也应该适用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多