Friday, 14 December 2012

multi threading with sock

#!/bin/bash
rm -rvf fol fol1
mkdir fol fol1
j=0
l=0
echo 0 > /tmp/i
cop()
{
until [ -f /tmp/sock ]
do
i=`cat /tmp/i`
for((i=$i;i<50;i++))
do
touch /tmp/sock
cp -rvf pub fol/test$i
sleep 3
i=$(($i+1))
echo $i > /tmp/i
break
done
rm -rvf /tmp/sock
done
}
thread()
{
if [ `ps aux | grep $0 | grep -v grep | grep -v vim | grep -v watch | wc -l` -le 6 ]
then
cop $j&
sleep 1
break
fi
}
for((k=0;k<50;k++))
do
while [ $l -le 45 ]
do
thread &
sleep 1
l=`cat /tmp/i`
done
if [ $l -gt 49 ]
then
exit 4
fi
done

Monday, 10 December 2012

Multithread v1

#!/bin/bash
dd if=/dev/zero of=~/pub bs=1M count=50
rm -rvf fol fol1
mkdir fol fol1
for i in $(seq 50)
do
cp -rvf pub fol/test$i
done
copy()
{
ls fol/ | grep -v gz$ | paste - | while read a
do
sleep 10
gzip fol/$a
sleep 1
mv -v fol/$a.gz fol1/
sleep 1
break
done
}
thread()
{
if [ `ps aux | grep $0 | grep -v grep | wc -l` -le 17 ]
then
$1 &
sleep 1
fi
}
while [ `ls fol/ | paste - | wc -l` -gt 0 ]
do
thread copy &
sleep 1
done


To check multi thread :

watch -n 0 "ps aux | grep start | grep -v grep "

Sunday, 18 November 2012

Reversing file content

co() { max=`cat $1 | wc -l`; for((i=$max;i>0;i--));
do
 cat $1 | gawk 'NR=='$i' {print}'
 done
 }


co filename

Saturday, 27 October 2012

checking more space consuming parttion

df -h | sed -e 's/%//g' | grep -v Mounted | gawk '{if($5>10) {print  $0}}'

Finding more consumption time

ps aux  | gawk 'BEGIN{FS=" ";RS="\n"} {if($3>0) {x+=$3}} END{if(x>58) {print "System is using more then the limit"}}'

Monday, 22 October 2012

File check up code

#!/bin/bash
ls -ltr | awk '{print $NF}' | while read a
do
if [ -f $a ]
then
echo $a >> /tmp/file
elif [ -d $a ]
then
echo $a >> /tmp/directory
else
echo "this is not a file"
fi
done

sorting file by month

