Tag Archives: bash

The simple solution with awk is doing:

awk ‘{print $NF}’

This is useful when like in the following  we want to list just files skipping directories:

ls -l | awk ‘NR!=1 && !/^d/ {print $NF}

There are other options of course, take a look at this example from a nice collection of bash and unix tips

 

 

This is how I quickly pull and insert the subversion path within the emacs shell, as I often need the absolute URL when restoring to a file to previous version for example.

(defun svn-root ()
"get path to svn repository"
(interactive)
(insert (shell-command-to-string "svn info | awk -F ':' '/URL/' | cut -c 6- | tr -d '\n'")))

Here’s something interesting I just learned about. You can echo a command to see it’s syntaxis before running it in the command line, and then, pipe it to the shell, like this:

echo ls -ltra | awk '/.html~/' | xargs rm

(examine it carefully) and call it again adding ” bash

echo ls -ltra | awk '/.html~/'| xargs rm | bash

Another recent use I gave to it is in this command:

svn status | grep ^M | awk '{print "scp ",$2," me@xxx:/home/myTemp/"}' | tr -d '\n' | bash

Which: 1) pulls the status from the svn, 2)filters the modified files, 3) prints the scp command with the name of the file and the destination, 4) makes that a sequence in one line (by removing the newlines “\n”), and finally pipes all to bash to execute all the commands and get the modified files moved elsewhere.

find . -type f -mtime -22 | awk '/gif/' | cut -c 3- | awk '{print "scp ",$0, " me@xxx:/home/ignacio/myTemp"}' | bash -i

This last example doesn’t need the (\n) newlines removed to run thanks to the “-i” option after bash
Ain’t it sweet?

To massively replace text from a group of files:
// this searches all html files (not into htdocs) which contain tab = “DATs” and modifies that to tab = “Datasets” in one pass
// it’s important to notice that in the process they leave a backup copy of each one with the -e appended to the extension so bla.html will be modified but an extra bla.htm-e will exist holding the content of the old file

find .  -name '*.html' | awk '!/htdocs/' | xargs grep -l 'tab = "DATs"' | xargs sed -i -e 's/tab = "DATs"/tab = "Datasets"/g'

Note1: Actually the -i option takes whatever it’s after to create the extension of the backup to create. Use -ei if you want nothing or -e -i.bak to make it more standard.
Note2:
this worked better later on

find . -type f | xargs grep "url *=> *'/DAT_introduction.html'" | awk '!/svn|htdocs|blib|README|.bk/' | awk '{print $1}' | awk -F ':' '{print $1}' | xargs perl -pi -e "s#url *=> *'/DAT_introduction.html'#url => '/datasets.html'#g"

There are many things different:
awk cleans clean up the fields so to have only the path and file name printed
awk also helps here filtering out many option I don’t want to list and they are put together with the simple clause “|” (or) in the regex

instead of sed I used perl this time (sort of more familiar), notice that I used a different character “#” for the substitution delimiter, since I don’t want to escape “/” in the regex