Shell 循环并合并两个文本文件的脚本
我有两个.csv文件,我正试图通过脚本“倍增”出来。第一个文件是个人信息,基本上如下所示:Shell 循环并合并两个文本文件的脚本,shell,Shell,我有两个.csv文件,我正试图通过脚本“倍增”出来。第一个文件是个人信息,基本上如下所示: First Name, Last Name, Email, Phone Sally,Davis,sdavis@nobody.com,555-555-5555 Tom,Smith,tsmith@nobody.com,555-555-1212 AccountID 1001 1002 First Name, Last Name, Email, Phone, AccountID Sally,Davis,sda
First Name, Last Name, Email, Phone
Sally,Davis,sdavis@nobody.com,555-555-5555
Tom,Smith,tsmith@nobody.com,555-555-1212
AccountID
1001
1002
First Name, Last Name, Email, Phone, AccountID
Sally,Davis,sdavis@nobody.com,555-555-5555, 1001
Tom,Smith,tsmith@nobody.com,555-555-1212, 1001
Sally,Davis,sdavis@nobody.com,555-555-5555, 1002
Tom,Smith,tsmith@nobody.com,555-555-1212, 1002
第二个文件是帐号,如下所示:
First Name, Last Name, Email, Phone
Sally,Davis,sdavis@nobody.com,555-555-5555
Tom,Smith,tsmith@nobody.com,555-555-1212
AccountID
1001
1002
First Name, Last Name, Email, Phone, AccountID
Sally,Davis,sdavis@nobody.com,555-555-5555, 1001
Tom,Smith,tsmith@nobody.com,555-555-1212, 1001
Sally,Davis,sdavis@nobody.com,555-555-5555, 1002
Tom,Smith,tsmith@nobody.com,555-555-1212, 1002
基本上,我想用每个帐户Id获取每个名称。因此,如果我在第一个文件中有10个名称,在第二个文件中有10个帐户Id,我应该在生成的文件中有100行,并使其如下所示:
First Name, Last Name, Email, Phone
Sally,Davis,sdavis@nobody.com,555-555-5555
Tom,Smith,tsmith@nobody.com,555-555-1212
AccountID
1001
1002
First Name, Last Name, Email, Phone, AccountID
Sally,Davis,sdavis@nobody.com,555-555-5555, 1001
Tom,Smith,tsmith@nobody.com,555-555-1212, 1001
Sally,Davis,sdavis@nobody.com,555-555-5555, 1002
Tom,Smith,tsmith@nobody.com,555-555-1212, 1002
任何帮助都将不胜感激您只需为每个值编写一个for循环,通过其id计数重复,并附加描述,但顺序正好相反。
这是否有效?或者您是否尝试过这一点?如果python适合您,下面是一个脚本:
def main():
f1 = open("accounts.txt", "r")
f1_total_lines = sum(1 for line in open('accounts.txt'))
f2_total_lines = sum(1 for line in open('info.txt'))
f1_line_counter = 1;
f2_line_counter = 1;
f3 = open("result.txt", "w")
f3.write('First Name, Last Name, Email, Phone, AccountID\n')
for line_account in f1.readlines():
f2 = open("info.txt", "r")
for line_info in f2.readlines():
parsed_line_account = line_account
parsed_line_info = line_info.rstrip() # we have to trim the newline character from every line from the 'info' file
if f2_line_counter == f2_total_lines: # ...for every but the last line in the file (because it doesn't have a newline character)
parsed_line_info = line_info
f3.write(parsed_line_info + ',' + parsed_line_account)
if f1_line_counter == f1_total_lines:
f3.write('\n')
f2_line_counter = f2_line_counter + 1
f1_line_counter = f1_line_counter + 1
f2_line_counter = 1 # reset the line counter to the first line
f1.close()
f2.close()
f3.close()
if __name__ == '__main__':
main()
我使用的文件如下:
info.txt
:
Sally,Davis,sdavis@nobody.com,555-555-555
Tom,Smith,tsmith@nobody.com,555-555-1212
John,Doe,jdoe@nobody.com,555-555-3333
1001
1002
1003
accounts.txt
:
Sally,Davis,sdavis@nobody.com,555-555-555
Tom,Smith,tsmith@nobody.com,555-555-1212
John,Doe,jdoe@nobody.com,555-555-3333
1001
1002
1003
如果您打算复制帐户ID 如果您打算将每个
帐户\u ID
添加到信息文件中的每个记录中,则可以使用简短的awk
解决方案,例如
$ awk -F, '
FNR==NR{a[i++]=$0}
FNR!=NR{b[j++]=$0}
END{print a[0] ", " b[0]
for (k=1; k<i; k++)
for (m=1; m<i; m++)
print a[m] ", " b[k]}
' info id
First Name, Last Name, Email, Phone, AccountID
Sally,Davis,sdavis@nobody.com,555-555-5555, 1001
Tom,Smith,tsmith@nobody.com,555-555-1212, 1001
Sally,Davis,sdavis@nobody.com,555-555-5555, 1002
Tom,Smith,tsmith@nobody.com,555-555-1212, 1002
(注意:该-d,
选项仅将分隔符设置为逗号)
尝试重新发明轮子似乎要容易得多。使用阵列可以轻松完成
OLD=$IFS; IFS=$'\n'
ar1=( $(cat file1) )
ar2=( $(cat file2) )
IFS=$OLD
ind=${!ar1[@]}
for i in $ind; { echo "${ar1[$i]}, ${ar2[$i]}"; }
是否确实要为每条记录复制
帐户ID
?这在一组数据中似乎是唯一的?如果您不打算复制帐户\u ID
请告诉我,我将取消删除我的答案。