Trimming text files from command line (Windows) - command-line

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 the 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"

Related

How to hide "PS C:\>" in powershell?

So you know how in powershell if you enter a command infront of it there will always be "PS (directory)>".
I know its possible in cmd with a simple command but how do you do this in powershell?
so:
Hide the text(PS C:>) thats infront of commands
If possible through a command
If any of you know the fix please let me know.
If i find it first i will self anwser.
(I later found this dup):Windows PowerShell: changing the command prompt
Just like Calculuswhiz mentioned, the following should work:
Function Prompt {" "}
Simply change it by adding anything else inside the double quotes.
Abraham Zinala's helpful answer shows that it is the prompt function - described in the about_Prompts conceptual help topic - that determines the string that PowerShell prints in interactive sessions when prompting the user for commands.
As Calculuswhiz notes, outputting the empty string ('') is not an option for not printing a prompt string at all, because PowerShell then defaults to PS>.
Printing a single space is one way to work around the problem, but that space does print. To prevent that, Dabombber proposes workarounds in a comment on Abraham's question, but the simplest solution as of Windows 10 is:
function prompt { "`0" } # Fully HIDES the prompt string.
"`0" outputs a NUL (null character), which effectively hides the prompt string, without side effects, including in Windows Terminal and ConEmu.[1]
On Windows 7, the NUL prints as a space, in which case Dabombber's alternative helps:
# A space followed by a backspace char. in effect amounts to the empty string.
function prompt { " `b" }
Add this function to your $PROFILE file to hide the prompt string in all future sessions.
[1] Verified on version v20.11.24.0. Dabombber mentions that ConEmu prints NUL characters as a space, but, given that ConEmu is conhost.exe-based, just like regular console windows, that applies only up to Windows 7.

How achieve a two line Prompt?

