Trimming text files from command line (Windows)
I have a text file which contains several hundred lines e.g.
test.bin:8948549854958
They are all styled like the above file (xxxxxxx.xxx:xxxxxxxxxxxxxx
)
Is there any way I could trim all lines e.g. take :xxxxxxxxxxxxxxx
of th开发者_JS百科e line, so just to leave xxxxxxx.xxx
?
Trim.bat:
@FOR /F "tokens=1 delims=:" %%G IN (%1) DO @echo %%G
Usage: trim source.txt > destination.txt
See here.
Well, since it it obvious that besides powershell, there is no 'standard' tool on windows that does this, you can roll your own:
#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 the 'side effect' of normalizing line-ends (CRLF) and not allowing lines>2048 characters in the input. However, it works equally well on all platforms and I just compiled it with winegcc (winelib), mingw (on linux) and MSVC compiler. If you want a binary, let me know
Oh, mandatory usage demo:
C:\> strip.exe < input.txt > output.txt
If you can distribute an exe with your script you could use a windows compile of 'grep' like egrep. If you want to write in script you don't have a lot of options for find replace on windows. Depending on your situation you might be able to make due with 'findstr' cmd.
Such a program already exists, although it is really buggy. This is the only program I could find that has this apparent function. I wish Notepad++ or some other text editing software would implement this feature.
For notepad++ do a find and replace. Be sure to turn on "Regular expression" in the lower left hand corner of the "Replace" tab and use this as your find criteria. (**\:.*) I would replace it with a ")" unless you wish it removed as well. If so you might want to remove the "(" for symmetry but be sure to turn off the "Regular expression" radio button or subsequent searches will not perform as expected.
The easiest way to do this non-programmatically is to use a text editor such as TextPad or Notepad++ and do block select (have to switch modes from the menu) and select a rectangular area of text (the last twelve columns or whatever) and simply delete them. When you are in block select mode, the selection will not wrap around and grab the beginning of your lines.
For /F "tokens=1,2 delims=:" %i in (filename) do @echo %i
same as margnus1 answer but no batch file from the command line:
FOR /F "tokens=1 delims=:" %G IN (inlist.txt) DO @echo %G >> outlist.txt
which answers the question "From Command Line"
精彩评论