Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent to 'cut' on Windows [duplicate]

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?

like image 414
user3437212 Avatar asked Jul 31 '14 18:07

user3437212


2 Answers

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

like image 155
MC ND Avatar answered Oct 31 '22 19:10

MC ND


PowerShell:

get-content test.txt | foreach-object {
  $_ -replace '/a/b/c/',''
} | out-file test2.txt
like image 43
Bill_Stewart Avatar answered Oct 31 '22 19:10

Bill_Stewart