Skip to content

Instantly share code, notes, and snippets.

@faskowit
Created March 24, 2020 17:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faskowit/29f79d3149469779cbff71eebc49340a to your computer and use it in GitHub Desktop.
Save faskowit/29f79d3149469779cbff71eebc49340a to your computer and use it in GitHub Desktop.
get some columns from fmriPrep's output using bash script
#!/bin/bash
# columns to extract
colterms="csf white_matter global_signal trans_x trans_y trans_z rot_x rot_y rot_z"
printcol()
{
local FILE=${1}
local COLUMNTITLE=${2}
# check if even there
local ccc=$(head -1 $FILE | grep $COLUMNTITLE)
if [[ -z $ccc ]] ; then
>&2 echo "$ccc not in file. error."
rm -v $tmp_file_1
rm -v $tmp_file_2
rm -v $tmp_file_3
exit 1
fi
cut $FILE -f `head -1 $FILE | tr "\t" "\n" | grep -n "^$COLUMNTITLE"'$' | cut -f 1 -d :`
}
inputfile=$1
outfile=$2
cols=$3
if [[ -z $outfile ]] || [[ -z $inputfile ]] ; then
echo "USAGE: $0 inputfile outputfile [optional string of columns to extract, use quote around whole string]"
exit 1
fi
if [[ -e $outfile ]] ; then
echo "$outfile already exists. please delete"
exit 1
fi
if [[ ! -z $cols ]] ; then
colterms=$cols
fi
################################################################################
tmp_file_1=$(mktemp /tmp/getcols.XXXXXX)
tmp_file_2=$(mktemp /tmp/getcols.XXXXXX)
tmp_file_3=$(mktemp /tmp/getcols.XXXXXX)
> $tmp_file_1
for CC in $colterms ; do
printcol $inputfile $CC > ${tmp_file_2}
if [[ -s ${tmp_file_1} ]] ; then
paste -d',' ${tmp_file_1} ${tmp_file_2} > ${tmp_file_3}
mv $tmp_file_3 $tmp_file_1
else
mv $tmp_file_2 $tmp_file_1
fi
done
mv $tmp_file_1 $outfile
#rm -v $tmp_file_1
rm -v $tmp_file_2
#rm -v $tmp_file_3
@Qiuishere
Copy link

Hi! Thanks for this script! I just want to how to use it? Is it a function with 3 input? what exactly shoud they be?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment