myfile.csv
445,aLabel
446,anotherLabel
447,aThirdLabel
Bashスクリプトでmyfile.csvを検索して「anotherLabel」(大文字と小文字を区別しない)があるかどうかを検索し、値446を取得したいと思います。また、「anotherLabel」が存在しない場合は、最後に追加して前の行のラベルを1ずつ増やしたいと思います。
私は次の文から始めますif
:
if grep -Fiq anotherLabel myfile.csv; then
#get the value of field 1 (446 in this example) and store it in a variable
else
#increment the last value of field 1 present in the file and store it in a variable
#and append "448,anotherLabel" to myfile.csv
fi
ファイルにタグがあるかどうかを確認するためにgrepを使用することがこの問題を解決する最善の方法であるか、sedまたはawkを使用するより簡単な方法があるかどうかはわかりません。
ベストアンサー1
この試み:
awk -F',' '{ if ($2 == "anotherLabel") { print $1 } }' myfile.csv