#!/bin/bash
s=( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec )
for((i=0;i<=${#s[@]};i++))
do
ls -ltr | gawk 'BEGIN{FS=" ";RS="\n";IGNORECASE=1;OFS="#"} {if($6=="'${s[i]}'") {print $6,$NF}}'
sleep 1
done


[praveensam.s@bar ~]$ sh -x awktest
+ s=(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
+ (( i=0 ))
+ (( i<=12 ))
+ ls -ltr
+ gawk 'BEGIN{FS=" ";RS="\n";IGNORECASE=1;OFS="#"} {if($6=="Jan") {print $6,$NF}}'
Jan#DBAN_LIST.xls
Jan#praveensam.s.xls
Jan#saron
+ sleep 1
+ (( i++ ))
+ (( i<=12 ))
+ ls -ltr
+ gawk 'BEGIN{FS=" ";RS="\n";IGNORECASE=1;OFS="#"} {if($6=="Feb") {print $6,$NF}}'
Feb#crypto-146
Feb#1.1.2
Feb#invite.ics
Feb#Template-1.doc
Feb#xf86-input-acecad-1.0.0.5.tar.bz2
Feb#xf86-video-intel-2.17.0.tar.bz2
Feb#xorg-server-1.6.2.tar.bz2
Feb#glproto-1.4.11.tar.bz2
Feb#util-macros-1.4.2.tar.bz2
Feb#glproto-1.4.10.tar.bz2
Feb#glproto-1.4.14.tar.bz2
Feb#mysql_lvm_backup-1.0.0.zsh
Feb#adwords-editor-en-US.msi
Feb#wine-1.1.27.tar.bz2
Feb#Gpp_Installation.doc
Feb#Revised.ppt
Feb#Python-2.6.1.tar.bz2
Feb#boost_1_44_0.tar.bz2
Feb#Makefile

Saturday, 20 October 2012

Getting a word one by one

 ls -lrt | gawk 'BEGIN{FS=" ";RS="\n";IGNORECASE=1} {for(i=0;i<=NF;i++) {if(i!=0) {print $i}}}'

praveensam.s
prodsupport
4096
Oct
19
06:12
GDP-2.5.1
-rwxr-xr-x
1
praveensam.s
prodsupport
302
Oct
19
08:16
gdp

Killing over process consuming vlc automatically

while true > /dev/null; do ps aux | grep -i vlc | gawk '{IGNORECASE=1} {if(($3>6)&&($0~/robot/)) {system("kill -9 "$2)}}'; done &

Friday, 19 October 2012

gawk with finding a line end with a word

ll | gawk '{if($8~/..*s$/) {print }}'
-rw-r--r--  1 sam  sam       127 2012-10-20 09:51 .gtk-bookmarks
-rw-------  1 sam  sam     16955 2012-10-20 11:00 .xsession-errors
sam@sam-Lenovo-G450:~$

Finding nth occurance of the particular word

sam@sam-Lenovo-G450:~$ cat sam | gawk '{if($2==2) {x++; if(x==6) {print $0}}}'
drwxr-xr-x  2 sam sam     4096 2012-09-27 22:53 Documents

watching news using awk

lynx -dump www.google.com | gawk 'BEGIN{IGNORECASE=1} {if(($0~/news/)&&($0~/http/)) {print $2}}' | xargs lynx -dump | sed -n '/Top Stories/,/References/p'

Daily News & Analysis
   See realtime coverage

[47]Suspension of Kingfisher licence on cards: DGCA officials


   Daily News & Analysis - <U+200E>2 hours ago<U+200E>

   Suspension of licence stares in the face of crisis-ridden Kingfisher
   Airline as it extended its lockout till October 23 and submitted a
   reply to aviation regulator DGCA's show-cause notice on the matter.
   Related[48] Kingfisher Airlines » [49]Aviation »
   [50]Kingfisher to stay grounded till NovemberThe Hindu
   [51]Suspension of Kingfisher licence on cards: OfficialsDeccan Herald
   In-depth:[52]How Mallya killed KingfisherTehelka
   [53]See all 70 sources »
   Related[54] Kingfisher Airlines » [55]Aviation »
   IBNLive
   See realtime coverage

[56]Digvijaya slams 'self-serving, ambitious' Kejriwal, says he has Hitler's
'streak'


   Hindustan Times - <U+200E>1 hour ago<U+200E>

   Arvind Kejriwal is a "self-serving ambitious megalomaniac" with a
   "streak" of Hitler, Congress general secretary Digvijaya Singh alleged
   on Friday in the sharpest attack on the activist.
   Related[57] Anna Hazare » [58]Arvind Kejriwal » [59]Sonia Gandhi »
   [60]Digvijay 'rattled' Congress' last resort: KejriwalDaily News &
   Analysis
   [61]Diggy's salvo: Should Kejriwal dignify him with a
   response?Firstpost
   Highly Cited:[62]Anna Hazare to expand his core teamIndian Express
   Wikipedia:[63]Arvind Kejriwal
   [64]See all 70 sources »
   Related[65] Anna Hazare » [66]Arvind Kejriwal » [67]Sonia Gandhi »
   IBNLive

awk + split

sam@sam-Lenovo-G450:~$ cat sam
echo hi
hi this is sam
hare u getting ma msg
if it so please ping reply

-------------------------------------------------------------------------------------------

sam@sam-Lenovo-G450:~$ cat sam | gawk '{IGNORECASE=1} {c=split($0,s) ; for(i=0;i<=c;i++) {printf s[i]"\n" }}'

echo
hi

hi
this
is
sam

hare
u
getting
ma
msg

if
it
so
please
ping
reply

---------------------------------------------------------------------------------------------------

word grep

cat sam | gawk '{IGNORECASE=1} {c=split($0,s) ; for(i=0;i<=c;i++) {if(s[i]=="msg") {print $0}}}'

awk with for

sam@sam-Lenovo-G450:~$ ls -ltr | gawk 'BEGIN{FS=" ";RS="\n";IGNORECASE=1} {for(i=0;i<=NF;i++) {if((i==7)||(i==4)) {print $0,i}}}'
drwxr-xr-x  9 sam sam     4096 2012-09-06 04:52 firefox 4
drwxr-xr-x  9 sam sam     4096 2012-09-06 04:52 firefox 7
-rw-r--r--  1 sam sam      179 2012-09-27 22:48 examples.desktop 4
-rw-r--r--  1 sam sam      179 2012-09-27 22:48 examples.desktop 7
drwxr-xr-x  2 sam sam     4096 2012-09-27 22:53 Desktop 4
drwxr-xr-x  2 sam sam     4096 2012-09-27 22:53 Desktop 7
drwxr-xr-x  2 sam sam     4096 2012-09-27 22:53 Videos 4
drwxr-xr-x  2 sam sam     4096 2012-09-27 22:53 Videos 7
drwxr-xr-x  2 sam sam     4096 2012-09-27 22:53 Templates 4
drwxr-xr-x  2 sam sam     4096 2012-09-27 22:53 Templates 7
drwxr-xr-x  2 sam sam     4096 2012-09-27 22:53 Public 4
drwxr-xr-x  2 sam sam     4096 2012-09-27 22:53 Public 7
drwxr-xr-x  2 sam sam     4096 2012-09-27 22:53 Pictures 4
drwxr-xr-x  2 sam sam     4096 2012-09-27 22:53 Pictures 7
drwxr-xr-x  2 sam sam     4096 2012-09-27 22:53 Documents 4
drwxr-xr-x  2 sam sam     4096 2012-09-27 22:53 Documents 7
-rw-r--r--  1 sam sam   213721 2012-09-28 09:22 miami-beach.jpg 4
-rw-r--r--  1 sam sam   213721 2012-09-28 09:22 miami-beach.jpg 7
-rw-r--r--  1 sam sam 19280364 2012-09-28 10:23 firefox-15.0.1.tar.bz2 4
-rw-r--r--  1 sam sam 19280364 2012-09-28 10:23 firefox-15.0.1.tar.bz2 7
-rw-r--r--  1 sam sam     5712 2012-09-28 12:50 test 4
-rw-r--r--  1 sam sam     5712 2012-09-28 12:50 test 7
-rw-r--r--  1 sam sam  4357631 2012-09-29 11:23 Firefox_wallpaper.png 4
-rw-r--r--  1 sam sam  4357631 2012-09-29 11:23 Firefox_wallpaper.png 7
drwxr-xr-x 22 sam sam     4096 2012-10-03 20:11 Music 4
drwxr-xr-x 22 sam sam     4096 2012-10-03 20:11 Music 7
drwxrwxr-x  2 sam sam     4096 2012-10-03 23:20 Ubuntu One 4
drwxrwxr-x  2 sam sam     4096 2012-10-03 23:20 Ubuntu One 7
-rw-r--r--  1 sam sam      391 2012-10-08 14:14 Untitled Document 1 4
-rw-r--r--  1 sam sam      391 2012-10-08 14:14 Untitled Document 1 7
-rwxr-xr-x  1 sam sam        8 2012-10-08 18:33 sam 4
-rwxr-xr-x  1 sam sam        8 2012-10-08 18:33 sam 7
drwxr-xr-x  3 sam sam     4096 2012-10-14 22:14 Downloads 4
drwxr-xr-x  3 sam sam     4096 2012-10-14 22:14 Downloads 7

Monday, 15 October 2012

file replace

#!/bin/bash
pycon()
{
lynx -dump -auth=prodsupport:prodsupport http://ipaddress/Repos/ | awk '/http/{print $2}' | grep -i $1 | xargs wget
y=$(lynx -dump  -auth=prodsupport:prodsupport http://ipaddress/Repos/ | awk '/http/{print $2}' | grep -i $1)
tar -zxvf  $(basename $y)
z=$(basename $y)
cd $(tar -ztvf $z | awk 'NR==1 {print $NF}')
$python_path setup.py {build,install}
}
py zsi
#!/bin/bash
pyfile()
{
lynx -dump -auth=prodsupport:prodsupport http://ipaddress/Repos/ | awk '/http/{print $2}' | grep -i $1 | xargs wget
y=$(lynx -dump  -auth=prodsupport:prodsupport http://ipaddress/Repos/ | awk '/http/{print $2}' | grep -i $1)
mv -vf  $(basename $y) $2/
}
#!/bin/bash
echo "python"
read pyth
pytar()
{
lynx -dump http://ipaddress/Repos/ | awk '/http/{print $2}' | grep -i $1 | xargs wget
y=$(lynx -dump http://ipaddress/Repos/ | awk '/http/{print $2}' | grep -i $1)
tar -zxvf  $(basename $y)
z=$(basename $y)
cd $(tar -ztvf $z | awk 'NR==1 {print $NF}')
$pyth setup.py install 
}
py zsi
#!/bin/bash
contar()
{
lynx -dump http://ipaddress/Repos/ | awk '/http/{print $2}' | grep -i $1 | xargs wget
y=$(lynx -dump http://ipaddress/Repos/ | awk '/http/{print $2}' | grep -i $1)
tar -zxvf  $(basename $y)
z=$(basename $y)
cd $(tar -ztvf $z | awk 'NR==1 {print $NF}')
./configure
make
make install
}

Awk Rules Sample


  awk 'NF != 0 {++count} END {print count}' list
 
 

* Awk is useful for performing simple iterative computations for which a more sophisticated language like C might prove overkill. Consider the Fibonacci sequence:

   1 1 2 3 5 8 13 21 34 ... 
 
 
 
   awk 'BEGIN {a=1;b=1; while(++x<=10){print a; t=a;a=a+b;b=t}; exit}'
This generates the following output data:

   1
   2
   3
   5
   8
   13
   21
   34
   55
   89 
 
* Sometimes an Awk program needs to be used repeatedly. In that case, it's simple to execute the Awk program from a shell script. For example, consider an Awk script to print each word in a file on a separate line. This could be done with a script named "words" containing:

   awk '{c=split($0, s); for(n=1; n<=c; ++n) print s[n] }' $1 
 
 
The method for achieving this is also simple, and involves using a variable named "skip". This variable is set to "1" every time a blank line is skipped, to tell the Awk program not to skip the next one. The scheme is as follows:

   BEGIN {set skip to 0}
   scan the input:
      if skip == 0    if line is blank
                         skip = 1
                      else
                         print the line
                      get next line of input
      if skip == 1    print the line
                      skip = 0
                      get next line of input
This translates directly into the following Awk program:

   BEGIN      {skip = 0}
   skip == 0  {if (NF == 0) 
                {skip = 1} 
               else 
                {print}; 
               next}
   skip == 1  {print; 
               skip = 0;
               next}
 
 
My awk try :
 
 ls | gawk '{for(i=0;i<=NF;i++) {if(i==0) {print $i}}}'
 
 


 
 
 
 

Shell array

Take, for example, the array definition below:
names=( Jennifer Tonya Anna Sadie )
The following expression evaluates into all values of the array:
${names[@]}
It also can be used anywhere a variable or string can be used.
A simple for loop can iterate through this array one value at a time:
for name in ${names[@]} do
echo $name
# other stuff on $name
done