Archive for the ‘ Linux ’ Category

#ShellScript Split String

Whitespace

 
$kata = "nur aini" 
arr=($kata) 
echo ${arr[1]} 

Tab

$kata = "nur\taini"
arr=(${kata//'\t'/ })
echo ${arr[1]}

Comma

 $kata = "nur,aini" 
arr=(${kata//','/ }) 
echo ${arr[1]} 

#Perl Parsing CSV

Today, I encounter a CSV parsing problem. The CSV file contains a comma inside double quote (").
for instance,
"Nur,Aini",1,2
The desired output parsing is
"Nur,Aini" 1 2
Here is a perl snippet to parse the above CSV file
perl -MText::ParseWords -nle' @kata=parse_line(",", 1, $_)
$|=1
foreach(@kata) {
printf ("%s\t",$_)
}
print "\n" ' filename

#ShellScript Timeout Snippet

Just stick a great snippet that implements Timeout in the shellscript command 🙂

timeout=5                       # in seconds
ping 127.0.0.1 & cmdpid=$!      # Command to terminate
# Start "watchdog" process to terminate the command
# after $timeout seconds:
(sleep $timeout; kill -9 $cmdpid) &
watchdogpid=$!
wait $cmdpid                    # wait for command
kill $watchdogpid >/dev/null 2>&1

Courtesy : http://www.oase-shareware.org/shell/scripts/quickies/timeout

GetOpts Parsing Options for #Shellscript

The following lines is a snippet that show how to parse the option parameters in the shell script command ./foo -B -S -i /tmp -o /tmp/out -n 100


BIGLITERAL=no
SF=no
input=""
output="out"
numberofqueries=2
set -- $(getopt BSi:o:n: "$@")
while [ $# -gt 0 ]
do
case "$1" in
(-B) BIGLITERAL=yes;;
(-S) SF=yes;;
(-i) input="$2"; shift;;
(-o) output="$2"; shift;;
(-n) numberofqueries="$2"; shift;;
(--) shift; break;;
(-*) echo "$0: error - unrecognized option $1" 1>&2; exit 1;;
(*)  break;;
esac
shift
done

echo $input
echo $output
echo $BIGLITERAL
echo $numberofqueries

if [[ $input == "" ]] || [[ ! -d $input ]]
then
echo "$0: error "
echo "no input"
exit 1
fi

Convert Speech To Text in One line script

Transcribing a long audio file is a bit irritating. Nevertheless, it is not hard anymore 😛 We can do it with one following shell script line:

curl -F file=@one.flac -H "Content-Type: audio/x-flac; rate=44100" http://www.google.com/speech-api/v1/recognize?lang=en

note : one.flac is the file name, you can change it with your filename. if you need a sample audio for testing purpose only, you can download my audio in here. you have to wait for about 10 seconds (depends on your network speed)

Here is the result :
{"status":0,"id":"a029ad14d62be3dbaa814647f0672c50-1","hypotheses":[{"utterance":"I am a mother of two girls","confidence":0.8087615}]}

[ShellScript] Count Number of Lines of GZ file

zgrep -Ec "$" file.gz

zcat file.gz | wc -l

Merge multiple Avi Files

#!/bin/bash
avi=""
for j in $1/*.avi
do
echo "adding file $j to the list"
avi="$avi $j"
done

avimerge -i $avi -o output.avi

note :
you must install transcode-utils

sudo apt-get install transcode-utils

The result :