本帖最后由 yhcfsr 于 2018-6-9 14:47 编辑
哈哈,我发两个脚本你看看。注意备份源文件,POWERSHELL的修改是直接覆盖源文件的。
输出的文件用EXCEL打开,可以直观查看6列数据。
POWERSHELL强大的正则,不需要截取去尾这些操作。
- $sf='D:\test\data.txt';
- $content=gc -LiteralPath $sf;
- $output=$null;
- foreach($line in $content){
-
- $output+=($line -replace '(\d+)\s*.*','$1')+"`t";
- $output+=($line -replace '.*(\(USA\).*NA).*','$1')+"`t";
- $output+=($line -replace '.*NA\s*(\d+)\s*.*','$1')+"`t";
- $output+=($line -replace '.*NA\s*\d+\s*(\d+)\s*.*','$1')+"`t";
- $output+=($line -replace '.*NA\s*\d+\s*\d+\s*(\d+)\s*.*','$1')+"`t";
- $output+=($line -replace '.*\s*(\d+)$','$1')+"`r`n";
- }
- $output>$sf;
复制代码 BAT
字符串截取的数值设置,需要根据源数据具体情况,自行调试。- @echo off&setlocal enabledelayedexpansion
- (for /f "delims=" %%a in ('type data.txt') do (
- set "str=%%a"
-
- set "colum1=!str:~,12!"
- call:TrimEnd "!colum1!" colum1 " "
-
- set "colum2=!str:~12,24!"
- call:TrimEnd "!colum2!" colum2 " "
-
- set "colum3=!str:~36,3!"
- call:TrimEnd "!colum3!" colum3 " "
-
- set "colum4=!str:~39,11!"
- call:TrimEnd "!colum4!" colum4 " "
-
- set "colum5=!str:~50,2!"
- call:TrimEnd "!colum5!" colum5 " "
-
- set "colum6=!str:~52!"
- call:TrimEnd "!colum6!" str " "
- call echo;!colum1! !colum2! !colum3! !colum4! !colum5! !colum6!
- ))>out.txt
- endlocal&pause&exit
-
- :TrimEnd (源字符串,输出字符串,要删除的尾字符)去尾
- setlocal
- if "%~1" neq "" (set "var=%~1") else endlocal&goto:eof
- :lp
- if "%var:~-1%" equ "%~3" set "var=%var:~,-1%"&goto:lp
- endlocal&set "%2=%var%"&goto:eof
复制代码
|