我有一个文件,ttt.txt。内容/格式如下:
AAA BBB
AAA CCC
_
CCC DDD
我需要:
这是我目前的剧本:
:: ===== Here to underscore lines
type ttt.txt | findstr /v "^_" >Final.txt
@echo off
setlocal enabledelayedexpansion
for /F "tokens=*" %%A in (ttt.txt) do (
set line=%%A
echo(!line:~1!>>myFinal.txt
)
答案 0 :(得分:2)
处理文本文件时,FOR
处理将自动删除前导空格。所以你唯一要做的就是检查线值是否等于下划线,如果是,则忽略它。
@ECHO OFF
SETLOCAL
FOR /F "tokens=*" %%A IN (ttt.txt) DO (
IF NOT "%%A"=="_" ECHO %%A>>myFinal.txt
)
ENDLOCAL
编辑:根据您更改的要求,以下内容将验证长度是否为3或更长。
@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /F "tokens=*" %%A IN (ttt.txt) DO (
SET Line=%%A
REM Verify it is not an underscore.
IF NOT "!Line!"=="_" (
REM Check that the length is 3 chars or longer.
SET Char=!Line:~2,1!
IF NOT "!Char!"=="" (
REM All conditions satisfied.
ECHO %%A>>myFinal.txt
)
)
)
ENDLOCAL
答案 1 :(得分:0)
@echo off
FOR /F "tokens=* delims= " %%G IN (ttt.txt) DO ECHO %%G|findstr /R /V /C:"^_$">>final.txt
编辑: 由于您现在要求该行长度至少为3个字符,因此在开头只有下划线的行将成为静音点。所以你需要的就是这个。
@echo off
FOR /F "tokens=* delims= " %%G IN (ttt.txt) DO >>Final.txt (ECHO %%G|FINDSTR /R /C:"^...")