2014-12-04 126 views
-2

我必须使用以下信息创建while循环。Unix while循环

Clive Cussler,Ghost Ship,9780399167317 
Clive Cussler,Bootlegger,9780399167294 
James Patterson,Invisible,9780316405348 
James Patterson,Gone,9781455515844 
James Rollins,Map of Bones,9780062017857 
Michael Connelly,The Lincoln Lawyer,9781455516346 
David Baldacci, The Escape,9781478984344 

该脚本必须通过作者,书名和isbn来区分它们。看起来像这样

Author    Name of Book     ISBN 
Clive Cussler   Ghost Ship      978-0399-16731-7 
Clive Cussler   Bootlegger      978-0399-16729-4 
James Patterson  Invisible      978-0316-40534-8 
James Patterson  Gone       978-1455-51584-4 
James Rollins   Map of Bones     978-0062-01785-7 
Michael Connelly  The Lincoln Lawyer    978-1455-51634-6 
David Baldacci  The Escape      978-1478-98434-4 

任何关于如何做到这一点的建议?

+3

首先要决定的是你要使用的语言。 Bash shell脚本? Perl的? C? C++?蟒蛇? AWK? SED? – 2014-12-04 16:18:03

+1

是的,你必须决定你想使用的语言... ;-) – Axel 2014-12-04 16:18:19

+1

然后尝试写它,如果你遇到问题,*然后*这是适当的发布你的问题在SO上。 – ajp15243 2014-12-04 16:19:12

回答

1

使用python:

f=open('file.txt') 
print("{:<30}{:<30}{:<30}".format("Author","Name Of Book","ISBN")) 
for x in f: 
    x = x.strip().split(',') 
    print("{:<30}{:<30}{:<30}".format(x[0],x[1],x[2])) 

用awk:

awk -F ',' 'BEGIN{printf("%-30s%-30s%-30s\n","Author","Name of Book","ISBN")}{printf("%-30s%-30s%-30s\n",$1,$2,$3)}' file.txt 

输出:

Author      Name Of Book     ISBN       
Clive Cussler     Ghost Ship     9780399167317     
Clive Cussler     Bootlegger     9780399167294     
James Patterson    Invisible      9780316405348     
James Patterson    Gone       9781455515844     
James Rollins     Map of Bones     9780062017857     
Michael Connelly    The Lincoln Lawyer   9781455516346     
David Baldacci     The Escape     9781478984344