Search This Blog

Friday, May 08, 2015

Use variable in search pattern in awk

Passing a variable to awk and using it in the search pattern.



awk -v string="$STRING" -f  script.awk  [filename]

Script extract:

$0 ~ string {
      
       print $0;
}

Note:

        On Solaris use nawk.

Tuesday, April 14, 2015

Match IP numbers with regex.


Print any IP address talking on port 21 in coming or out going.
Change the port number to any port that needs to be matched.

netstat -na | perl -ne '/((\d{1,3}\.){4}21)/ &&  print ;'
 

Monday, January 26, 2015

Variable assigments

Validating positional parameters.

The below example will print a usage message if there is no 2nd position paramaeter.

PACKAGE=${2:?$(usage ${SERVERLIST} [Package name:Version:Package file[:Dependent service]])}

Wednesday, August 06, 2014

awk and ifconfig

Print interface name and network config oneliner.


#ifconfig -a | awk '/^[a-z]/{intf=sprintf("%-10s", $1);getline; if($1=="inet") printf "%-6s %s\n", intf, $0;}'
          inet addr:172.19.2.26  Bcast:172.19.2.255  Mask:255.255.255.0
          inet addr:127.0.0.1  Mask:255.0.0.0

Wednesday, October 16, 2013

Colourise  your shell scripts
#!/usr/bin/ksh
#----------------------------------------------------------------------------------------------
#                       Define Colours for scripts.
#
#       Written : Stan Lovisa
#       Date    : 26-10-2012
#       Mod     :
#
# Black         0;30     Dark Gray      1;30
# Blue          0;34     Light Blue     1;34
# Green         0;32     Light Green    1;32
# Cyan          0;36     Light Cyan     1;36
# Red           0;31     Light Red      1;31
# Purple        0;35     Light Purple   1;35
# Brown         0;33     Yellow         1;33
# Light Gray    0;37     White          1;37
# 0 normal, 1 bold, 4 underline, 7 reverse video
#----------------------------------------------------------------------------------------------
#
#       Notes. How to use.
#
#       printf "${TXTGREEN}"    To switch on colour.
#       printf "${TXTNC}"       To switch off colour.
#
#----------------------------------------------------------------------------------------------
TXTRED="\033[1;31m"
TXTREDRV="\033[7;31m"
TXTLBLUE="\033[1;34m"
TXTBLUERV="\033[7;36m"
TXTGREEN="\033[0;32m"
TXTGREENRV="\033[7;32m"
TXTYELLOW="\033[1;33m"
TXTYELLOWRV="\033[7;33m"
TXTNC="\033[1;0m"




Thursday, July 11, 2013

Scripting SMTP
SMTP: Using smtp commands in a shell script when sendmail isn't setup.




LOG="/tmp/${0##*/}.log"
SUBJECT="${0##*/}"
MAILSERVER=mailhost
PORT="25"
MAILFROM="user1@domain user2@domain"
MAILLIST="email@somedomain"
#------------------------------------------------------------------------
mailto()
{
exec 3<>/dev/tcp/$MAILSERVER/$PORT
if [ $? -ne 0 ]
then
echo
echo "ERROR: Cannot connect to the Mail Server"
echo "Please check the servername and/or the port number"
exit
fi
echo -en "HELO ${MAILSERVER}\r\n" >&3
echo -en "MAIL FROM:$MAILFROM\r\n" >&3
for MAILTO in ${MAILLIST}
do
echo -en "RCPT TO:$MAILTO\r\n" >&3
done
echo -en "DATA\r\n" >&3
echo -en "Subject: $SUBJECT\r\n\r\n" >&3
echo -en "$(cat ${LOG})\r\n" >&3
echo -en ".\r\n" >&3
echo -en "QUIT\r\n" >&3
cat <&3
}
#------------------------------------------------------------------------
Some commands
.
.
mailto





Tuesday, September 20, 2011

AD group member listing.

List member of a AD group from XP

net group G_UNIX_SCHED /domain

or list user attributes.


net user <user id> /domain

Tuesday, August 30, 2011

Awk: Using getline to read a file in BEGIN

