简体   繁体   中英

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

My name is Syarz and I'm new to programming. I want to know how to remove characters from a string using cmd or batch file.

This is the string in data.txt file:

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

I want this after running command or batch file in 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 -

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

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:

$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. # 7 and # 8 might come closer to what you want, but these approaches do not suit your sample data. And # 5 and # 6 are definitely not batch files.

To split text at a certain character, namely the colon in your situation, simply use a for /F loop :

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":

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 . 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 . In other words, for /F does not return empty tokens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM