次のファイル名を持つテストフォルダがあります。
100080_Accounting Statistic-42005_04May2020_0000-04May2020_0100.csv
110001_ABCD Traffic_04May2020_header_only.csv
cat '110001_ABCD Traffic_04May2020_header_only.csv'
ID,NAME,LOCATION
1,Vikrant,Gurgaon
2,Bharat,Noida
3,Raju,Hyderabad
cat '100080_Accounting Statistic-42005_04May2020_0000-04May2020_0100.csv'
ID,NAME,AGE,DEPARTMENT
4,Raju,22,Admin
5,Rajeev,23,Admin
テストディレクトリから特定のファイルを読み込んでいる間にファイル名を抽出し、ファイル内の各レコードにそのファイル名を追加しようとしています。新しく作成されたファイルに実際のレコードを書き込む前に、ヘッダーに新しい列名 "GroupName"を追加し、このファイルを別のディレクトリに書きたいと思います。
以下は、ロジックが期待どおりに機能するシェルスクリプトですが、同じファイル名を持つ別のディレクトリに書き込む方法はありません。また、2 番目のファイルのヘッダーに列を追加する際にも問題があります。
#!/bin/bash
# Go to where the files are located
filedir=/home/vikrant_singh_rana/testing/*
#reading file from directory
for filename in $filedir; do
#extracting modified file name from file
b=$(basename "$filename" ".csv" | awk -F "_" '{print $2}' | awk -F "-" '{print $1}')
#reading line by line from file
first='yes'
while read -r line;do
#reading header from file
header=$(head -n1 "$filename")
if [[ $first == 'yes' ]]; then
#writing header with new column just once
printf '%s,%s\n' "GroupName" "$header"
first='no'
else
#writing modified filename to each record of a file
printf '%s,%s\n' "$b" "$line"
fi
done <"$filename"
#echo $filename
output_filename=$(basename "$filename")
#echo $output_filename
#done
done > /home/vikrant_singh_rana/enrichment_files/enrichment_file.txt
私が得た結果は次のとおりです。
cat /home/vikrant_singh_rana/enrichment_files/enrichment_file.txt
GroupName,ID,NAME,AGE,DEPARTMENT
Accounting Statistic,4,Raju,22,Admin
Accounting Statistic,5,Rajeev,23,Admin
GroupName,ID,NAME,LOCATION
ABCD Traffic,1,Vikrant,Gurgaon
ABCD Traffic,2,Bharat,Noida
ABCD Traffic,3,Raju,Hyderabad
私が期待するものは次のとおりです。ファイル名は私が読んでいる名前と同じで、出力は別々のファイルに書き込む必要があります。
file name as :110001_ABCD Traffic_04May2020_header_only.csv
GroupName,ID,NAME,AGE,DEPARTMENT
Accounting Statistic,4,Raju,22,Admin
Accounting Statistic,5,Rajeev,23,Admin
file name as :100080_Accounting Statistic-42005_04May2020_0000-04May2020_0100.csv
GroupName,ID,NAME,LOCATION
ABCD Traffic,1,Vikrant,Gurgaon
ABCD Traffic,2,Bharat,Noida
ABCD Traffic,3,Raju,Hyderabad
ベストアンサー1
出力をファイルに書き込め、次のように変更しました。変更が必要な場合は修正してください。
#!/bin/bash
# Go to where the files are located
filedir=/home/vikrant_singh_rana/testing/*
#reading file from directory
for filename in $filedir; do
#extracting modified file name from file
b=$(basename "$filename" ".csv" | awk -F "_" '{print $2}' | awk -F "-" '{print $1}')
#reading line by line from file
first='yes'
output_filename=$(basename "$filename")
while read -r line;do
#reading header from file
header=$(head -n1 "$filename")
if [[ $first == 'yes' ]]; then
#writing header with new column just once
printf '%s,%s\n' "CounterGroupName" "$header" > /home/vikrant_singh_rana/enrichment_files/"$output_filename"
first='no'
else
#writing modified filename to each record of a file
printf '%s,%s\n' "$b" "$line" >> /home/vikrant_singh_rana/enrichment_files/"$output_filename"
fi
done <"$filename"
done