Unix SG 3
Unix SG 3
Defining functions enable you to execute the same code snippet in a shell script repeatedly without
having to write the entire code again. You first define a function and then call (execute) the function in
the main program as shown below.
$ cat funcs
function function1
read a
func2()
read a
function1
func2
$ ksh funcs
one
two
Exit status functions / Return value functions – are functions that return an output when called
(executed) much similar to the “$?” shell variable
$ cat funcsion
func1()
number=$1
if [ "$number" -lt 5 ]
then
echo 0
else
echo 1
fi
func1 4
func1 5
func1 6
$ ksh funcsion
Functions as comments
$ cat one
func()
$ ksh one
this is 1
this is 2
Awk
Extracting fields using awk, awk is used to extract a field(column) from a given output in tabled
format,awk can also extract using delimiters as shown below. By default awk takes the white space as
the delimiter.
-v : initialize a variable which can be used inside the BEGIN and END block
-f : read the awk program source from a program-file instead of the first command line
argument
$ cat rows
one
four
seven
ten
three
six
nine
twelve
one-two-three
four-five-six
seven-eight-nine
ten-eleven-twelve
two
five
eight
eleven
exercise : use awk to extract the filenames from the output of ls –ltr command, use the pipe (|) operator
to pass the output of ls command to awk - the file name field in the ls –ltr command is the 9 field.
$ cat rows1
Iam one
Iam four
Iam seven
Iam ten
$ awk -v var='this is' '{print var" " $1}' rows1
this is one
this is four
this is seven
this is ten
$ cat rows1
this is one
this is four
this is seven
this is ten
$ cat cmd.awk
{print $1}
one
four
seven
ten
$ cat cmd.awk
this is one
this is four
this is seven
this is ten
$ cat cmd1.awk
Cut
Cut is another unix binary/utility that lets you extract fields from a given output, cut does not
have any default delimiting case behavior
-f : extract only these fields; also print any line that contains no delimiter character,
one-two
four-five
seven-eight
ten-eleven
one-three
four-six
seven-nine
ten-twelve
one-two-three
four-five-six
seven-eight-nine
ten-eleven-twelve
$ cat rows
one-two-three
four-five-six
seven-eight-nine
ten-eleven-twelve
$ cut -c 1-4 rows
one-
four
seve
ten-
o-
fr
se
t-
$ cat rows
one-two-three
four-five-six
seven-eight-nine
ten-eleven-twelve
two-thre
-five-si
n-eight-
eleven-t
$ cat rows1
two
five
eight
Eleven
$ cat rows2
one-two-three
four-five-six
seven-eight-nine
ten-eleven-twelve
two-three
five-six
eight-nine
eleven-twelve
$ cat rows2
one-two-three
four-five-six
seven-eight-nine
ten-eleven-twelve
one three
four six
seven nine
ten twelve
$ cat rows2
one-two-three
four-five-six
seven-eight-nine
ten-eleven-twelve
one-two-three
four-five-six
seven-eight-nine
ten-eleven-twelve
one three
four six
seven nine
ten twelve
Sed
sed like awk is another language in itself, sed stands for stream editor. sed has so many applications but
the most predominantly used are search and replace
number: a number in the script ensures that the patterns are changed only on that line of
the file
w : ‘w’ flag in the script writes a new file with the resulting output but only for the
matching lines
: or | : using these two characters will change the syntax of the sed command to include
$ cat streaming
this is line 1
this is line 2
this is line 3
this is line n
that is line 1
that is line 2
that is line 3
that is line n
$ cat streaming
this is line 1
this is line 2
this is line 3
this is line n
this is line 1
this is line 2
that is line 3
this is line n
$ cat streaming
this is line 1
this is line 2
this is line 3
this is line n
$ sed '1,3 s/this/that/' streaming
that is line 1
that is line 2
that is line 3
this is line n
$ cat streaming
this is line 1
this is line 2
this is line 3
this is line n
this is line 1
this is line 2
that is line 3
that is line n
$ cat streaming
this is line 1
this is line 2
this is line 3
this is line n
that is line 1
that is line 2
that is line 3
that is line n
$ cat file1
that is line 1
that is line 2
that is line 3
that is line n
$ cat streaming
this is line 1
this is line 2
this is line 3
this is line n
$ cat streaming
this is /line 1
this is /line 2
this is /line 3
this is /line n
$ sed 's/\/line/phrase/' streaming
this is phrase 1
this is phrase 2
this is phrase 3
this is phrase n
$ cat streaming
this is /line 1
this is /line 2
this is /line 3
this is /line n
this is phrase 1
this is phrase 2
this is phrase 3
this is phrase n
$ cat streaming
this is /line 1
this is /line 2
this is /line 3
.
this is /line n
this is /phrase 1
this is /phrase 2
this is /phrase 3
this is /phrase n
$ cat streaming
this is /line 1
this is /line 2
this is /line 3
this is /line n
$ cat cmd.sed
s/this/that/
that is /line 1
that is /line 2
that is /line 3
that is /line n
$ cat streaming
this is /line 1
this is /line 2
this is /line 3
this is /line n
$ cat cmd.sed
s/this/that/
s/is/was/
s/1/11/
s/2/22/
$ sed -f cmd.sed streaming
$ cat streaming
$ cat streaming
s/this/that/g
s/is/was/
s/1/11/g
s/2/22/
Grep
grep stands for global regular expression and print, is used to search for patterns in a given output/file.
grep extracts the horizontal lines (rows) unlike awk which prints the vertical columns (fields)
-i : ignore case, prints all the lower/upper matching patterns from a given input file/text
$ cat regexp
Streaming
Writing to files in a loop can be achieved with “>” and “>>”, but how do you read from a file and
manipulate the line for further processing ?
Use the “while construct” by taking the output from “cat” command
$ cat streaming
this is line 1
this is line 2
this is line 3
this is line n
$ cat streaming|while read line
do
echo "$line"
done
this is line 1
this is line 2
this is line 3
this is line n