16 Juni 2010

some useful daily scripts

Last update: 13 Feb 2011
I wrote several simple scripts to manipulate the content of files. Most of them are written in bash, but due to my lack of expertise sometimes I mixed bash with make, and perl. It will be very practical.. almost no explanation, but I'll try to gradually improve this article. Here are they:

  1. Find the files with specific content
    find . -type f -name "*.bash" -print0|xargs -0  grep -ne "keyword"


  2. Change certain content of files in batch manner. Just copy these script into a command.bash, and change the permission then run it (command filenamepattern old new)
    ./command.bash "tt*" tir ti
    The script:
    #!/bin/bash
    
    TMPDIR=/tmp
    
    fname=${1}
    old=${2}
    new=${3}
    tmpfile=${TMPDIR}/$$.tmp
    #echo "ls ${fname}"
    for mydataset in `ls ${fname}`; do
    echo "${mydataset}"
    cat ${mydataset} | sed -e "s/${old}/${new}/g" > ${tmpfile}
    mv ${tmpfile} ${mydataset}
    done
    
    
    


  3. Delete a bunch of file
    If you have thousand of files, rm * will not work, therefore here is an alternative. Create a makefile with the following entry and call make clean.
    clean:
    find . -name "*.log" -print -exec rm {} \;
    
    
    

  4. Creating tags for emacs
     find . -name "*.[chCH]" -print | etags -

    In case you have only few files
     etags file1 file2 ...
  5. Removing Hijriah calendar from islamicfinder.com praying timetable :). Often the hijriah date make me confuse and pick a wrong time. I do it in vi using this search and replace command.
    :%s/[0-9]*\/[0-9]*\t//g

  6. Cleaning carriage return/line feed (crlf) but left double crlf. I use this for formatting my latex style text file (which uses crlf extensively) before I upload it into blogger.
    cat fileinput |sed -n 'H;${g;s/\n/crlf/g;p}'| sed -n 'H;${g;s/crlfcrlf/\n\n/g;p}'| sed -n 'H;${g;s/crlf/ /g;p}' > file output

  7. Resize pictures/images, 1024 is the width
    mogrify -resize 1024 *.jpg
  8. Montage/combine/collate pictures into one
    montage *.jpg -tile 1x2 -geometry 1024 combine.jpg
  9. Convert  images into pdf
     mogrify -format pdf *.jpg
  10. Download Al-Quran ayats.
    #usage:
    # want to download albaqarah ayat 1 to 10
    # to download other ayat you must change this script accordingly
    # just to list the download commands
    #$ bash dlayat.sh 1 10
    # actually download
    #$ bash dlayat.sh 1 10 download
     
    i="${1}"
    j="${2}"
    download=0
    if [ "${3}" == "download" ]
    then
        download=1
        echo download
    fi
    
    while [ $i -le  $j ] 
    do
    padd=""
    if [ $i -lt 100 ] 
    then
     padd="0"
    fi
    if [ $i -lt 10 ]  
    then
     padd="00"
    fi
    
    if [ $download -eq 1 ]
    then
        wget http://www.everyayah.com/data/Ghamadi_40kbps/002${padd}${i}.mp3
    else
        echo wget http://www.everyayah.com/data/Ghamadi_40kbps/002${padd}${i}.mp3
    fi
    i=$[$i+1]
    done
    
  11. Converting raw images into jpg in batch manner
    #change the raw file extension accordingly
    thepath=${1}
    
    for file in `ls ${thepath}*.CR2`; do
        ufraw-batch --out-type=jpg ${file}
    done
    
  12. To be continued..

Tidak ada komentar: