![使用批次從文字檔案中提取文字或序列?](https://rvso.com/image/1543203/%E4%BD%BF%E7%94%A8%E6%89%B9%E6%AC%A1%E5%BE%9E%E6%96%87%E5%AD%97%E6%AA%94%E6%A1%88%E4%B8%AD%E6%8F%90%E5%8F%96%E6%96%87%E5%AD%97%E6%88%96%E5%BA%8F%E5%88%97%EF%BC%9F.png)
我的檔案名稱為file.txt
其中包含,
American 9876543 [email protected]
Australian 7674840 [email protected]
Indian 9364906 [email protected]
Chinese 6383936 [email protected]
Japanese 9363839 [email protected]
現在我想從此文件中過濾三件事作為另一個文字檔案中的輸出。
例如,
輸出file1.txt
- 需要包含所有郵件 ID
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
輸出file2.txt
- 需要包含所有數字
9876543
7674840
9364906
6383936
9363839
輸出file3.txt
- 只需要包含從 A 開始的序列詞,
例如,
American
Australian
為此,我嘗試使用 FC 命令和 Findstr 命令。
請幫忙..提前謝謝..
答案1
我想從此文件中過濾三件事作為另一個文本文件中的輸出
你不需要fc
或findstr
不需要這樣做。一個簡單的for /f
命令就足夠了。
使用以下批次檔 (test.cmd):
@echo off
setlocal enabledelayedexpansion
for /f "usebackq tokens=1-3" %%a in (`type file.txt`) do (
echo %%a >> file3.txt
echo %%b >> file2.txt
echo %%c >> file1.txt
)
endlocal
用法範例:
> type file.txt
American 9876543 [email protected]
Australian 7674840 [email protected]
Indian 9364906 [email protected]
Chinese 6383936 [email protected]
Japanese 9363839 [email protected]
> test
> type file1.txt
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
> type file2.txt
9876543
7674840
9364906
6383936
9363839
> type file3.txt
American
Australian
Indian
Chinese
Japanese
進一步閱讀
- Windows CMD 命令列的 AZ 索引
- Windows CMD 指令的分類列表
- 對於 /f- 根據另一個指令的結果循環指令。
- 重定向- 重定向運算子。
- 類型- 顯示一個或多個文字檔案的內容。