如何使用Bash逐行替换变量

时间:2014-07-08 19:02:01

标签: string bash replace sed

我对Bash比较陌生,所以请原谅我的天真。

我有效地尝试在第二十个字符的非常大的文件中标记每一行,标签取决于找到特定字符串的次数。所以从一般意义上讲,我希望迭代文件中的每一行并添加一个非静态变量。

以下是我(很多)试图让它逐行循环的尝试之一。

    #!/bin/bash

    COUNT=0              # Number of TERs encountered -> chain incrementer.
    CHAIN_LET="$(printf \\$(printf '%03o' $((65+$COUNT)) ))"
    PLACE=1


    while read LINE 
    do       
            if [[ "${LINE}" == *TER* ]]
            then
                    COUNT=$(($COUNT + 1))
                    CHAIN_LET="$(printf \\$(printf '%03o' $((65+$COUNT)) ))"
                    # I've been able to confirm that the code counts the instances
                    # properly.


            fi

            sed -e ''$PLACE's/./&'$CHAIN_LET'/19/' <text.pdb >text_out.pdb
            # Tried to ask sed to replace at each line.

            # sed 's/./&'$CHAIN_LET'/19' <text.pdb >text_out.pdb   
            # Doing this replaces the twentieth character at every iteration,
            # rather than at each line.  Basically, this isn't dynamic.            

            PLACE=$(($PLACE + 1))
    done <text.pdb

所以,例如,我希望转向以下内容:

    This
    Is
    TER
    An
    Example
    TER           
    Of
    A
    File

进入:

    This        A
    Is          A
    TER         B
    An          B
    Example     B
    TER         C  
    Of          C
    A           C
    File        C

非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

使用awk更简单:

awk -v s='TER' '$1==s{c++} {printf "%s%s%c\n", $1, OFS, (65+c)}' OFS='\t' file
This    A
Is      A
TER     B
An      B
Example B
TER     C
Of      C
A       C
File    C