2016-11-15 18 views
2

我試圖用記事本++發現有錯誤位數集和在它前面加零,以解決這個問題增添特色。發現一定數量的數字,在記事本+

所以,我需要找到比2位和低於6位數的人數較多。然後加零使其成爲6位數。

這必須在記事本++或批處理來完成,因爲沒有人計算背景還需要運行它。

我試着學習一些正則表達式,但不能想出一個好辦法,以填補高達6位數。

下面是一些例子,我需要自動完成:

1 1 21 10121 416138 
1 1 22 10122 574158 
1 1 23 1
1 1 24 10124 58157 
1 1 25 10125 5880 
1 1 26 10126 127144 

1 1 21 010121 416138 
1 1 22 010122 574158 
1 1 23 01
1 1 24 010124 058157 
1 1 25 010125 005880 
1 1 26 010126 127144 
+2

這是製表符分隔的文件還是固定的文件? – Squashman

+0

[Notepad ++ RegEx替換爲Zerofill]的可能重複(http://stackoverflow.com/questions/25624498/notepad-regex-replace-with-zerofill) – Toto

回答

0

不是一個非常好的辦法做到這一點,但工程對少量套數。我用你的例子中的2150行進行了測試,幾乎立即完成。雖然開始掙扎在100萬行左右。它的工作原理,但它需要時間:

使用搜索和替換在記事本++與搜索模式正則表達式搜索

\b([0-9]{3,5})\b

0\1

更換和命中Replace all 3倍。
因此,每3-5位數字將被其前面的0代替。當全部替換3次時,確保3位數字填充3個前導零。

同樣不是一個好方法,但對於我的工作......

編輯:剛纔看到它必須自動完成...也許記事本+ +有一些工具來執行搜索和替換爲腳本,但我會把它留給別人;)

又一編輯:我只是忍不住自己我不得不找出它...我用記事本++的宏函數來記錄上述S & R和擡頭,你可以找到你保存宏...

成果是這樣的:

<Macro Key="32" Shift="yes" Alt="yes" Ctrl="no" name="MyUpdater"> 
    <Action sParam="" lParam="0" wParam="0" message="1700" type="3"/> 
    <Action sParam="\b([0-9]{3,5})\b" lParam="0" wParam="0" message="1601" type="3"/> 
    <Action sParam="" lParam="2" wParam="0" message="1625" type="3"/> 
    <Action sParam="0\1" lParam="0" wParam="0" message="1602" type="3"/> 
    <Action sParam="" lParam="768" wParam="0" message="1702" type="3"/> 
    <Action sParam="" lParam="1609" wParam="0" message="1701" type="3"/> 
    <Action sParam="" lParam="0" wParam="0" message="1700" type="3"/> 
    <Action sParam="\b([0-9]{3,5})\b" lParam="0" wParam="0" message="1601" type="3"/> 
    <Action sParam="" lParam="2" wParam="0" message="1625" type="3"/> 
    <Action sParam="0\1" lParam="0" wParam="0" message="1602" type="3"/> 
    <Action sParam="" lParam="768" wParam="0" message="1702" type="3"/> 
    <Action sParam="" lParam="1609" wParam="0" message="1701" type="3"/> 
    <Action sParam="" lParam="0" wParam="0" message="1700" type="3"/> 
    <Action sParam="\b([0-9]{3,5})\b" lParam="0" wParam="0" message="1601" type="3"/> 
    <Action sParam="" lParam="2" wParam="0" message="1625" type="3"/> 
    <Action sParam="0\1" lParam="0" wParam="0" message="1602" type="3"/> 
    <Action sParam="" lParam="768" wParam="0" message="1702" type="3"/> 
    <Action sParam="" lParam="1609" wParam="0" message="1701" type="3"/> 
</Macro> 

Close Notepad ++。 轉到%appdata%\Notepad++\並打開文件shortcuts.xml與正常的文本編輯器(記事本+ +將覆蓋它回到正常狀態),並將上面的xml代碼放在那裏(應該已經有一個名爲「Trim Trailing and Save」的宏, ;。只要把它關閉</Macro>標記下,並將其保存在您打開Notepadd ++現在和菜單欄點擊宏應該有一個叫「MyUpdater」
目前該快捷方式設置爲Alt鍵 + + 空間,但你可以改變它點擊宏 - >編輯快捷方式。

這對我有用,就像這樣,你可以將makro「導出」給其他用戶。

要撤消此操作,您可以搜索\b([0])並將其替換爲空字符串。注意你必須再次檢查正則表達式!這裏只需打一次Replace就足夠了。

如果有什麼事隨時問:)

0
@ECHO OFF 
SETLOCAL ENABLEDELAYEDEXPANSION 
SET "sourcedir=U:\sourcedir" 
SET "destdir=U:\destdir" 
SET "filename1=%sourcedir%\q40601412.txt" 
SET "outfile=%destdir%\outfile.txt" 
(
FOR /f "usebackqtokens=1-5" %%a IN ("%filename1%") DO (
SET /a column4=1000000+%%d 
SET /a column5=1000000+%%e 
ECHO %%a %%b %%c !column4:~-6! !column5:~-6! 
) 
)>"%outfile%" 

GOTO :EOF 

你需要改變的sourcedirdestdir設置以適合你的情況。

我使用了一個名爲q40601412.txt的文件,其中包含我的測試數據。

可生產定義爲%OUTFILE%文件

讀取文件並tokenise作爲token1..token5至%%一個.. %%ë

添加百萬到每個最後2列這將每個產生一個數字1000000..1999999。

Echo五列了一遍,一個%% %% .. C作爲他們的立場,以及每個column5column6

的間距是給你的最後6個字符。由於字段都是固定長度的(1,1,2,6,6),因此請使用echo行中所需的空格或製表符。

如果欄3可以是1位或2位,則需要

SET /a column3=1000000+%%c 

,和代替%%c回聲

相關問題