Trimming text files from the command line (Windows)

I have a text file that contains several hundred lines, for example.

test.bin:8948549854958 

They are all styled as the aforementioned file ( xxxxxxx.xxx:xxxxxxxxxxxxxx )

Is it possible to somehow trim all lines, for example. take :xxxxxxxxxxxxxxx lines, so just leave xxxxxxx.xxx ?

+4
source share
7 answers

Trim.bat:

 @FOR /F "tokens=1 delims=:" %%G IN (%1) DO @echo %%G 

Usage: trim source.txt > destination.txt

See here .

+7
source

Well, since it is obvious that besides powershell there is no standard tool for windows that does this, you can roll:

 #include <stdio.h> #include <string.h> main(int argc, char *argv[]) { char s[2048], *pos=0; while (fgets(s, 2048, stdin)) { if (pos = strpbrk(s, ":\r\n")) *pos='\0'; puts(s); } return 0; } 

Note that this has a β€œside effect” of line end normalization (CRLF) and does not allow lines> 2048 characters per input. However, it works equally well on all platforms, and I just compiled it using wingcc (winelib), mingw (on linux), and the MSVC compiler. If you want a binary, let me know

Oh, mandatory demonstration of use:

 C:\> strip.exe < input.txt > output.txt 
+3
source

If you can distribute exe with a script, you can use Windows compilation from grep, such as egrep . If you want to write in a script, you do not have a lot of options for replacing on windows. Depending on your situation, you can do this with "findstr" cmd.

0
source

Such a program already exists , although it is really buggy. This is the only program I could find that has this obvious function. I wish Notepad ++ or some other text editing software to implement this feature.

0
source

For notepad ++, find and replace. Be sure to include the Regular Expression in the lower left corner of the Replace tab and use this as your search criteria. (** \ :. *) I would replace it with ")" if you did not want to delete it. If so, you can remove the "(" for symmetry ", but be sure to turn off the Regular Expression radio button or subsequent searches will not run as expected.

0
source

The easiest way to do this non-programmatically is to use a text editor such as TextPad or Notepad ++, and make a block selection (you need to switch modes from the menu) and select a rectangular area of ​​the text (last twelve columns or whatever) and just delete them. When you are in block selection mode, the selection will not wrap around and capture the beginning of your lines.

0
source
 For /F "tokens=1,2 delims=:" %i in (filename) do @echo %i 
0
source

All Articles