This blog is part of a shell tips list I find useful to use on every script -- the whole list can be found here.
In the situational Unix/Linux commands family, one would most likely be on top of the list: rev. Indeed, rev has no option and does one and unique thing: it reverse characters. Let's have a look at an example:
To sum up, you will most likely not use rev on a daily basis but it is still a command worth to know as you will need it one day for sure!
< Previous shell tip / Next shell tip >
$ echo "abcdef" | rev fedcba $It also works with files:
$ rev afile.txt 1 enil elifa 2 enil elifa $And with heredocs as well:
$ cat << EOF | rev heredoc line 1 heredoc line 2 EOF 1 enil codereh 2 enil codereh $And the result is even more interesting when using tac instead of cat
$ tac << EOF | rev heredoc line 1 heredoc line 2 EOF 2 enil codereh 1 enil codereh $I would not argue that this is useful on a daily basis though :) You can use rev to sort a list by the last character:
$ cat << EOF | rev | sort | rev file1.txt file2.log file3.csv EOF file2.log file1.txt file3.csv $You could also use rev to have the last character of a strong in uppercase for example by using an extra variable and variable substitution:
$ VAR=$(echo "abcdef" | rev) $ echo "${VAR^}" | rev abcdeF $But to be fair we could also do this with sed:
$ echo "abcdef" | sed 's/.$/\U&/' abcdeF $And if you would like to upper case the first and the last character of a string, you would do it as below:
$ echo "abcdef" | sed -e 's/\(^.\)\(.*\)\(.$\)/\U\1\L\2\U\3/' AbcdeF $But this is another story :)
To sum up, you will most likely not use rev on a daily basis but it is still a command worth to know as you will need it one day for sure!
Great tipps, I enjoy it;)
ReplyDelete