如何重新排列 csv 文件的列顺序

问题描述 投票:0回答:1

我正在尝试重新排列以下示例数据的列顺序,

USI,CFG LEI,Counterparty LEI,UPI,Product Class,Execution venue,MTM,Currency 1,Currency 2,Notional amount 1,Notional amount 2,Exchange rate,Delivery type,Settlement or expiration date,Execution Venue LEI,Indication of collateralization,Trade ID,CounterParty full Name,Start Date,Deal Code,Settlement Currency,Valuation Fixing Date,Trade Date and timestamp,CIS Code (Internal),Trade Description,UTI,Is CFG Reporting Party?,Contract type,UTI Leg 2 ,Counterparty purchasing protection ,Counterparty selling protection ,Reference Entity ,Maturity Date ,Price ,Cap Strike ,Floor Strike ,Notional Amount ,Notional Currency ,Upfront Payment Amount ,Upfront Payment Currency ,Payment frequency of the reporting counterparty. ,Payment frequency of the non-reporting counterparty ,Day count convention ,Notional amount (leg 1) ,Notional currency (leg 1) ,Notional amount (leg 2) ,Notional currency (leg 2) ,Pay Leg Type (CFG) ,Receive Leg Type (CFG) ,Direction ,Option type ,Fixed rate ,Fixed rate 2 ,Fixed rate day count fraction ,Floating rate payment frequency ,Floating rate reset frequency ,Floating rate index name/rate period ,Floating rate Index 2 ,Buyer ,Seller ,Quantity unit ,Quantity frequency ,Total quantity ,Total Remaining Quantity ,Notional ,Settlement method ,Price unit ,Price currency ,Buyer pay index ,Buyer pay averaging method ,Seller pay index ,Seller pay averaging method ,Grade ,Option style ,Option premium ,Hours from through ,Hours from through time zone ,Days of week ,Load type

,DRMSV1Q0EKMEXLAU1P80,549300UGRJZFKLBDNB67,,FXF,OffFacility,-6573.66,EUR,USD,817785,923933.49,1.1298,Physical,4/13/2028,,Uncollateralized,2.02304E+12,AG NET LEASE RLTY FND IV Q LP,4/12/2023,B,,,12-Apr-23,AA077C7,FX FORWARD TRANSACTION,DRMSV1Q0EKMEXLAU1P80WSSFX202304120000436044681000001,Y,FX FORWARD TRANSACTION,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

,DRMSV1Q0EKMEXLAU1P80,549300UGRJZFKLBDNB67,,FXF,OffFacility,-6573.66,EUR,USD,817785,923933.49,1.1298,Physical,4/13/2028,,Uncollateralized,2.02304E+12,AG NET LEASE RLTY FND IV Q LP,4/12/2023,B,,,12-Apr-23,AA077C7,FX FORWARD TRANSACTION,DRMSV1Q0EKMEXLAU1P80WSSFX202304120000436044681000001,Y,FX FORWARD TRANSACTION,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

为了实现这一点,我正在尝试下面的 shell 脚本,

   #!/bin/bash
 
 #Reordering the  columns
    
     
    
    temp_file=$(mktemp)
    
     
    
    mlr --csv reorder -f "\"Trade ID\",\"Trade Date and timestamp\",\"CounterParty full Name\",\"CIS Code (Internal)\",\"Trade Description\",\"USI\",\"UTI\",\"UTI Leg 2\",\"CFG LEI\",\"Counterparty LEI\",\"Is CFG Reporting Party?\",\"UPI\",\"Contract type\",\"Execution venue\",\"Execution Venue LEI\",\"Counterparty purchasing protection\",\"Counterparty selling protection\",\"Reference Entity\",\"Start Date\",\"Maturity Date\",\"Price\",\"Cap Strike\",\"Floor Strike\",\"Notional Amount\",\"Notional Currency\",\"Upfront Payment Amount\",\"Upfront Payment Currency\",\"Payment frequency of the reporting counterparty.\",\"Payment frequency of the non-reporting counterparty\",\"Currency 1\",\"Currency 2\",\"Notional amount 1\",\"Notional amount 2\",\"Exchange rate\",\"Delivery type\",\"Settlement or expiration date\",\"Day count convention\",\"Notional amount (leg 1)\",\"Notional currency (leg 1)\",\"Notional amount (leg 2)\",\"Notional currency (leg 2)\",\"Pay Leg Type (CFG)\",\"Receive Leg Type (CFG)\",\"Direction\",\"Option type\",\"Fixed rate\",\"Fixed rate 2\",\"Fixed rate day count fraction\",\"Floating rate payment frequency\",\"Floating rate reset frequency\",\"Floating rate index name/rate period\",\"Floating rate Index 2\",\"Buyer\",\"Seller\",\"Quantity unit\",\"Quantity frequency\",\"Total quantity\",\"Total Remaining Quantity\",\"Notional\",\"Settlement method\",\"Price unit\",\"Price currency\",\"Buyer pay index\",\"Buyer pay averaging method\",\"Seller pay index\",\"Seller pay averaging method\",\"Grade\",\"Option style\",\"Option premium\",\"Hours from through\",\"Hours from through time zone\",\"Days of week\",\"Load type\",\"Indication of collateralization\",\"MTM\",\"Product Class\",\"Deal Code\",\"Settlement Currency\",\"Valuation Fixing Date\"" Wallstreet_Source_File.csv > "$temp_file"
    
     
    
    mv "$temp_file" Wallstreet_Source_File.csv

执行上述 shell 脚本时出现以下错误

mlr CSV header/data length mismatch 79 != 28 at filename Wallstreet_Source_File.csv row2

我正在尝试根据脚本(mlr)中提到的顺序对列重新排序,缺少任何内容或任何其他方法来实现它。

shell awk sed cd mlr
1个回答
0
投票

如果您被允许安装工具,我建议尝试使用 csvkit 中的

csvcut,它可用于对列重新排序,如下所示,让 
file.csv
内容为

Able,Baker,Charlie
1,2,3
4,5,6

然后

csvcut --columns "Able,Charlie,Baker" file.csv

提供输出

Able,Charlie,Baker
1,3,2
4,6,5
© www.soinside.com 2019 - 2024. All rights reserved.