Translate Linux 'cut' command to Windows command - linux

I am running some code which necessitates the usage of the following line exactly:
cat data/train.txt | cut -d$'\t' -f 2 | grep -v "^$"| sort | uniq > data/labels.txt
Does Anyone know how this line of command can be translated to a Windows command - or what it means?
P.S. I have been trying to run the code through WSL on my Windows, and the line does execute correctly, however the package I'm trying to use necessitates use of Pytorch with GPU access, which WSL does not facilitate, thus the need to use it on the Windows command line

The Answer was provided by #a_horse_with_no_name, as downloading the utix utilities for windows from: unxutils.sourceforge.net

Related

An error occurred while processing STDERR using the pipe

My working directory is as follows:
MyWorkDirectory
└── test
The file "test" contains a line of information:
1:2:3:4:5
When I use the following command:
cat test foo
the output is as follows:
1:2:3:4:5
cat: foo: No such file or directory
I want to ignore STDOUT and only deal with STDERR, and I want to use the cut command to get the third STDERR segment separated by the ":",So I tried the following command:
cat test foo 2>&1 1>/dev/null | cut -d ':' -f 3
I think the output here should be as follows:
No such file or directory
However, there are many different outputs here:
1、
1
No such file or directory
2、
No such file or directory
3
3、
2
4、
...
Why are these outputs generated? What commands should I use if I want to achieve my goal?
The issue here is "multiple redirect".
The reason this can not be reproduced on Ubuntu, is because Ubuntu (like many Debian based Linux distros) uses Ash or Bash shell by default, while recent MacOS version switched to using ZSH.
ZSH on Ubuntu will give similar result to what you are getting.
Apparently, using both redirect > and pipe | causes ZSH to split you stderr similar to tee command.
This post shows a solution to the problem:
https://unix.stackexchange.com/questions/265061/how-can-i-pipe-only-stderr-in-zsh
Specifically, you need to close stdout before redirecting it to /dev/null to prevent the split:
cat test foo 2>&1 >&- > /dev/null | cut -d ':' -f 3
This works with ZSH on Ubuntu, and should work on Mac OS.
If it does not, check the linked post for enabling mult_ios option in your shell.

Run grep in build step in teamcity linux agent

I'm running my teamcity agent in linux. The first step of the build will get the version of a package using a command line script, which contains the following:
$versionNumber = grep "<Version>" %env.RelativeProjectPath% | sed -e "s/<[^>]*>//g"
The problem is that I receive this error:
/BuildAgent/temp/agentTmp/custom_script5887915083946808286: line 1: versionNumber =: command not found
Any ideas why this happens or how to fix it?
Problem solved using this:
versionNumber=$(grep '<Version>' %env.RelativeProjectPath% | sed -e 's/<[^>]*>//g')

shell cmd transfair windows cmd

I have a regexp using grep which I use to get the latest directory matching it.
Any chance I get a equivalent command as the one below to work on a windows machine?
ls | grep -E "20[0-9]{2}-[0-1][0-9]-[0-3][0-9]-[0-9]{0,5}" | tail -1
I would appreciate any help.
Installing cygwin will give you a nearly linux like shell environment. When you set the cygwin bin into your windows PATH you can even build the pipe like that in a CMD. I can't say anything about the unix like stuff that newer windowses are supposed to bring along though.

Linux command to DOS

I have a file include some linux command and I want to run in on windows (DOS command).
The command is:
cat tmp/$id/index.html | sed -e 's/ID/$id/g' > a;mv a tmp/$id/index.html
What is the similar command in MS-DOS?
Thank you!
The problem is that natively there is no equivalent command to sed. You have two options from my point of view. Either create a vb script that does what you want (It will not take 1 line though - more like 10-15 I guess), or use something like GnuWin32 that gives you the option to run unix commands in windows terminal.
You could consider using powershell to do approximately the same thing. It supports cat and mv and you can get a sed like equivalent by using %{_ -replace "expression", "replace"}. Details here http://blogs.msdn.com/b/zainnab/archive/2007/07/09/grep-and-sed-with-powershell.aspx
Or consider using a linux like command prompt like bash which should be available through cygwin
I think this is impossible to do in "bare" command line (as you called DOS command), because cat and sed are separate utilities. If you want to port this script from Linux command shell to windows command line, I would advise you to download and install CygWin
DOS itself does not have support for that. You could try with a port of SED for DOS available here. If you can get Powershell, that's an option. Here's an example of using grep/sed with Powershell.
There are many options.
You can try to install cygwin or download and install Git and use Git-bash or add the bin directory to your PATH so you can run this command on your CMD prompt.
There is no such command(s) for MS-DOS.

Read stdout from a process (linux embedded)

Before flagging the question as duplicate, please read my various issues I encountered.
A bit of background: we are developing a C++ application running on embedded ARM sbc using a lite variant of debian linux. The application start at boot launched by the boot script and print various information to stdout. What we would like is the ability to connect using SSH/Telnet and read the application output, without having to kill the process and restart it for the current bash session. I want to create a simple .sh script for non-tech-savvy people to use.
The first solution for the similar question posted here is to use gdb. First it's not user-friendly (need to write multiple commands manually) and I wonder why but it don't seems to output anything into the file.
The second solution strace -ewrite -p PID works perfectly, that's what I want. Problem is, there's a lot more information than just the stdout, and it's badly formatted.
I managed to get an "acceptable" result with strace -e write=1 -s 1024 -p 20049 2>&1 | grep "write(1," but it still have the superfluous write(1, "...", 19) = 19 text. Up to this point it's simply a bit of string formatting, and I've found on multiple other pages this line saying it achieve good formatting : strace -ff -e write=1,2 -s 1024 -p PID 2>&1 | grep "^ |" | cut -c11-60 | sed -e 's/ //g' | xxd -r -p
There are some things I find strange in this command (why -ff?, why grep "^ |"?, why use xxd there?) and it just don't output anything when I try it.
Unfortunately, we do use a old buggy version of busybox (1.7.1) that have some problem with multiple pipes. That bug gives me bad results. For example, if I only do grep it works, and if I only do cut it also works, but let's say "grep "write(1," | cut -c11-60" returns nothing.
I know the real solution would simply be to update busybox and use these multiple pipes to format the string, but we can't update it since the os distribution is already installed on thousands of boards shipped to our clients worldwide..
Anyone have a miraculous solution? Thanks
Screen can be connected to an existing process using reptyr (http://blog.nelhage.com/2011/01/reptyr-attach-a-running-process-to-a-new-terminal/), or you can use neercs (http://caca.zoy.org/wiki/neercs) which I haven't used but apparently is like screen but supports attaching to an existing process all by itself.

Resources