7

I have a file which has entries as below.

/a/b/c/d/e/f
/a/b/c/g/k/l/f/j/h
/a/b/c/i/m/n/p

I need a command in Windows which would remove the '/a/b/c' part from the file.

The output file should look like

d/e/f
g/k/l/f/j/h
i/m/n/p

I tried using the for command with / as the delimiter, but I couldn't get the expected result. How can I do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3437212
  • 637
  • 1
  • 10
  • 20
  • 2
    there is a windows port of cut http://gnuwin32.sourceforge.net/packages/coreutils.htm – Hank Lapidez Jul 31 '14 at 18:30
  • I get this error 'cut' is not recognized as an internal or external command, operable program or batch file. – user3437212 Jul 31 '14 at 18:38
  • @user3437212 did you download the GnuWin32 package? Did you put the tools into your `PATH` variable? –  Jul 31 '14 at 18:40
  • @a_horse_with_no_name Isn't there any other way this can be done? without installing the gnuwin32 package? I dont have previleges to install packages on the system that I'm working on currently :( – user3437212 Jul 31 '14 at 18:44
  • I don't think so. But what's the problem with "installing" it? Download the zip-file, unzip it, done. –  Jul 31 '14 at 18:45
  • Consider UnxUtils as well. – konsolebox Jul 31 '14 at 19:12

2 Answers2

12
@echo off

(for /f "tokens=3,* delims=/" %%a in (input.txt) do echo %%b) > output.txt

And the "trick" is to ask for the third token and the rest of the line.

To use it directly from the command line:

(for /f "tokens=3,* delims=/" %a in (input.txt) do @echo %b) > output.txt

The escaped percent signs are simplified and, as from command line the echo off is not enabled by default, the @ before echo is needed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • this is exactly what i was looking for! thanks! %%b is a typo? I seem to get wrong result when i give echo %%a !! – user3437212 Jul 31 '14 at 19:26
  • Cool trick. Wicked notation. So the above is not a typo. For each 'token' pulled out from the delimiter, it's assigned to the first letter in the series, %%a, and the code automatically assigns subsequent matches to incremented variables %%b, %%c,...and so on. I don't know if it always has to start with %%a. So the trick used above takes the (3)rd token, followed by all other tokens (*). This gets assigned to %%a and %%b respectively. Since you wanted everything that trails /a/b/c, you want the second match for output, which is %%b. %%a just serves as a positional anchor. – Robert Casey Jan 29 '15 at 00:57
  • 1
    how do I use it in a piped operation? I have this for Linux: svn info | grep "Revision:" | cut -d':' -f2 that gives me the "102" from the string "Revision: 102" and I need the equivalent in Windows BAT file. So far I have; svn info | findstr "Revision:" and that works fine, but I don't know how to pipe the result to the for to isolate the number from the word. – m4l490n Jul 09 '19 at 18:00
2

PowerShell:

get-content test.txt | foreach-object {
  $_ -replace '/a/b/c/',''
} | out-file test2.txt
Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62