简体   繁体   English

如何使用命令提示符或批处理文件从字符串中删除某些字符?

[英]How to remove certain characters from a string using command prompt or batch file?

My name is Syarz and I'm new to programming.我的名字是 Syarz,我是编程新手。 I want to know how to remove characters from a string using cmd or batch file.我想知道如何使用 cmd 或批处理文件从字符串中删除字符。

This is the string in data.txt file:这是 data.txt 文件中的字符串:

test.rar:$rar5$16$b19108e5cf6a5dadaa0ff515f59c6df7$15$f7f4933a2a8f7aa43d7fc31e86c47e2b$8$307ef2987e735bec

I want this after running command or batch file in data.txt在 data.txt 中运行命令或批处理文件后我想要这个

$rar5$16$b19108e5cf6a5dadaa0ff515f59c6df7$15$f7f4933a2a8f7aa43d7fc31e86c47e2b$8$307ef2987e735bec

I have tried a few methods available on the website but none of them seemed to work for me.我尝试了网站上提供的几种方法,但似乎没有一种对我有用。 Either they delete entire string or delete the portion from where I don't want to.他们要么删除整个字符串,要么删除我不想删除的部分。

What I have already tried我已经尝试过的

1 - 1 -

more +1 "data.txt" > "data_NEW.txt"

2 - 2 -

@echo off
(
    for /F usebackq^ skip^=1^ delims^=^ eol^= %%L in ("data.txt") do echo(%%L
) > "data_NEW.txt"

3- 3-

@echo off
(
    for /F "skip=1 delims=" %%L in ('findstr /N "^" "data.txt"') do (
        set "LINE=%%L"
        setlocal EnableDelayedExpansion
        echo(!LINE:*:=!
        endlocal
    )
) > "data_NEW.txt"

4 - 4 -

@echo off
for /F %%C in ('find /C /V "" ^< "data.txt"') do set "COUNT=%%C"
setlocal EnableDelayedExpansion
(
    for /L %%I in (1,1,%COUNT%) do (
        set "LINE=" & set /P LINE=""
        if %%I gtr 1 echo(!LINE!
    )
) < "data.txt" > "data_NEW.txt"
endlocal

5 - 5 -

void chopN(char *str, size_t n)
{
    assert(n != 0 && str != 0);
    size_t len = strlen(str);
    if (n > len)
        return;  // Or: n = len;
    memmove(str, str+n, len - n + 1);
}

6 - 6 -

void chopN(char *str, size_t n) {
  char *dest = str;

  // find beginning watching out for rump `str`
  while (*str && n--) {
    str++;
  }

  // Copy byte by byte
  while (*src) {
    *dest++ = *src++;
  }

  *dest = '\0';
}

7 - 7 -

for /f %%a in (input.txt) do set "line=%%a"
set "line=%line:pub:04:=%"
set "line=%line::=%"
echo %line%

8 - 8 -

@echo off
setlocal enabledelayedexpansion
set "first=true"
(for /f "eol=p delims=" %%a in (input.txt) do (
  set "line=%%a"
  if defined first (set "line=!line:~2!" & set "first=")
  <nul set /p ".=!line::=!"
))>output.txt

Just trying to learn something new.只是想学习一些新的东西。 Thanks, everyone.谢谢大家。

I recognize that you asked for batch but I haven't been providing a Batch answer so I'll the folks who were helping you with batch answer back.我知道您要求批处理,但我没有提供批处理答案,所以我会帮助您进行批处理的人回复。

Here's an easy way to do it PowerShell:这是 PowerShell 的一种简单方法:

$f = Get-Content data.txt
$out = 'data2.txt'

$f | ForEach-Object { 
    $_.Split(':')[1] 
} | Out-File $out -Append

Well, many of your attempts – # 1 to # 4 – seem to remove the first line of the input text.好吧,您的许多尝试——#1 到#4——似乎都删除了输入文本的第一行。 # 7 and # 8 might come closer to what you want, but these approaches do not suit your sample data. # 7 和 # 8 可能更接近您想要的,但这些方法不适合您的样本数据。 And # 5 and # 6 are definitely not batch files.而#5和#6绝对不是批处理文件。

To split text at a certain character, namely the colon in your situation, simply use a for /F loop :要在某个字符处拆分文本,即您的情况下的冒号,只需使用for /F循环

for /F "tokens=1,2 delims=:" %%I in (data.txt) do (
    echo 1st token: %%I
    echo 2nd token: %%J
)

In case the second part could contain colons on its own you want to keep, you need to change the token 2 to * , which means "all the rest":如果第二部分可能包含自己想要保留的冒号,则需要将标记2更改为* ,这意味着“所有其余部分”:

for /F "tokens=1,* delims=:" %%I in (data.txt) do (
    echo 1st token: %%I
    echo remainder: %%J
)

Regard that a for /F loop combines multiple adjacent delimiters into one, hence a string like abc::def is going to be split into the parts abc and def .考虑到for /F循环将多个相邻的分隔for /F为一个,因此像abc::def这样的字符串将被拆分为部分abcdef Also note that leading delimiters become removed, so a string like :abc::def is also going to be split into the parts abc and def .另请注意,前导分隔符将被删除,因此像:abc::def这样的字符串也将被拆分为部分abcdef In other words, for /F does not return empty tokens.换句话说, for /F不返回空标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Windows命令提示符/批处理文件中的路径字符串获取叶 - Getting the leaf from a path string in Windows Command Prompt/batch file 是否可以从批处理文件(命令提示符)中使用uglifyjs? 如果是这样,怎么办? - Is it possible to use uglifyjs from a batch file (command prompt)? If so, how? 确定是否使用命令提示符的批处理文件 - Batch file to determine if using Command Prompt 批处理文件|| 如何冻结命令提示符 - Batch File || How to freeze command prompt 如何使用批处理从文本文件中删除回车符和换行符? - How to remove carriage return and line feed characters from a text file using batch? 如何在 Windows 命令提示符中从十六进制字符串写入二进制文件 - How to write binary file from hex string in Windows command prompt 使用批处理删除文件的最后n个字符 - remove last n characters of a file using batch 如何使用批处理命令从文本文件中删除 CRLF 和新行 - How to remove CRLF and a new line from text file using batch command 我如何输入从批处理文件,Java应用程序运行的命令提示符 - How do I input into a command prompt, that's ran from a batch file, from a java application 使用命令提示符调用子目录中的批处理文件 - Using Command Prompt To Call a Batch File in a Sub-Directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM