使用批处理文件删除列. csv中的单词



我有一个批处理文件在同一个文件夹。csv传入文件夹。

传入的.CSV文件有一个列| GPSPosition |

输出数据看起来像| 21 deg 14' 4.621" S, 159 deg 46' 45.358" W |,是的,它是一列中的一个大短语。

我在确定如何处理这个问题时遇到了麻烦,我想写一个。bat文件来删除单词"deg"从.csv文件中列的每一行中的值。

当前结果

| GPSPosition |
| 21 deg 14' 4.621" S, 159 deg 46' 45.358" W |
| 21 deg 14' 4.621" S, 159 deg 47' 45.358" W |
| 21 deg 14' 4.621" S, 159 deg 48' 45.358" W |

预期的结果

| GPSPosition |
| 21  14' 4.621" S, 159  46' 45.358" W |
| 21  14' 4.621" S, 159  47' 45.358" W |
| 21  14' 4.621" S, 159  48' 45.358" W |

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:your files"
SET "destdir=u:your results"
SET "filename1=%sourcedir%q65987390.txt"
SET "outfile=%destdir%outfile.txt"
SET "indelims=|,"
SET "outdelim=^|"
(
FOR /f "usebackq tokens=1,2delims=%indelims%" %%b IN ("%filename1%") DO (
SET "col1=%%b"
SET "col2=%%c"
IF DEFINED col2 (
SET "col1=!col1:deg =!"
SET "col2=!col2:deg =!"
ECHO %outdelim%!col1:~1!%outdelim%!col2:~1,-1!%outdelim%
) ELSE (
rem - no col2, so header
ECHO %outdelim%!col1:~1,-1!%outdelim%Column2header string%outdelim%
)
)
)>"%outfile%"
GOTO :EOF

从提示符中查看set /?,破译:m,n语法。注意,输出时管道需要转义(使用插入符号),否则cmd将其视为重定向。

很简单:取每一行,将deg替换为" none "并输出结果字符串:

@echo off
setlocal enabledelayedexpansion
(for /f "usebackq delims=" %%a in ("input.csv") do (
set "line=%%a"
set "line=!line:deg =!"
REM set "line=!line:,=|!"
echo !line!
))>output.csv

set命令中使用引号可以避免管道符号的错误,延迟展开可以使用echo命令来处理管道。

(必要时,也用管道符号替换逗号)。根据您的示例输出,这是不需要的。但是我插入了代码;删除REM(如果你想使用它)

相关内容

  • 没有找到相关文章

最新更新