2014-12-04 117 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