wtorek, 5 maja 2015

Using grep and xargs for files with spaces in their names

Normally you could just make grep search recursively, redirect the output to xargs and get the job done. E.g. to replace some string with another:
grep -lr ABCSOMETHING . | xargs sed -i 's/ABCSOMETHING/SOMETHING/g'
... but sometimes you can get an error like:
sed: can't read ./Next: No such file or directory
Issuing solely the grep part reveals that spaces are the culprit:
grep -lr ABCSOMETHING .
./Next version/some file.xml
./Next version/another file.xml
The solution is to change the delimiter upon which the 2 piped commands communicate to a null character:

grep -lrZ ABCSOMETHING . | xargs -0 sed -i 's/ABCSOMETHING/SOMETHING/g'

The relevant quote from xargs manual:

--null
-0     Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally).  Disables the end of file string, which is treated  like  any other argument.  Useful when input items might contain white space, quote marks, or backslashes.  The GNU find -print0 option produces input suitable for this mode.

And the same from grep manual:

-Z, --null
        Output a zero byte (the ASCII NUL character) instead of the character that normally follows a file name.  For example, grep -lZ outputs a zero byte after each file name instead of the usual newline.  This option makes the output unambiguous, even in the presence of file names containing unusual characters like newlines.  This option can be used with commands like find -print0, perl -0, sort -z, and xargs  -0  to  process  arbitrary file names, even those that contain newline characters.

środa, 15 października 2014

How to quickly get rid of all xs:annotation elements from XML schema file (XSD, that is)

xmlstarlet ed -N xs="http://www.w3.org/2001/XMLSchema" -d "//xs:annotation" schema-file.xsd > pruned-schema-file.xsd

It lends itself well to automation and I like it. It uses XmlStarlet.
Useful when you want to compare 2 schemas (or versions of the same schema) and you're interested mainly in the schema definition.

środa, 9 kwietnia 2014

Running MPlayer in the background

Well,  a standard & doesn't work. But there is a trick:

mplayer somefile.mp3 < /dev/null &

http://www.mplayerhq.hu/DOCS/HTML/en/faq.html#idp61248544

środa, 2 kwietnia 2014

Log only commit hashes in Git


The answer is --pretty=format:%H. E.g.
git log --reverse --pretty=format:%H my-branch-created-from-maintenance..my-branch-created-from-master
Useful in some ad-hoc scripts.
Suppose there are some changes on maintenance branch you do not want on master (yet?). You have created a branch from maintenance and made some local commits there. Now it turns out they should be commited on master, not maintenance. If you rebase on master, the unwanted changes will be present there. So:
for hash in $(git log --reverse --pretty=format:%H my-branch-created-from-maintenance..my-branch-created-from-master)
do
git cherry-pick $hash
done
Actually, I think there is some nicer way of doing it...

środa, 11 grudnia 2013

Revive deleted file from Git

You accidentally deleted one file which you didn't want to. But together with a bunch of other files you no longer need. Well, you can make a temporary branch off the "last good" Git commit and take the file from there. But there is a better way.
git show LAST_GOOD_COMMIT_HASH:path/to-the/file > path/to-the/file
# if you have just done it so it has happened in the last commit
git show HEAD^1:path/to-the/file > path/to-the/fil
e
... etc, so in general:
git show COMMIT:path/to-the/file > path/to-the/file

poniedziałek, 2 grudnia 2013

A quick and dirty Groovy snippet to test JDBC connection to Oracle

That's a snippet to test JDBC connection to Oracle with minimum number of lines.
1. Run Groovy shell with ojdbc6.jar on the classpath:
groovysh -cp /opt/sqldeveloper/jdbc/lib/ojdbc6.jar
2. Run the script:
import groovy.sql.Sql
sql = Sql.newInstance('jdbc:oracle:thin:@localhost:1521:mysid',
'user', 'password', 'oracle.jdbc.OracleDriver')
result = sql.firstRow("select * from dual")

===> {DUMMY=X}
In the end it turned out to be a problem described here:
http://appsdbastuff.blogspot.com/2012/01/starting-listener-fails-with-tns-12557.html
In short, the solution was to run this as root:
mkdir /var/tmp/.oracle
chmod 01777 /var/tmp/.oracle
chown root /var/tmp/.oracle
chgrp root /var/tmp/.oracle

How to mount VMDK (VMware disk file) under Linux

Provided you have some VMware product installed (VMware Player will suffice)
sudo mkdir /mnt/vmdk
sudo vmware-mount the-disk.vmdk /mnt/vmdk/
If the disk has only one partition, that's everything you need.
To unmount:
sudo vmware-mount -d /mnt/vmdk/
To list partitions on a VMDK file:
vmware-mount -p the-disk.vmdk
To mount nth partition (they say the numbering starts from 1):
sudo vmware-mount the-disk.vmdk n /mnt/vmdk/
See also http://pubs.vmware.com/vsphere-50/index.jsp?topic=%2Fcom.vmware.vddk.utils.doc_50%2Fdiskutils_mount.4.4.html