2つの異なるcsvファイルの列を比較し、異なるファイルの列を置き換える方法

2つの異なるcsvファイルの列を比較し、異なるファイルの列を置き換える方法

ファイル1の列4(レベル)をファイル2の列1(レベル)と一致させる必要があります。一致するものがある場合は、File1(レベル)の列4をFile2(タグ)の列2に置き換えます。一致するものがない場合は、列4(レベル)の値を変更せずに残します。

ファイル1

"RollNo","Name","Age","Grade","Class","Teacher"
"1","Asha","7","A","1","David"
"2","Mona","7","B","1","David"
"3","Sonali","7","C","1","David"
"4","Rani","7","D","1","David"
"5","Raj","7","B","1","David"

ファイル2

"Grade","Marks"
"A","90"
"B","80"
"C","70"

予想出力:

"RollNo","Name","Age","Grade","Class","Teacher"
"1","Asha","7","90","1","David"
"2","Mona","7","80","1","David"
"3","Sonali","7","70","1","David"
"4","Rani","7","D","1","David"
"5","Raj","7","80","1","David"

この出力を得るためにawkコマンドを使用するのに役立ちますか?
ご返信をお待ちしております。

答え1

CSVのフィールドに,sまたは改行文字(\n)を含めることはできず、Unixシェルを実行しているとします。

$ awk '
    BEGIN { FS=OFS="," }
    NR==FNR { map[$1] = $2; next }
    (FNR>1) && ($4 in map) { $4 = map[$4] }
1' file2 file1
"RollNo","Name","Age","Grade","Class","Teacher"
"1","Asha","7","90","1","David"
"2","Mona","7","80","1","David"
"3","Sonali","7","70","1","David"
"4","Rani","7","D","1","David"
"5","Raj","7","80","1","David"

関連情報