Thursday, July 3, 2008

executing a command from 'find' results in UNIX

This one greps through all the files from the 'find' for org/apache/regexp:


find . -exec grep "org/apache/regexp" '{}' \; -print

complicated rearrangements of text in UNIX

This shows how to take whitespace-delimited input and build something fancy from it:


echo -e "123 abc\n987 zyx" | awk '{ print "<nodeXXX attr1=\"" $1 "\">\n\t" $2 "\n\t<nodeYYY attr2=" $1 "/>\n</nodeXXX>"}'

matching multiple lines in Unix

This will pull out the multiple lines from the log, where the first line has XXX and the last line has YYY:


perl -n -e "print if (/XXX/ ... /YYY/)" file


To substitute string "country_bumpkin" for "country" in a whole bunch of files:


perl -p -i -e "s/country/country_bumpkin/g" files

counting lines in Unix

Andrzej tracked down those open files with the following command. What's cool is that he counts how many instances a record occurs with 'uniq', and he uses 'sort -n':


lsof | grep -e '^java.* REG ' | grep ' dtstage ' | awk '{print $9}' | sort | uniq -d -c | sort -n


Here's a quick demo of 'sort -n'... see how the results are different if you remove the '-n':


echo " 1\n.5" | sort -n