Tuesday, February 19, 2013

A Regular Expressions Approach To Sed And Awk

from ed and grep to sed and awk
-------------------------------------------------------

The unix utilities sed and awk are both based on pattern matching, i.e. regular expressions.

Historically, ed was the first utility on Unix to use regular expressions. It was a line editor and so to make edits, we had to specify both the line address (using the line number or any set of words in that line) and the action to be performed (typically print the line to screen, delete the line or substitute a set of words on that line.)

Almost every shell utility in Unix has the impression of ed. That is, they have the features of both a programming language and of a text editor.

In ed -
* Every command is of the type '/pattern/ {action}'
* A pattern is always a regular expression.
* An actions are typically print, delete or substitute.
* If a pattern is not specified, then the action is performed on the current line.

For example,
/pattern/d    # deletes the next line containing the regular expression "pattern"
g/pattern/d    # deletes all lines containing the regular expression "pattern"
p        # prints current line to screen

In fact, the name of the grep command is derived from the frequently used ed command g/re/p, which prints all lines with the regular expression "re."

In sed and awk, both the input file/stream and the command file/stream typically containg multiple lines.
The first line of the input is read in and all the commands are run against that line. As with ed, if a pattern is specified, then sed/awk will run the action only if the line matches the pattern. If no pattern is specified, then the action is invariably run.
This is what happens with each line of input till we reach end-of-file for the input file/stream.

References -

A Tutorial Introduction to the UNIX Text Editor
by Kernighan
Unix Seventh Edition Manual. (Volume 2A has the tutorial)
http://cm.bell-labs.com/7thEdMan/bswv7.html


sed & awk, 2nd Edition
Dale Dougherty
Arnold Robbins

Historical references -
awk - A Pattern Scanning and Processing Language (September 1, 1978)
SED - A Non-Interactive Text Editor (August 15, 1978) by Lee E. McMahon.

A Regular Expressions Approach To Sed And Awk

from ed and grep to sed and awk
-------------------------------------------------------

The unix utilities sed and awk are both based on pattern matching, i.e. regular expressions.

Historically, ed was the first utility on Unix to use regular expressions. It was a line editor and so to make edits, we had to specify both the line address (using the line number or any set of words in that line) and the action to be performed (typically print the line to screen, delete the line or substitute a set of words on that line.)

Almost every shell utility in Unix has the impression of ed. That is, they have the features of both a programming language and of a text editor.

In ed -
* Every command is of the type '/pattern/ {action}'
* A pattern is always a regular expression.
* An actions are typically print, delete or substitute.
* If a pattern is not specified, then the action is performed on the current line.

For example,
/pattern/d    # deletes the next line containing the regular expression "pattern"
g/pattern/d    # deletes all lines containing the regular expression "pattern"
p        # prints current line to screen

In fact, the name of the grep command is derived from the frequently used ed command g/re/p, which prints all lines with the regular expression "re."

In sed and awk, both the input file/stream and the command file/stream typically containg multiple lines.
The first line of the input is read in and all the commands are run against that line. As with ed, if a pattern is specified, then sed/awk will run the action only if the line matches the pattern. If no pattern is specified, then the action is invariably run.
This is what happens with each line of input till we reach end-of-file for the input file/stream.

References -

A Tutorial Introduction to the UNIX Text Editor
by Kernighan
Unix Seventh Edition Manual. (Volume 2A has the tutorial)
http://cm.bell-labs.com/7thEdMan/bswv7.html


sed & awk, 2nd Edition
Dale Dougherty
Arnold Robbins

Historical references -
awk - A Pattern Scanning and Processing Language (September 1, 1978)
SED - A Non-Interactive Text Editor (August 15, 1978) by Lee E. McMahon.

Simple Backup Script



Monday, February 18, 2013

Shell Script - Install RPM

#!/bin/bash 
# @(#) user1    Demonstrate rpm query.
uname -rv
bash --version
rpm --version
echo
P=${1?" must specify package name."}
rpm -qa "$P" > t1
my_size=$( wc -l < t1 )
echo " Size of report file is $my_size lines"
if [[ $( rpm -qa $P ) =~ ${P} ]]
# if [[ $( rpm -qa $P ) == *${P}* ]]
then
  echo " Package $P is installed."
else
  echo " Package $P not found."
fi
exit 0

Execute Shell Script in PHP

<?php
$output shell_exec('ls -lart');
echo 
"<pre>$output</pre>";?>

Shell Script with Operator


#!/bin/bash
# Prompt for your Age...
echo "Please enter your age:"
read AGE
if [ "$AGE" -lt 20 ] || [ "$AGE" -ge 50 ]; then
echo "Sorry, you are out of the age range."
elif [ "$AGE" -ge 20 ] && [ "$AGE" -lt 30 ]; then
echo "You are in your 20s"
elif [ "$AGE" -ge 30 ] && [ "$AGE" -lt 40 ]; then
echo "You are in your 30s"
elif [ "$AGE" -ge 40 ] && [ "$AGE" -lt 50 ]; then
echo "You are in your 40s"
fi

Shell Login Script



#!/bin/bash
# This is program to login using password.
CORRECT_PASSWORD="geeks" #this is our password.
echo "Please enter correct password"
read PASSWORD
if [ "$PASSWORD" == "$CORRECT_PASSWORD" ]; then
echo "You have logged in Successfully"
else
echo "INCORRECT PASSWORD LOGIN ABORTED !"
fi