Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Saturday, April 16, 2011

Replacing a path in all files in a directory tree

Sometimes it is necessary to change a hardcoded path in all files in a given directory tree (i.e. if you migrated to a new home folder and need to change the paths to your libraries in all of your scripts).

This can be achieved extremely easily with some bash magic:

find . -type f | grep $PATTERN | xargs perl -pi -e "s|$OLD_PATH|$NEW_PATH|"

Tuesday, November 23, 2010

Passing shell variables to AWK

If you have a shell variable in a bash script you can't pass it to AWK just by putting "$" sign in front of it, but you can enclose them with "'" in AWK code and they will be used in AWK with no problem.

for example you have a bed file called "example.bed":

$ cat example.bed
chr1     1000     2000   id1
chr1     4000     5000   id2
chr1     5500     6000   id3

Let's say you want to concatenate a string (in this case "brain_" string) to column 4 of this file, you can do this in AWK as follows:

$ awk '{OFS="\t";$4="brain_"$4; print;}' example.bed
chr1     1000     2000   brain_id1
chr1     4000     5000   brain_id2
chr1     5500     6000   brain_id3

however if you store the string in a variable as follows in the terminal or in a bash script:

$ TISSUE="brain_"

the following will not work,
$ awk '{OFS="\t";$4=$TISSUE$4; print;}' example.bed

 but this will :

$ awk '{OFS="\t";$4="'"$TISSUE"'"$4; print;}' example.bed
chr1     1000     2000   brain_id1
chr1     4000     5000   brain_id2
chr1     5500     6000   brain_id3

check out for details and other ways to do this at:
http://www.tek-tips.com/faqs.cfm?fid=1281

Friday, November 19, 2010

Sending e-mail from command line (terminal) in unix/linux

Useful for people who want to have status updates on their scripts. You can send e-mails from unix terminal. I use it to report status on the scripts I'm running. If they are successfully executed or crashed, for example.

mail -s "script finished" fool@bs.com < file.txt
The line above sends a mail to fool@bs.com titled "script finished" and with the contents of file.txt. You can omit the file.txt part and send a small piece of content using "echo" and "|", like this:
echo "ABYSS 3rd  run finished" | mail -s "ABYSS run" fool@bs.com

this sends "ABYSS 3rd run finished" as the content of the e-mail.

Getting nth line in a file

Ever wondered what is the nth line of a file without using a text editor ?
Here is something you may use in a unix environment.

sed -n '5 p' file1.txt

this sed one-liner outputs the 5th line of the file1.txt