ファイル2のファイル1の同じ行を条件に置き換えます。

ファイル2のファイル1の同じ行を条件に置き換えます。

ファイル1:

19a9s
c9019
5777

ファイル2:

99a9s
89019
10919

期待される出力

19a9s
89019
5777

だからそれは letter で始まりますfile 1。行が文字で始まる場合を条件として使用したいので、2番目のファイルで置き換えたいと思います。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

おすすめ記事