修剪多余的管道关闭Windows中的文本文件结束 [英] Trim extra pipes off end of text file in Windows
问题描述
所以基本上我有一个输出被管道分隔文本文件,看起来像这样一个过程:
| ABC123 | 1 * | 004 | ** gobbligook | 001 |%| 2014年1月1日|||||||||||||
这只是一个例子,我不知道,如果答案包括定期EX pressions。如果是的话我会把实际线路。不管怎么说
问题
因此,对于这个例子,接受该文件是寻找8的管道,但也有20,如果它看到任何更多的管道8之后它在寻找导入过程失败导入过程。
问
有没有我可以在Windows环境下使用修剪尾随管道关闭本月底整个文件?
进程更新
脱线提供给我,我的工作有很大的答案,但我不断收到此错误:分隔符是在这个时候意外
下面是code:
@ECHO OFF
SETLOCAL
SETsourcedir = C:\\用户\\桌面\\竖线分隔符工程
SETDESTDIR = C:\\用户\\桌面\\竖线分隔符工程
(
FOR / F令牌= 1-7delims = | %%一个IN('键入%sourcedir%\\ test.txt的')DO(
ECHO(^ | %%一个^ | %% b ^ | %% C ^ | %%Ð^ | %% E ^ | %%˚F^ | %% G ^ |
)
)>%DESTDIR%\\ newfile.txt
任何人都知道什么是错?我也只是把该行从问题| ABC123 | .. |粘贴在文件中像6次...谢谢!
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SETsourcedir =。
SETDESTDIR = U:\\ DESTDIR
(
FOR / Fdelims =%%一个IN('键入%sourcedir%\\ q22863616.txt')DO(
SET行= %%一个
!ECHO(行:〜0,-12!
)
)>%DESTDIR%\\ newfile.txtGOTO:EOF
我用命名包含您的数据,我的测试文件 q22863616.txt
。
生产newfile.txt
假设最后的12场都是空的,因为缺乏其它信息。
另一种形式,给出更多的信息。
@ECHO OFF
SETLOCAL
SETsourcedir =。
SETDESTDIR = U:\\ DESTDIR
(
FOR / F令牌= 1-7delims = | %%一个IN('键入%sourcedir%\\ q22863616.txt')DO(
ECHO(^ | %%一个^ | %% b ^ | %% C ^ | %%Ð^ | %% E ^ | %%˚F^ | %% G ^ |
)
)>%DESTDIR%\\ newfile.txt
确定 - 第三次是一个魅力
@ECHO OFF
SETLOCAL enabledelayedexpansion
SETsourcedir =。
SETDESTDIR = U:\\ DESTDIR
::除去起始$变量
FOR / Fdelims ==%% a。在('设置$ 2 ^>讷')DO SET一%% =
(
FOR / Fdelims =%%一个IN('键入%sourcedir%\\ q22863616.txt')DO(
SET$ 0 = %%一个
SET$ 1 = %%一个
FOR / L %% C IN(1,1,8)DO SET$ 1 = $ 1:!* | =
SET$ 2 = %%一个
SET$ 3 =
SET / TOT一= 0
FOR / Fdelims =%% E在('设置$ ^ | FINDSTR / O / R$')DO SET / TOT一= %%ë - TOT! - 5
CALL:秀地合计!
CALL ECHO %% $ 2:!〜0, - 地合计%%
)
)>%DESTDIR%\\ newfile.txtGOTO:EOF:显示
呼叫建立$ 3 = %% $ 2:0〜 - %1 %%
FOR / F令牌= 1 * delims ==%%的Y('设置$ 3')DO ECHO(%%ž
GOTO:EOF
这似乎不受%
中的数据,但扼流圈!
或&放;
。支付你的钱,你需要你的选择...
So basically I have a process that outputs a text file that is pipe delimited, looks something like this:
|abc123|1*|004|**gobbligook|001|%|2014-01-01|||||||||||||
This is just an example, and I'm not sure if the answer involves regular expressions. If it does i will put the actual line. Anyways
ISSUE
So for this example the import process that accepts this file is looking for 8 pipes, but there are 20, if it sees any more pipes after the 8 it's looking for the import process fails.
Question
Is there a process that I can use in a Windows environment to trim the trailing pipes off the end of this for the entire file?
UPDATE
Magoo supplied me with a great answer that I am working but I keep getting this error: Delimiter was unexpected at this time
Here is code:
@ECHO OFF
SETLOCAL
SET "sourcedir=C:\Users\Desktop\Pipe Delimiter Project"
SET "destdir=C:\Users\Desktop\Pipe Delimiter Project"
(
FOR /f "tokens=1-7delims=|" %%a IN ('TYPE "%sourcedir%\test.txt"') DO (
ECHO(^|%%a^|%%b^|%%c^|%%d^|%%e^|%%f^|%%g^|
)
)>%destdir%\newfile.txt
Anyone know what's wrong? I also just put in the line from the question |abc123|..| pasted in the file like 6 times...thanks!
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=."
SET "destdir=U:\destdir"
(
FOR /f "delims=" %%a IN ('TYPE "%sourcedir%\q22863616.txt"') DO (
SET "line=%%a"
ECHO(!line:~0,-12!
)
)>%destdir%\newfile.txt
GOTO :EOF
I used a file named q22863616.txt
containing your data for my testing.
Produces newfile.txt
Assuming that the final 12 fields are all empty, for lack of information otherwise.
Another form, given additional information
@ECHO OFF
SETLOCAL
SET "sourcedir=."
SET "destdir=U:\destdir"
(
FOR /f "tokens=1-7delims=|" %%a IN ('TYPE "%sourcedir%\q22863616.txt"') DO (
ECHO(^|%%a^|%%b^|%%c^|%%d^|%%e^|%%f^|%%g^|
)
)>%destdir%\newfile.txt
OK - third time's a charm.
@ECHO OFF
SETLOCAL enabledelayedexpansion
SET "sourcedir=."
SET "destdir=U:\destdir"
:: remove variables starting $
FOR /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="
(
FOR /f "delims=" %%a IN ('TYPE "%sourcedir%\q22863616.txt"') DO (
SET "$0=%%a"
SET "$1=%%a"
FOR /l %%c IN (1,1,8) DO SET "$1=!$1:*|=!"
SET "$2=%%a"
SET "$3="
SET /a tot=0
FOR /f "delims=:" %%e IN ('set $^|findstr /o /r "$"') DO SET /a tot=%%e - !tot! - 5
CALL :show !tot!
CALL ECHO %%$2:~0,-!tot!%%
)
)>%destdir%\newfile.txt
GOTO :EOF
:show
CALL SET "$3=%%$2:~0,-%1%%"
FOR /f "tokens=1*delims==" %%y IN ('set $3') DO ECHO(%%z
GOTO :eof
This seems immune to %
in the data, but chokes on !
or &
. You pays your money, you takes your choice...
这篇关于修剪多余的管道关闭Windows中的文本文件结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!