Wednesday, January 11, 2017

grep vs awk - Part 2

In this article, we will see more awk alternatives for the frequently used grep commands. This is in continuation to the grep vs awk - Part 1. Let us consider a sample file as shown below:
$ cat file
Unix

Linux
Uniix
Solaris
AIX
ArchLinux
Ubuntu
1. Search for a pattern present in a variable
$ x="Linux"
$ grep "$x" file
Linux
The variable x contains the search pattern. Using grep, the variable can directly be used.
$ awk -v var="$x" '$0 ~ var' file
Linux
In awk, the variable cannot be used directly. It needs to be passed to awk from shell using the -v option. More about the variable passing to awk.

Wednesday, January 4, 2017

How to use arrays in ksh?

  Array, as in any programming language, is a collection of elements. These elements need not be of the same type. One important difference here is shells support only one-dimensional arrays.

1. Creating an array:
   Creating an array is pretty simple.
$ typeset -a arr
$ arr[0]=25
$ arr[1]=18
$ arr[2]="hello"
 Using the typeset command, we let the shell know that we are intending to use the variable arr to store a list of elements. The -a indicates arr as an indexed array. Then the elements are assigned one by one using their index positions. Array indexing always starts from 0.

Wednesday, September 17, 2014

Perl : Find files using File::Find

How to find the list of files in Perl  like the UNIX find command? The answer is File::Find CPAN module. Let us see in this article how to use the File::Find module.

File::Find:
   File::Find module contains 2 modules: find and finddepth. Both are used to find the files, the difference being the order in which the files and directories are parsed. find has all the options like the Unix find command.

Wednesday, August 13, 2014

sed - 20 examples to remove / delete characters from a file

In this article of sed series, we will see the examples of how to remove or delete characters from a file. The syntax of sed command replacement is:
$ sed 's/find/replace/' file
 This sed command finds the pattern and replaces with another pattern. When the replace is left empty, the pattern/element found gets deleted.

Let us consider a sample file as below:
$ cat file
Linux
Solaris
Ubuntu
Fedora
RedHat

Tuesday, November 12, 2013

How to use SUID for shell scripts in Linux?

Question : How to write a shell script which will read the required passwords/connect strings from a config file? Other users should be able to execute the scripts, however they should not be able to read the config file.

Say, I have a config file, myconfig.txt, which contains credentials for an oracle connection:
$ cat myconfig.txt
SQLUSR="scott"
SQLPWD="pwd123"