为什么 ECHO 命令会在文件中打印一些额外的尾随空格? [英] Why does ECHO command print some extra trailing space into the file?

查看:41
本文介绍了为什么 ECHO 命令会在文件中打印一些额外的尾随空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从一个数据文件中读取多行并将其附加到另一个文件中.问题是附加行末尾的输出文件中有一些额外的空间.根据我的要求修剪尾随空格是强制性的,因为在原始数据文件中,行尾没有空格.我尝试了所有可能的事情,但我仍然无法实现这一点.

I am reading multiple lines from a data file and append it to another file. The issue is that some extra space is in output file at end of appended lines. It is mandatory to trim the trailing space according to my requirement because in original data file there is no space at end of the lines. I tried all possible things, but I still cannot achieve this one.

@echo off
setlocal enabledelayedexpansion
FOR /F "delims=" %%c IN (C:Users184863Desktopmydata.txt) DO ( 
echo %%c>>temp_data.txt 
)
endlocal

我尝试过的选项:

  1. echo %%c>>temp_data.txt ... 得到 1 个空格的行尾

  1. echo %%c>>temp_data.txt ... getting 1 space end of line

设置行=%%c
echo !lines!>>temp_data.txt ... 得到 1 个空格的行尾

set lines=%%c
echo !lines!>>temp_data.txt ... getting 1 space end of line

echo !lines:~0,-1!>>temp_data.txt ... 行的最后一个字符被修剪

echo !lines:~0,-1!>>temp_data.txt ... last char of the line trimmed

set lines=!lines: =!
echo !lines!>>temp_data.txt ... 行内的所有空格也被修剪(只需要修剪行尾,原始数据文件中不存在)

set lines=!lines: =!
echo !lines!>>temp_data.txt ... all spaces inside the line are also trimmed (need to trim only end of the line, which is not present in original data file)

推荐答案

您在 echo %%c>>temp_data.txt 行上有一个终端空格,应该删除.这是终端空格引起的典型混淆.

You have a terminal space on the line echo %%c>>temp_data.txt (amongst others) which should be deleted. This is typical of the confusion that terminal spaces causes.

这篇关于为什么 ECHO 命令会在文件中打印一些额外的尾随空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