我想用逗号替换回车符 [英] I want to replace carriage returns with commas in batch

查看:119
本文介绍了我想用逗号替换回车符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为test.txt的文本文件,其中有一些名为PROP-VALUES的部分。使用批处理,我想用逗号替换每一个回车,但只能在PROP-VALUES部分。附件是原始文件test.txt和固定文件test.txt

original

修正了

解决方案

当我看到像这样的问题代码,甚至没有几个示例文本行!)我认为我可以写任何我想要的代码,所以我无法抗拒诱惑尝试一个疯狂的方法!

 $ set $ set $ b $ set $ set $ set $ set $ set $ set $ set $ set $ set $ set $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $% b $ b设置EOL =!LF! &安培;设置EOL2 =!LF! 

for / F %% a in(test.txt)do(
if %% a equ PROP-SUMMARY setEOL =!LF!
set / P = %% a!EOL!< NUL
setEOL0 =!EOL!& setEOL =!EOL2!& setEOL2 =!EOL0!
if% a equ PROP-VALUES setEOL =,

这是 test.txt

  4.12 
0
1
1
21
物业估价
625
4.12
945
3.64
地产概算
400
400
厚度

这是输出:

 4.12 
0
1
1
21
物业价值
625,4.12
945 ,3.64
PROP-SUMMARY
400
400
厚度


I have a text file called test.txt that has sections called PROP-VALUES. using batch, I want to replace every other carriage return with commas, but only in the PROP-VALUES sections. Attached are the original file test.txt and the fixed file test fixed.txt

original

fixed

解决方案

When I see "questions" like this one (with no good specifications nor code, not even a couple example text lines!) I consider that I can write whatever code I want, so I couldn't resist the temptation to try a crazy method!

@echo off
setlocal EnableDelayedExpansion

set LF=^
%Don't remove%
%these lines%
set "EOL=!LF!" & set "EOL2=!LF!"

for /F %%a in (test.txt) do (
   if %%a equ PROP-SUMMARY set "EOL=!LF!"
   set /P "=%%a!EOL!" < NUL
   set "EOL0=!EOL!" & set "EOL=!EOL2!" & set "EOL2=!EOL0!"
   if %%a equ PROP-VALUES set "EOL=,"
)

This is test.txt:

4.12
0
1
1
21
PROP-VALUES
625
4.12
945
3.64
PROP-SUMMARY
400
400
Thickness

This is the output:

4.12
0
1
1
21
PROP-VALUES
625,4.12
945,3.64
PROP-SUMMARY
400
400
Thickness

这篇关于我想用逗号替换回车符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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