Example reading in the password file into an array for later use.
BEGIN {
        FS=":";
        while ((getline  < "/etc/passwd")  >;0 )
        {
                Alias[$1]=$0;
        }
}



Tuesday, June 28, 2011

Shell Scripting: basename variable

"${0##*/}" is the equivalent of "basename $0".
I.e. From /usr/bin/sh we get just sh.


Also try this to execute commands $(shell command)

Thursday, December 23, 2010

Perl script options

Using getopt in perl scripts.
Sample code
#!/usr/bin/perl
#-------------------------------------------------------------------------
#       Using getop
#-------------------------------------------------------------------------
use Getopt::Std;
getopt('cw');
my @op = `./x.sh -c $opt_c -w $opt_w`;
foreach (@op)
{
        #printf "-c %s\t-w %s\n", $opt_c,$opt_w;
        print $_;
}

Output
./x.pl -c 3 -w 55
in ./x.sh.sh : -c 3 -w 55
-c 3
-w 55

or

my %options=();
#Pre set default values 
my $critv=95;
my $warnv=85;
getopts("c:w:", \%options);
$critv = $options{c} if defined $options{c};
$warnv = $options{w} if defined $options{w};

This method won't report an error if an option is emitted.

Wednesday, December 01, 2010

awk convert time string to time format

This will take a time string HHMMSS (121525) and format it as 12:15:25

A sample line from a.txt:

01201 095440 sd10 3393.2 1.1 13572.8 4.4 0.0 1.0 0.3 2 91

awk '{h=substr($2,1,2);\
      m=substr($2,3,2);\
      s=substr($2,5,2);\
      $2=sprintf("%s:%s:%s",h,m,s);\
      print $0;}' a.txt | head 
 

Will produce the following out put:

101201 09:54:40 sd10 3393.2 1.1 13572.8 4.4 0.0 1.0 0.3 2 91

Thursday, September 02, 2010

Midnight commander

Startup Midnight commander with each panel in a pre-detirmined location.

mc /tmp /#ftp:guest@remote-host.com/pub/dir

Thursday, April 08, 2010

Using awk to remove path and print file date

$ls -lat /current/*xml | \
         nawk '{printf "%s ", $8; system("basename " $9)}'
20:24 file1.xml
21:44 file2.xml
22:34 file3.xml
$

Sunday, March 21, 2010

Thursday, February 18, 2010

Tuesday, August 18, 2009

Add users from copied passwd file


for U in $(awk -F":" '{print $1}' passwd)
do
echo "________________ $U __________________"
UDIR=$(awk -F":" "/$U/"'{print $6}' passwd)
mkdir $UDIR
chown $U $UDIR
done

Friday, October 10, 2008

Simple compiling with gcc

Having trouble with a simple compile:

gcc -o data data.c
/tmp/ccCAfDR5.o: In function `main':
data.c:(.text+0x4c): undefined reference to `exp'
collect2: ld returned 1 exit status

Then:

gcc -lm -o data data.c

And it works, because math.h is just a header file and you need to link with the math's library.

Tuesday, August 12, 2008

rsync with ssh between servers

Rsync quicky with ssh between servers.

rsync -av -n source dir/ server:/target dir -n for testing. nothing happens.

rsync -av source dir/ server:/target dir -n removed. Do it for real.

Also note:
Source dir has a "/" after it to copy its contents and not the directory itself.

Rsync installed in a different location on the remote host:

rsync --rsync-path=/opt/csw/bin/rsync -av -n source dir/ server:/target

Tuesday, June 10, 2008

Perl. Replace text in file.

Substitute text in a file in-situ

perl -i -pe 's/1234/4321/g' filename

-i edits file in-situ.
-p loops through each filename and prints out each line.

Convert Upper case to lower case

perl -i -pe 'tr/A-Z/a-z/' x

Monday, September 17, 2007

Shell scripting: case statement & numbers

Selecting a numeric range with the case statement.
case ${NUMBER} in
[0-9]|[0-6][0-9]|7[0-5])
echo "${NUMBER} <= 75"
;;
[7-8][0-9]|9[0-5])
echo "75 < ${NUMBER}<=95"
;;
[9][5-9]|100)
echo "95< ${NUMBER} <=100"
;;
esac