In the batch language of Microsoft's CMD.EXE console window, I never liked having my command start at the far right, after a long display of the directory path. So in my Control Panel → System → Advanced System Settings → Environment Variables I saved the following assignment, where $_ is like a Soft Return:
PROMPT=[$P\]$_$+$G$S
The displayed prompt was two lines like this:
[C:\Temp\]
>
(The $+ tracks pushd and popd, the fancier than chdir commands. $S is space. By the way, the ^ character a line wrap/continuation character in batch, just as backtick ` is in PowerShell.)
Now I want the same-ish two line prompt in PowerShell. There is good news and bad news.
The good news is I can achieve that in my open PowerShell window by typing at the > prompt:
function prompt {'[' + $(get-location) + '\] SHIFTENTER > '
(By SHIFTENTER I mean press Shift+Enter, what I think might be called a "soft return"?)
....... BAD NEWS, PROBLEM ......
I want to put the above function prompt ... line into my profile PowerShell script, namely Microsoft.PowerShell_profile.ps1 (at path $Profile). But how?
Notepad.exe has no support for Shift+Enter.
MS Word understands Shift+Enter, but when I SaveAs .txt, and then examine with Notepad++, I see a plain CR-LF (meaning \r\n, 0x0d 0x0a).
Notepad++ menu Edit → Character Panel enables me to insert special ASCII characters into my .txt / .ps1 file, such as 0x0b called VT (for "vertical tab"). But despite some claims on websites, VT is not behaving like a Soft Return when I use it in my function prompt ... profile .ps1 file (I also run the profile .ps1 script to retest).
Can the prompt I want be established by a profile .ps1 script?
The PowerShell equivalent of your batch-prompt is:
function prompt { "[$(Get-Location)\]`r`n$("+"*(Get-Location -Stack).Count)>" }
#`r`n is just a shorter way of writing [System.Environment]::NewLine
Add it to the profile to suits your needs:
AllUsersAllHosts:
C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersPowerShell:
C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
AllUsersISE:
C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShellISE_profile.ps1
CurrentUserAllHosts:
C:\Users\username\Documents\WindowsPowerShell\profile.ps1
CurrentUserPowerShell:
C:\Users\username\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
CurrentUserISE:
C:\Users\username\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

How can one provide colour to tab completion in tcsh?

(Crossposted to unix.stackexchange.com)
This question and the answer teach us how to introduce colour into tcsh prompts.
This webpage explains nicely how to get colour into any output of the echo command:
> echo \\e[1\;30mBLACK\\e[0m
BLACK
> echo '\e[1;30mBLACK\e[0m'
BLACK
The word 'BLACK' in the example above is printed with a black (or darkgrey) foreground colour (depending on the overall color scheme).
Now I'd like to introduce this into the [TAB] command autocompletion feature of tcsh. I tried:
complete testcmd 'p/*/`echo '"'"'\e[1;30mf834fef\e[0m'"'"'`/'
And I get:
> testcmd [TAB]
> testcmd ^[\[1\;30mf834fef^[\[0m
Obviously the characters lose their special meaning. Hopefully I just did not get the escaping right. But I tried several other ways. So any help is appreciated.
The real use case is that I've got a command completion that offers three different types of completions and I'd like to visually distinguish the types. Also the alternatives are computed by an external command. That is why I need the completion to use the backticks with an external command, such as echo. I don't care about the details of this command. If you make it work in any way with the tcsh's complete command I'll probably be able to adapt (thinking perl -pe wrappers and such).
The reason why I believe this has to work somehow is that the tcsh itself offers coloured command completion if you e.g. type ls [TAB]. That works correctly in my setup. Also you can use ls -1F inside the autocompletion and the colours that ls outputs are also piped through. An example would be:
complete testcmd 'p/*/`ls -1F`/'
Update: As user mavin points out, the colourization of ls in this example is indeed not piped through. The colours of ls are lost, but the auto completion can reapply colours according to LS_COLOURS variable based on hints such as the / and * marker endings as added by the ls. This can be verified by doing
complete testcmd 'p/*/`ls --color -1`/'
which fails to provide colour, and only provides garbled output. (Literally pipes through the escape character sequences)
I'm on tcsh version 6.13.00
Any ideas? Pointers?
In your example, complete testcmd 'p/*/ls -1F/', the colours aren't getting passed through from ls. You'll find that you get colour here even if you set ls to produce monochrome output, but not if you leave off the -F. What's happening is that tcsh is doing its own colouring based on the symbols added to the end of each filename by ls -F. For example:
complete testcmd 'p%*%`echo dir/ symlink# device# socket=`%'
You could exploit this in your completion generator, e.g.,
complete testcmd 'p/*/`echo foo bar | perl -lane '"'"'print join " ", map { $_. "%" } #F'"'"'`/'
The snag is that you end up with these symbols in your completed command-line, and will have to manually backspace each time.
tcsh will colour filenames based on their suffix, dependent on the $LS_COLORS environment variable (e.g., show all *.gz files in red). You could pre-calculate the list of potential completions, place them all in $LS_COLORS, then set up dummy files for the completion to use. If you use the precmd alias, you can have the completions automatically recalculated every time the prompt is shown.
complete testcmd "p#*#F:$HOME/.cache/testcmd-completions#"
alias prep-testcmd "setenv LS_COLORS '*red=01;31:*green=01;32:' && rm -r ~/.cache/testcmd-completions && mkdir -p ~/.cache/testcmd-completions && touch ~/.cache/testcmd-completions/red ~/.cache/testcmd-completions/green"
alias precmd prep-testcmd
Aside: it'd be nice to use this with a ``-style completion rather than an F-style completion; that way you wouldn't need to create the dummy files. However, I tried that in tcsh 6.17 and it didn't work.

How can I script vim to run perltidy on a buffer?

At my current job, we have coding-style standards that are different from the ones I normally follow. Fortunately, we have a canned RC file for perltidy that I can apply to reformat files before I submit them to our review process.
I have code for emacs that I use to run a command over a buffer and replace the buffer with the output, which I have adapted for this. But I sometimes alternate between emacs and vim, and would like to have the same capabilities there. I'm sure that this or something similar is simple and had been done and re-done many times over. But I've not had much luck finding any examples of vim-script that seem to do what I need. Which is, in essence, to be able to hit a key combo (like Ctrl-F6, what I use in emacs) and have the buffer be reformatted in-place by perltidy. While I'm a comfortable vim-user, I'm completely clueless at writing this sort of thing for vim.
After trying #hobbs answer I noticed that when filtering the entire buffer through perltidy the cursor returned to byte 1, and I had to make a mental note of the original line number so I could go back after :Tidy completed.
So building on #hobbs' and #Ignacio's answers, I added the following to my .vimrc:
"define :Tidy command to run perltidy on visual selection || entire buffer"
command -range=% -nargs=* Tidy <line1>,<line2>!perltidy
"run :Tidy on entire buffer and return cursor to (approximate) original position"
fun DoTidy()
let l = line(".")
let c = col(".")
:Tidy
call cursor(l, c)
endfun
"shortcut for normal mode to run on entire buffer then return to current line"
au Filetype perl nmap <F2> :call DoTidy()<CR>
"shortcut for visual mode to run on the current visual selection"
au Filetype perl vmap <F2> :Tidy<CR>
(closing " added to comments for SO syntax highlighting purposes (not required, but valid vim syntax))
DoTidy() will return the cursor to its original position plus or minus at most X bytes, where X is the number of bytes added/removed by perltidy relative to the original cursor position. But this is fairly trivial as long as you keep things tidy :).
[Vim version: 7.2]
EDIT: Updated DoTidy() to incorporate #mikew's comment for readability and for compatibility with Vim 7.0
My tidy command:
command -range=% -nargs=* Tidy <line1>,<line2>!
\perltidy (your default options go here) <args>
If you use a visual selection or provide a range then it will tidy the selected range, otherwise it will use the whole file. You can put a set of default options (if you have any) at the point where I wrote (your default options go here), but any arguments that you provide to :Tidy will be appended to the perltidy commandline, overriding your defaults. (If you use a .perltidyrc you might not have default args -- that's fine -- but then again you might want to have a default like --profile=vim that sets up defaults only for when you're working in vim. Whatever works.)
The command to filter the entire buffer through an external program is:
:%!command
Put the following in ~/.vimrc to bind it to Ctrl-F6 in normal mode:
:nmap <C-F6> :%!command<CR>
For added fun:
:au Filetype perl nmap <C-F6> :%!command<CR>
This will only map the filter if editing a Perl file.
Taking hobbs' answer a step further, you can map that command to a shortcut key:
command -range=% -nargs=* Tidy <line1>,<line2>!perltidy -q
noremap <C-F6> :Tidy<CR>
And another step further: Only map the command when you're in a Perl buffer (since you probably wouldn't want to run perltidy on any other language):
autocmd BufRead,BufNewFile *.pl,*.plx,*.pm command! -range=% -nargs=* Tidy <line1>,<line2>!perltidy -q
autocmd BufRead,BufNewFile *.pl,*.plx,*.pm noremap <C-F6> :Tidy<CR>
Now you can press Ctrl-F6 without an active selection to format the whole file, or with an active selection to format just that section.
Instead of creating a new keyboard shortcut, how about replacing the meaning of the = command which is already in people's finger memory for indenting stuff? Yes, perlcritic does more than just indent but when you use perlcritic anyways, then you probably don't want to go back to the inferior "just indent" = command. So lets overwrite it!
filetype plugin indent on
autocmd FileType perl setlocal equalprg=perltidy
And now we can use = just like before but with the added functionality of perlcritic that goes beyond just indenting lines:
== run perlcritic on the current line
5== run perlcritic on five lines
=i{ Re-indent the 'inner block', i.e. the contents of the block
=a{ Re-indent 'a block', i.e. block and containing braces
=2a{ Re-indent '2 blocks', i.e. this block and containing block
gg=G run perlcritic on the entire buffer
And the best part is, that you don't have to learn any new shortcuts but can continue using the ones you already used with more power. :)
I'm used to select text using line oriented visual Shift+V and then I press : an I have !perltidy -pbp -et4 somewhere in history so I hit once or more up arrow ⇧.

Unicode output on Windows command line?

I wrote a small Java application which output includes Unicode characters. When I use Eclipse to run it—I see all the output as expected.
The people who are supposed to use the application will run it as a jar file. I thought they could use standard cmd window, but in this window the Unicode appear as Gibberish.
Is there a way to make "cmd window" recognize the Unicode chars and display them properly?
Or, is there any tool to easily run the jar file and get the correct output?
BTW - redirecting the output to a file works okay, but the program is interactive, so this will not be a good solution.
Edit: Thanks everybody for the suggestions.
It seems that the cmd fonts don't have the specific characters I need, and this is why changing the code page did not solve my problem.
I found a way to add more monospaced fonts to the console, but after I add them any change that I want to do regarding the fonts (even choosing one of the original fonts in a different size) - is ignored.
I think that I will simply try with another tool, which supports chhosing a differnt font more easily.
Reference: Java Unicode on Windows Command Line
Try chcp 1252 or chcp 65001 from the command line. With Lucida Console or other font support.
try CMD /c /U java your.jar
The problem is the font with which the windows console is displaying output. Unfortunately for you, this is a user setting.
I recommend you suggest that your users set their windows console font to Lucida Console. That font should be able to handle wide/unicode characters.
In C++/C just use this: system("chcp 65001");
Don't forget to change the console's font to Lucida Console
For any answers, check it first. This is a simple console program, which verifies that changing the font actually does not work.
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main( string[] args )
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.WriteLine( "日本語です" );
Console.Write( "Finished. Press a key. " );
Console.ReadLine();
return;
}
}
}
I will check to see if the answer is concretely "Cannot be done". Other avenues to check: use a different shell. i.e. Powershell? I'll see if that works.
However, you could do:
ConsoleApplication1.exe > output.txt
notepad.exe output.txt
Disclaimer: My example is C#, but the console application should still work as such.
And view the results like that, in the meanwhile.
UTF-16 on cmd.exe
Open/run cmd.exe
Click on the icon at the top-left corner
Select properties
Then "Font" bar
Select "Lucida Console" and OK.
Write Chcp 10000 at the prompt
Finally dir /b
Also from
Is there a Windows command shell that will display Unicode characters?
CHCP 65001
DIR > UTF8.TXT
TYPE UTF8.TXT