我有一个包含目录列表的电子表格,并且需要为每个目录分配相关的可变数量的“帐户”(可以很容易地转换为csv),即:
directory # of accounts needed
/usr/src/Mon-Carlton/ 110
/usr/src/Mon-CoalMtn/ 50
/usr/src/Mon-Cumming/ 90
etc...
我还有一个'master_account_list.csv'文件,其中包含可分发到每个区域的所有可能帐户的完整列表,即:
account_1,password,type
account_2,password,type
account_3,password,type
etc...
我希望能够将master_account_list.csv拆分为每个唯一目录的单独accounts.csv文件,并列出所需的帐户数。
master_file经常使用新帐户进行更新,并且需要再次重新分发到所有目录。 (生成的accounts.csv文件与master_account_list具有相同的格式。)
在Linux中完成的最佳方法是什么?
编辑:当脚本完成后,如果master_account_list.csv中剩余的未分配帐户成为新的master_account_list.csv,那将是理想的。
答案 0 :(得分:0)
假设您已将帐户spreasheet转换为逗号(!)分隔的csv文件,没有标题(!),您可以使用以下awk
程序:
split_accounts.awk :
# True as long as we are reading the first file which
# is the converted spreadsheet
NR==FNR {
# Store the directories and counts in arrays
dir[NR]=$1
cnt[NR]=$2
next
}
# This block runs for every line of master_account_list.csv
{
# Get the directory and count from the arrays we've
# created above. 'i' will get initialized automatically
# with 0 on it's first usage
d=dir[i+1]
c=cnt[i+1]
# append the current line to the output file
f=d"account_list.csv"
print >> f
# 'n' holds the number of accounts placed into the current
# directory. Check if it has the reached the desired count
if(++n==c) {
# Step to the next folder / count
i++
# Reset the number of accounts placed
n=0
# Close the previous output file
close(f)
}
}
这样称呼:
awk -F, -f split_accounts.awk accounts.csv master_account_list.csv
注意:当前实现中的计数不允许0
。