将文件 2 中文件 1 中的同一行替换为条件

将文件 2 中文件 1 中的同一行替换为条件

文件一:

19a9s
c9019
5777

文件2:

99a9s
89019
10919

预期产出

19a9s
89019
5777

所以它以 letterfile 1开头。如果该行以字母开头,我想将其用作条件,所以我想从第二个文件中替换它line#2ccfile 2

我尝试了以下操作,但未能给出预期的输出:

awk '
    NR == FNR{         #for lines in first file
        S[NR] = $0     #put line in array `S` with row number as index 
        next           #starts script from the beginning
    }
    /^c/{$0=S[FNR]}{             #for line stared with `c`
        $0=S[++count]  #replace line by corresponded array element
    }
    1                  #alias for `print $0`
    ' file2 file1

答案1

awk '
    NR == FNR{         #for lines in first file
        S[NR] = $0     #put line in array `S` with row number as index 
        next           #starts script from the beginning
    }
    /^c/{$0=S[FNR]}{             #for line stared with `c`
    }
    1                  #alias for `print $0`
    ' file2 file1

答案2

会走多远

paste file[12] | sed 's/^c[^   ]*      //; t; s/       .*$//;'
19a9s
89019
5777

我懂了?

相关内容