How do I remove everything after the first word?

Asked

Viewed 157 times

2

How to remove everything after the first word on each line in a text file containing the character pipe?

It works for removing simple/common characters, but has not worked to remove the character | ( pipe ) of the text file.

blue | a
[email protected]:pass | a
fear under dark | a 
123 | a 
abc 123 | a 
hustlerman | a 
yellow | a 
123 | a 
[email protected]:senha | a 
[email protected]:guypassord | a 
12345678-king-man | a 
rabaman | a 
abc | a 

I’m using the following code:

setlocal enableextensions disabledelayedexpansion

set /p txtfile=Text File Name: 
    Echo.
set /p "search=Search for: "
    Echo.
set /p "replace=Replace to: "

for /f "delims=" %%i in ('type "%txtfile%.txt" ^& break ^> "%txtfile%.txt" ') do (
      set "line=%%i"
      setlocal enabledelayedexpansion
      >>"%txtfile%.tmp" echo(!line:%search%=%replace%!
      endlocal
    )

ren "%txtfile%".tmp "%txtfile%".txt 

pause

Error:

inserir a descrição da imagem aqui

1 answer

1


Unedited serves only for files with equal layout of your example, where we have the pipe as delimiter, for another delimiter, edit the command set _delimiter, and to the special characters, I imagine you already know, but put to the other visitors, if they do not know, add the ^ before the character, as an example: ^|, ^&, ^^, etc...

@echo off && setlocal enableextensions enabledelayedexpansion && color 0a

set /p "'_delimiter=^|"<nul

echo. && set /p "txtfile=Text File Name: "

copy "%txtfile%" "%temp%\%txtfile%.tmp" /y

type nul >"%temp%\%txtfile%_crop.txt"

for /f "tokens=1 delims=%_delimiter%" %%i in ('type "%temp%\%txtfile%.tmp"') do (

   set "_line=%%~i"
   echo/!_line!>>"%temp%\%txtfile%_crop.txt"

)

move /y "%temp%\%txtfile%_crop.txt" "%txtfile%"

del /q /f  "%temp%\%txtfile%.tmp"

pause
  • Thanks, sorry for the delay, it helped a lot!

  • Trankilo Tê!! No peacock!! Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.