2016-11-29 102 views
-1

我有在多行下列數據的txt文件:如何更改日期格式列文本文件中的Windows

10/01/2015,125.99,129.00,125.00,128.97,1615600 
10/02/2015,129.92,129.92,127.01,128.81,509800 
10/05/2015,129.50,130.00,125.50,126.37,876500 
10/06/2015,128.00,131.70,128.00,130.67,1043000 
10/07/2015,134.00,135.45,132.60,134.49,1181500 
10/08/2015,133.90,133.90,131.60,132.25,736700 
10/09/2015,135.01,138.50,135.00,137.80,1493200 
10/12/2015,139.90,142.80,138.12,141.85,1879800 
10/13/2015,138.80,139.50,137.75,137.99,1103600 
10/14/2015,138.34,138.79,137.11,137.92,505200 
10/15/2015,138.00,138.80,134.50,135.21,481600 
10/16/2015,135.10,140.31,133.51,138.62,2431700 
10/19/2015,138.51,139.89,137.50,137.94,521700 

我想要的日期格式在使用.bat script所有行改爲YYYY/MM/DD

我曾嘗試以下:

@echo off&setlocal 
for /f "tokens=1,2,3,* delims=,/ " %%i in ('type "kam3.txt"') do (
echo %%k/%%i/%%j,%%l 
)>"newfile.txt" 

不過,我只看到了最新的界線正在發生變化,數據的其餘部分沒有顯示。

回答

0

以防萬一,如果你是開了PowerShell腳本,你可以試試這個:

Get-Content data.txt | Foreach { $_ -replace "^(\d\d)/(\d\d)/(\d\d\d\d),(.*)",'$3/$1/$2,$4' } 

結果:

2015/10/01,125.99,129.00,125.00,128.97,1615600 
2015/10/02,129.92,129.92,127.01,128.81,509800 
2015/10/05,129.50,130.00,125.50,126.37,876500 
2015/10/06,128.00,131.70,128.00,130.67,1043000 
2015/10/07,134.00,135.45,132.60,134.49,1181500 
2015/10/08,133.90,133.90,131.60,132.25,736700 
2015/10/09,135.01,138.50,135.00,137.80,1493200 
2015/10/12,139.90,142.80,138.12,141.85,1879800 
2015/10/13,138.80,139.50,137.75,137.99,1103600 
2015/10/14,138.34,138.79,137.11,137.92,505200 
2015/10/15,138.00,138.80,134.50,135.21,481600 
2015/10/16,135.10,140.31,133.51,138.62,2431700 
2015/10/19,138.51,139.89,137.50,137.94,521700 

您需要在PowerShell中運行這個(在命令鍵入powershell提示,例如)。 Get-Conetent獲取輸入文件的內容。 Foreach命令遍歷每一行。命令replace與正則表達式匹配,並將行替換爲匹配的變量(但順序不同)。如果不清楚,請隨時要求澄清。


如果您想要不同的分隔符,請使用此命令而不是上述命令。它不查找特定的分隔符(如逗號或分號)。

Get-Content data.txt | Foreach { $_ -replace "^(\d\d)/(\d\d)/(\d\d\d\d)(.*)",'$3/$1/$2$4' } 
+0

謝謝你的工作。 – Zam

+0

對不起,但是當我在具有以下數據的其他文件上嘗試相同的代碼時:10/01/2015; 125.99; 129.00; 125.00; 128.97; 1615600 10/02/2015; 129.92; 129.92; 127.01; 128.81; 509800 10/05/2015; 129.50; 130.00; 125.50; 126.37; 876500 10/06/2015; 128.00; 131.70; 128.00; 130.67; 1043000 10/07/2015; 134.00; 135.45; 132.60; 134.49; 1181500 10/08/2015; 133.90; 133.90; 131.60; 132.25; 736700 10/09/2015; 135.01; 138.50; 135.00; 137.80; 1493200 它不起作用 – Zam

+0

您在此數據中有一個新的分隔符(分號代替逗號)。使用上面張貼的新命令。 – blackpen

相關問題