![バッチを使用してテキスト ファイルからテキストまたはシーケンスを取得しますか?](https://rvso.com/image/1543203/%E3%83%90%E3%83%83%E3%83%81%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%97%E3%81%A6%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88%20%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%8B%E3%82%89%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88%E3%81%BE%E3%81%9F%E3%81%AF%E3%82%B7%E3%83%BC%E3%82%B1%E3%83%B3%E3%82%B9%E3%82%92%E5%8F%96%E5%BE%97%E3%81%97%E3%81%BE%E3%81%99%E3%81%8B%3F.png)
というファイルがありfile.txt
、その中には、
American 9876543 [email protected]
Australian 7674840 [email protected]
Indian 9364906 [email protected]
Chinese 6383936 [email protected]
Japanese 9363839 [email protected]
ここで、このファイルから 3 つの項目をフィルターして、別のテキスト ファイルに出力します。
例えば、
出力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
このファイルから3つの項目をフィルタリングして別のテキストファイルに出力したい
これを行う必要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の場合- 別のコマンドの結果に対してコマンドをループします。
- リダイレクション- リダイレクト演算子。
- タイプ- 1 つ以上のテキスト ファイルの内容を表示します。