Showing posts with label shell script. Show all posts
Showing posts with label shell script. Show all posts

Tuesday, September 8, 2009

how to use awk | substr

hi all,

herewith i have a flat file (test.txt) below :

test.txt :

CTI-ProgramStart|VVV2|Program Start|VKGXXXXXXXX|VKGXXXXXX|06/01/22 00:33:49.467|WSSACCCXXXX|VVV21137XXX3|B Program Version:.6.4.5 AgentID:2247 Password: Signature: Extention:7950 PeripheralID:2 SideAHost:109.XX.XX.XX SideBHost:192.XX.XX.XX SideAPort:42027 SideBPort:5874 HEARTBEATINTERVAL:-1 EXPECTEDCLIENTS:150 QUEUESIZE:32 POOLSIZE=64 SERVICESMASK:-214749088 EVENTSMASK:311071 STATUSMASK:1031 TRACEMASK:5|

i would like to get ext = 7950

BEGIN {
FS="[|]"
}

$1 == "CTI-ProgramStart" {
if( match($9, /Extention:[0-9][0-9]*/) ) {
str=substr($9, RSTART, RLENGTH)
ext=substr(str, index(str, ":")+1)
print ext
}

}


Note : u have to read the flat file fisrt before insert the algorithm.

how to display date yesterday at unix

if u want to display date yesterday at unix with format YYYYMMDD, you can try with my script below :

if u type at unix shell with syntac date, so the result is below :
bash-3.2$ date
Tue Aug 18 08:41:44 wib 2009

we can change the format as our need. example i need format yyyymmdd.
please make script below and save to tanggal_hari_ini.sh

run as below

sh tanggal_hari_ini.sh

Input Script :

#!/usr/bin/bash
# bof date
month=`date +%m`
day=`date +%d`
year=`date +%Y`
month=`expr $month + 0`
day=`expr $day - 1`
if [ $day -eq 0 ]; then
month=`expr $month - 1`
if [ $month -eq 0 ]; then
month=12
day=31
year=`expr $year - 1`

else
case $month in
1|3|5|7|8|10|12) day=31;;
4|6|9|11) day=30;;
2)
if [ `expr $year % 4` -eq 0 ]; then
if [ `expr $year % 400` -eq 0 ]; then
day=29
elif [ `expr $year % 100` -eq 0 ]; then
day=28
else
day=29
fi
else
day=28
fi
;;
esac
fi
fi
case $month in
[0-9]) month="0$month";;
esac
case $day in
[0-9]) day="0$day";;
esac
yesterday=$year$month$day
echo $yesterday
# eof date
exit 0


output script : 20090818

Monday, September 7, 2009

swap columns and replace

hi, any problem manipulating (swap coloumn n replace)
example :

File sebelum.txt

1A6Y173BPHE-75.1167.5
1A6Y174BGLY58.7155.7
1A6Y175BARG-18.1142.6
1A6Y176BILE-72.6136.1
1A6Y177BPRO-68.1-136.2
1A6Y178BLYS-162.6360.0

sould be :

File sesudah.txt

1A6YB173PHE-75.1167.5
1A6YB174GLY58.7155.7
1A6YB175ARG-18.1142.6
1A6YB176ILE-72.6136.1
1A6YB177PRO-68.1-136.2
1A6YB178LYS-162.6360.0

you can do it with this script

cat sebelum.txt awk 'BEGIN { FS=""; OFS="" } {$a=$2 $2=$3; $3=$a; print $0 } ' >> sesudah.txt


Note :

FS = Field Sparator
OFS = Output Field Sparator