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

问题描述 投票:0回答:1

我叫Syarz,我是编程新手。我想知道如何使用cmd或批处理文件从字符串中删除字符。

这是data.txt文件中的字符串:

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

我在运行命令或data.txt中的批处理文件后需要这个

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

我已经尝试了网站上提供的几种方法,但是似乎没有一种对我有用。他们要么删除整个字符串,要么删除我不想要的部分。

我已经尝试过的内容

1-

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

2-

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

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-

@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-

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-

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-

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

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

只是尝试学习新知识。谢谢大家。

windows shell batch-file cmd command-line
1个回答
0
投票

[我知道您要批处理,但我没有提供批处理答案,所以我会帮助您的人员返回批处理答案。

这是执行PowerShell的简单方法:

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

$f | ForEach-Object { 
    $_.Split(':')[1] 
} | Out-File $out -Append
© www.soinside.com 2019 - 2024. All rights reserved.