Internet Explorer (IE) may not work properly. Unfortunately, fixing this problem is out of my control and I can only suggest you to use some other browser. I am sorry for inconvenience. |
UNIX Utils & Tips contains useful utils (scripts) and tips for UNIX systems.
All scripts run on bash unless otherwise stated. |
Scripts
Log out other user on Mac (Mac only)
logout-other.sh logs out another user from the system.
Usage is: logout-other.sh ACCOUNTNAME. Remember to use sudo!
Tips
Calculate lines of (Java) code
Wanna know how many lines of code you have been written? No worries. The following command gives you the number of lines of code (all these calculation commands can be copy-pasted to your terminal):
find src/main/java/ -name '*.java' | xargs wc -l
If you want to exclude empty lines then sed can be used to delete unnecessary lines from calculation. Here is an example:
find src/main/java/ -name '*.java' | xargs sed -e '/^[[:space:]]*$/d' | wc -l
If you want to exclude all the comments also in addition to empty lines you can do this:
find src/main/java/ -name '*.java' \
| xargs sed -e '/^[[:space:]]*$/d' \
-e '/^[[:space:]]*\/\//d' \
-e '/^[[:space:]]*\/\*/,/\*\//d' \
| wc -l
Change file extension
Changing the file extension for a multiple files is a pretty simple task once you know the trick. The trick used here is one form of variable substitution on Bash; ${var%pattern}. This returns var after removing text matching the pattern from the right of var. So, to change the file extension for several files at once, type this on the command line:
for f in *.pdf; do mv $f ${f%.pdf}.bak; done
and the result would be like this:
Source | Target |
---|---|
mydoc.pdf | mydoc.bak |
otherdoc.pdf | otherdoc.bak |
nochange.txt | nochange.txt |
third.pdf | third.bak |
diff with sed (without writing on the disk in the middle)
Sometimes you want to see the changes your sed commands make to a file. Of course, this can be quite easily achieved by writing the result of the sed command on the disk and then using diff normally but it can be done directly on the console. The trick is to use minus (-) as parameter for diff command like this:
sed "s/FINDPATTERN/REPLACESTR/g" mytext.txt | diff mytext.txt - | less
Heredocs in Java
One of the nice feature of Groovy (and many other dynamic language) is the ability to write heredocs (i.e. multiline strings without having to escape anything or to concatenate different lines together manually). This is especially useful feature for testing. Unfortunately Java does not support heredocs.
However there is a way to simulate heredocs in Java. Or to be more precise to simulate the easiness of using heredocs. Here is a sed script which, when used together with command line copy/paste commands, gives you the easiness of heredocs. Let's call the following script heredoc.sed.
$!s/.$//
s/\\/\\\\/g
s/"/\\"/g
# Notice that the first argument for the substitute command
# should be a real tab character.
s/ /\\t/
1{
i\
final String eol = System.getProperty("line.separator");\
final String hereDoc =
s/^\(.*\)$/ "\1" + eol/
}
2,$s/^\(.*\)$/ + "\1" + eol/
$s/^\(.*\)" + eol$/\1";/
s/+ "" + eol$/+ eol/
Now it is very simple to create a string representation for Java of any stdout output you like. Just follow these simple steps:
- Copy to the clipboard the text from where you want to create a heredoc
- Select one of the following commands dependening on your operating system
OS | Command | Comment |
---|---|---|
Mac OSX | sed -f heredoc.sed <(pbpaste) | pbcopy | Works out of the box. |
Linux | sed -f heredoc.sed <(xclip -o) | xclip | May require installing xclip (if you have problems try -selection clipboard option). Other option is to use xsel. |
Cygwin | sed -f heredoc.sed <(getclip) | putclip | May require installing getclip and putclip. |
- Paste the content of the clipboard to your code.
Rename jpg files with a running number
Here the trick is to combine two variable substitution forms; ${var%pattern} and ${var#pattern}. The former was already explained in the "Change file extension" task. The other is its obvious counterpart; ${var#pattern} and returns var after removing text matching the pattern from the left of var. Type this on the command line to rename all your jpg files with your name and a running number:
declare -i i=0;for f in *.jpg; do i=i+1;mv $f myname-`printf "%04d" $i`${f#${f%.jpg}}; done
and the result would be like this:
Source | Target |
---|---|
345-876.jpg | myname-0001.jpg |
football.jpg | myname-0002.jpg |
pic1.jpg | myname-0003.jpg |
profile-pic.png | profile-pic.png |
window.jpg | myname-0004.jpg |