Skip to main content

Posts

Showing posts with the label Technology

Substitute data with a variable using "awk and sed"

Have you ever tried out substituting particular string with a variable? I thought it was easy.. but it is  NOT !! There are plenty of examples showing subsituting a fixed string with another fixed String. # substitute (find and replace) "foo" with "bar" on each line awk '{sub(/foo/,"bar");print}' # replaces only 1st instance awk '{gsub(/foo/,"bar");print}' # replaces ALL instances in a line But I had an issue to substitute  "foo" with value of "$bar" .. This is not easy. I did a workaround of using "awk" and "sed" The scenario in front of me is to list a directory with files and the hostname of the Server. thisHost=`hostname` ls -lrt | tail +2 | awk '{ print "thishost|"$9}' | sed "s/thishost/$thisHost/"   tail +2 will remove the headers The trick is to print  literal "thishost" and replace "thishost" literal using the valu

Complex examples of AWK

AWK is very powerful and newer generations nawk and gawk do have better flexibility. They are widely used by fellow scripters due to its sheen power. Let's look into some complex examples of AWK variants. I would recommend using nawk format as its installed in most of the Unix systems (AIX, Linux, Ubuntu have nawk by default) Syntax for one line awk commands   awk:  awk -Fs                     '/search/ {action}' awkvar=$shellvar infile nawk: awk -Fs -v awkvar=$shellvar '/search/ {action}'                  infile gawk: awk -Fs -v awkvar=$shellvar '/search/ {action}'                  infile   BEGIN { }, { } and end { } An awk script can have three types of blocks. One of them must be there. a) The BEGIN{} block is processed before the file is checked. b) The {} block runs for every line of input c) The END{} block is processed after the final line of the input file.   awk ' BEGIN    { myvalue = 1000 } /debt/   { myvalue -= $2  } /want/   { myvalue += $4 

Date Validation in a Shell Script

Well I had numerous occasions to verify something at input of a ksh script.  This could be a date or number of particular string etc. The most useful commands here are  "grep -E -q"  which returns a return code based on the search condition.  Exploiting this logic, please find sample Date Validation check in ksh shell script # Date should fall in 2010-01-01  to 2019-12-31 # Script expecting a Date parameter in YYYYMMDD format as input # This is not a 100% check, but will cover 99% of the scenario's if [ $# -eq 1 ] then echo ${DateFormatInput} | grep -E -q '^201[1-9][01][0-9][0-3][0-9]$' if [[ $? != 0 ]]; then echo "Please enter Date in YYYYMMDD format. You Entered $@ " echo "Quitting.. No action done..." exit 0 fi else echo "Please enter Date as parameter in YYYYMMDD format" echo "Quitting.. No action done..." exit 0 fi This script is useful especially when users enter various formats of date. Like UK employees use dd/mm/yyy

Epoch Human Time Batch Conversion script

I had to create a sample script to convert epoch time to Human readable format.  (both ways) Also from Human readable time format to Epoch.  Input is a file with "Epoch" or "Human Readable"  format and the program will convert in the other format. Till now we used: http://www.epochconverter.com/ . But for embedding into your application or for batch convert, please find my program in Java import java.io.*; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.SimpleTimeZone; import java.util.concurrent.TimeUnit; public class EpochConverterClass { /** * @param args */ public static String dateHumanConvert(String dateEpochFormat) { SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); Calendar cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT")); formatter.setCalendar(cal); Date dateHuman = new Date(Long.parseLong(dateEpochFormat) * 1000); // eg

Wordpress themes - Interesting ones

Had some analysis on the wordpress themes and found these very interesting.. Below ones are in queue to update the site :) All are nice looking themes and simple.   http://wp-themes.com/pagelines/?TB_iframe=true&width=600&height=400 http://wp-themes.com/zbench/?TB_iframe=true&width=600&height=400 http://wp-themes.com/simplicitybright/?TB_iframe=true&width=600&height=400 http://wp-themes.com/?p=19