Sort in Makefile - sorting

I have this in my makefile
test:
cat t.txt | sort -t $$'\t' -k 2,2
But "make test" gives me this error
cat t.txt | sort -t $'\t' -k 2,2
sort: multi-character tab `$\t'
make: * [test] Error 2
Works fine on Redhat linux but fails on Ubuntu linux

The $'\t' syntax you're trying to use is a bash-ism, but by default gmake uses /bin/sh as the shell. You can either override the SHELL variable in your makefile, as in:
SHELL=/bin/bash
or explicitly invoke bash for this specific command:
test:
bash -c "cat t.txt | sort -t $$'\t' -k 2,2"

Q: What OS is this failing on?
SUGGESTION:
How do I sort a tab separated file on the nth column using cygwin sort?
http://linux.derkeiler.com/Mailing-Lists/Fedora/2008-03/msg02180.html
You can substitute an actual tab, e.g. sort -t "<Ctl-V><Tab>"

Related

How to run a command like xargs on a grep output of a pipe of a previous xargs from a command in Bash

I'm trying to understand what's happening here out of curiosity, even though I can just copy and paste the output of the terminal to do what I need to do. The following command does not print anything.
ls /opt/local/var/macports/registry/portfiles -1 | sed 's/-.*//g' | sort -u | parallel "sudo port -N install" {} 2>&1 | grep -Po "Use '\K.*(?=')" | parallel "{}"
The directory I call ls on contains a bunch of filenames starting with the string I want to extract that ends at the first dash (so stringexample-4.2009 pipes stringexample into parallel (like xargs but to run each line separately). After running the command sudo port install <stringexample>, I get error outputs like so:
Unable to activate port <stringexample>. Use 'port -f activate <stringexample>' to force the activation.
Now, I wish to run port -f activate <stringexample>. However, I cannot seem to do anything with the output port -f activate gettext that I get to the terminal.
I cannot even do ... | grep -Po "Use '\K.*(?=')" | xargs echo or ... | grep -Po "Use '\K.*(?=')" >> commands_to_run.txt (the output stream to file only creates an empty file), despite the shorter part of the command:
ls /opt/local/var/macports/registry/portfiles -1 | sed 's/-.*//g' | sort -u | parallel "sudo port -N install {}" 2>&1 | grep -Po "Use '\K.*(?=')"
printing the commands to the terminal. Why does the pipe operator not work here? If the commands I wish to run are outputting to the terminal, surely there's got to be a way to capture them.

using alias ccat='pygmentize -g -O style=colorful,linenos=1' with line numbers

I'm using this alias to color my cat results. However, I want to add line numbers to the output, but using ccat -n <filename> is not working.
I can achieve it by ccat <filename> | cat -n but I can't create an alias for that (I expect an alias such as ccatn which I could use like ccatn <filename>)
What can I do to get this functionality (meaning, type in a command and a file name and get the same result as typing ccat <filename> | cat -n)?
Use a function instead:
ccatn() {
pygmentize -g -O style=colorful,linenos=1 "$1" | cat -n
}

How to escape shell script to run it using command_string option?

I am trying to escape the following script to run it via shell command-string option ( /bin/sh -c ).
privateIP=$(ifconfig eth0 | grep "inet " | awk \'{print $2}\');
sed -i "s/http:\/\/:/http:\/\/$privateIP:/g" init.conf
Please elaborate on the answer.
You're question is not clear, but perhaps you are looking for:
sh -c 'privateIP=$(ifconfig eth0 | awk "/inet/{print \$2}");
sed -i "s#http://:#http://$privateIP:#g" init.conf'

Fish shell input redirection from subshell output

When I want to run Wireshark locally to display a packet capture running on another machine, this works on bash, using input redirection from the output of a subshell:
wireshark -k -i <(ssh user#machine "sudo dumpcap -P -w - -f '<filter>' -i eth0")
From what I could find, the syntax for similar behavior on the fish shell is the same but when I run that command on fish, I get the Wireshark output on the terminal but can't see the Wireshark window.
Is there something I'm missing?
What you're using there in bash is process substitution (the <() syntax). It is a bash specific syntax (although zsh adopted this same syntax along with its own =()).
fish does have process substitution under a different syntax ((process | psub)). For example:
wireshark -k -i (ssh user#machine "sudo dumpcap -P -w - -f '<filter>' -i eth0" | psub)
bash | equivalent in fish
----------- | ------------------
cat <(ls) | cat (ls|psub)
ls > >(cat) | N/A (need to find a way to use a pipe, e.g. ls|cat)
The fish equivalent of <() isn't well suited to this use case. Is there some reason you can't use this simpler and more portable formulation?
ssh user#machine "sudo dumpcap -P -w - -f '<filter>' -i eth0" | wireshark -k -i -

Strange behavior of uniq on darwin shells

I've used 'uniq -d -c file' in many shell scripts on linux machines, and it works.
On my MAC (OS X 10.6.7 with developer tools installed) it doesn't seems to work:
$ uniq -d -c testfile.txt
usage: uniq [-c | -d | -u] [-i] [-f fields] [-s chars] [input [output]]
It would be nice if anyone could checks this.
Well, it's right there in the Usage message. [ -c | -d | -u] means you can use one of those possibilities, not two.
Since OSX is based on BSD, you can check that here or, thanks to Ignacio, the more Apple-specific one here.
If you want to achieve a similar output, you could use:
do_your_thing | uniq -c | grep -v '^ *1 '
which will strip out all those coalesced lines that have a count of one.
You can try this awk solution
awk '{a[$0]++}END{for(i in a)if(a[i]>1){ print i ,a[i] } }' file

Resources