Scripts and Code Snippets

From Colettapedia
Jump to: navigation, search

Bash scripts

Bash one-liners

Count all the tif files in all the subdirectories

for i in *; do 
    if [[ -d $i ]]; then 
        echo $i
        ls ${i}/*.tif | wc
    fi
done
  • note the space between if and [[

VirtualBox: mount virtual shared folder

mount -t vboxsf virtual_shared_folder /home/boincclient/Desktop/virtual_shared_folder

ImageMagick: Crop out the center of all the images in a directory

for i in ../../originals/heads/*.tif; do 
    echo $i
    BASENAME=`echo $i | sed -r "s/.tif$//g"`
    convert "$i" -gravity Center -crop 90x90+0+0 +repage "${BASENAME}_cropped.tif"
    mv -v "${BASENAME}_cropped.tif" .
done

Go into each subdirectory and output the first 10 images in each

for i in *; do 
    echo $i; 
    count=0; 
    for j in $i/*.tiff; do 
        if (( $count < 10 )); then 
            echo "$count $j";
        fi; 
        count=$(($count+1)); 
    done; 
done


ImageMagick: Convert all tiffs in this dir to 1 channel grayscale

for i in *.tif; do 
    BASENAME=`echo $i | sed -r "s/\.tif$//"`;
    convert "$i" -colorspace Gray -flatten +matte "${BASENAME}_gray.tif";
done

Use egrep to output only certain stuff from a tiffdump

  • for i in *; do ls -lh $i; tiffdump $i | egrep 'ImageWidth|ImageLength'; done

show files in this dir from largest to smallest

  • ls -lhS
  • USE CAPITAL S

Take the first n objects from a directory and copy them elsewhere

#!/bin/bash

for dir in `find ../josiah_worms -type d`; do
	echo $dir
	name=`echo $dir | gsed -r "s/^.*\///"`
	mkdir $name
	count=0
 	for tiff in `find $dir -name "*.tiff"`; do
		if [[ $count -lt 3 ]]; then
			echo $tiff
			count=$(( $count + 1 ))
			basename=`echo $tiff | gsed -r "s/\.tiff//"`
			cp -v ${basename}.* $name
			cp -v ${basename}_* $name
		fi
	done
done

Delete all files for which there isn't some accompanying file

#!/bin/bash
for sigfile in `find . -name "*.sig"`; do
   basename=`echo $sigfile | sed -r "s/-.+\.sig//"`
   echo $basename
   if [ ! -e ${basename}.tif ]; then
     echo "no tiff for sigfile $sigfile"
     rm -v $sigfile
   fi
done

SVN checkout

  • Sequentially check out code from every day and do a line count on the project
    • requires CLOC perl script and gnu date
#!/bin/bash

for i in {1..221}
do
	the_date=`date  +"%Y-%m-%d" -d "2012-06-27 +$i day"`
	echo $the_date
	svn co -r {$the_date} http://wnd-charm.googlecode.com/svn/pychrm/trunk pc_trunk
	svn info pc_trunk | grep Revision > ${the_date}.txt
	cloc --csv pc_trunk >> ${the_date}.txt
	rm -rf pc_trunk
done

Automator script

for i in "$@"
do
        echo "processing $i" >> ~/local_watch_folder.log
        scp $i lgchrm19:/home/eckleyd/RealTimeClassification/
        BASENAME=$(basename "$i")
        echo "basename is $BASENAME" >> ~/local_watch_folder.log
        ssh lgchrm19 "sh -c 'cd /home/eckleyd/RealTimeClassification/; nohup ./ScorePharynxBalanced.py $BASENAME > $BASENAME.log 2>&1 < /dev/null&'"
		mv $i /Users/eckleyd/Documents/RealTimedone
done

Count lines of files in list

  • cat 20180727_filelist.txt | xargs -I {} sh -c "echo {}; zcat {} | wc -l" | tee linecounts.txt

Combine FASTQ files

COUNTER=1
for THISDIR in $(cat 20180727_dirlist.txt); do
        cd $THISDIR
        THESEARGS=$(ls -1 | paste -sd " " -)
        #CMD=$(printf "cat %s > %s.fastq.gz" "$THESEARGS" $THISDIR)
        #CMD=$( echo "cat $THESEARGS > $THISDIR.fastq.gz" )
        #CMD=$( echo "pv -B 100m $THESEARGS > $THISDIR.fastq.gz" )
        #DBG_MSG=$(printf "%3d: \"%s\"\n" $COUNTER "$CMD")
        #echo $DBG_MSG
        #`$CMD`
        printf "%3d" $COUNTER
        pv -B 100m $THESEARGS > $THISDIR.fastq.gz
        cd ..
        COUNTER=$(( $COUNTER + 1))
done

Perl

Javascript

C++

  • Increment after assignment
#include <iostream>
#include <vector>

using namespace std;
int main() {

	vector<double> array (10, 10);

	vector<double>::iterator it = array.begin();

	*it++ = 20.0;
	*it++ = 30.0;
	*it++ = 40.0;

	vector<double>::iterator other_it = array.begin();

	for( ; other_it != array.end(); ++other_it ) 
		cout << *other_it << endl;
	
	return 0;

}

outputs:

chris@dk8rb2a19maclt2 ~
$ ./test_iterator 
20
30
40
10
10
10
10
10
10
10
  • fake_signature.cpp - using GNU scientific library random number generator, writing files, and directory manipulation in POSIX environment
  • dump some stuff out into a time stamped file
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <time.h>
#include <sys/time.h>

time_t ltime;
struct tm *Tm;
struct timeval detail_time;
 
ltime=time(NULL);
Tm=localtime(&ltime);
 
		std::ostringstream filename;
		filename << (Tm->tm_year+1900) << '-' 
			<< setw(2) << setfill('0') << (Tm->tm_mon+1) << '-' 
			<< setw(2) << setfill('0') << Tm->tm_mday << "_" 
			<< setw(2) << setfill('0') << Tm->tm_hour <<  ':'
			<< setw(2) << setfill('0') << Tm->tm_min << ':'
			<< setw(2) << setfill('0') << Tm->tm_sec << ".";
		gettimeofday(&detail_time,NULL);
		filename << setw(2) <<  detail_time.tv_usec / 1000;
		filename << "pixel_dump.txt";
		std::cout << filename.str() << std::endl;
		ofstream pixel_dump_file ( filename.str().c_str(), ios::app );

		int count = 0;
		int numpix = width*height*depth;
		while (count < numpix) {
			pixel_dump_file << data[count].intensity << std::endl;
			++count;
		}
		pixel_dump_file.close();

Python

from subprocess import call
with open( "universal5.txt") as file:
	for line in file:
		universal_line = line.replace( " (active)", "" ).strip()
		no_u_line = universal_line.replace( "+universal", "")
		no_u_line = "sudo port activate {}".format( no_u_line )
		call( no_u_line, shell=True )
		universal_line = "sudo port uninstall {}".format( universal_line )
		print universal_line
		call( universal_line, shell=True )

TeX