本文介绍了在Window Batch中-如何从文件中删除前导空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件ttt.txt.内容/格式是这样的:

I have a file, ttt.txt. The content/format is like this:

  AAA BBB
  AAA CCC
  _
  CCC DDD
  1. 每行有2个前导空格;
  2. 有些行只有1个店下招牌>

我需要:

  1. 删除只有1个商店下标(_)的空行;
  2. 我需要为每行修剪领导空间.

这是我当前的脚本:

:: ===== 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
)

推荐答案

在处理文本文件时,FOR处理将自动删除前导空格.因此,您唯一要做的就是简单地检查行值是否等于下划线,如果是,请忽略它.

When processing text files, the FOR processing will automatically remove leading spaces. So the only thing you really have to do is simply check if the line value is equal to an underscore and, if so, ignore it.

@ECHO OFF
SETLOCAL

FOR /F "tokens=*" %%A IN (ttt.txt) DO (
    IF NOT "%%A"=="_" ECHO %%A>>myFinal.txt
)

ENDLOCAL


根据您更改的要求,以下内容还将验证该长度是否为3或更长.


Per your changed requirements, the following would verify the length is 3 or longer as well.

@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

这篇关于在Window Batch中-如何从文件中删除前导空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 13:42