How to play with sed command

Attention: Cet article/tutorial est ‰gé de plus de 45 jours. Ainsi les informations qu’il contient peuvent être, maintenant, dépassé. Merci de lire toutes les informations qu’il contient pour vous assurez que cet article fonctionnera bien sur votre système

Materials and methods

ifconfig command line, Control operator | (pipe), sed command line, Regular Expressions.

man sed(1).
man sh(1).
man bash(1).
man ifconfig(8).
man regex(3)

The sed utility reads the specified files, or the standard input if no
files are specified, modifying the input as specified by a list of com-
mands. The input is then written to the standard output.

sed options used for this example

-e command
Append the editing commands specified by the command argument to
the list of commands.

-n
By default, each line of input is echoed to the standard output
after all of the commands have been applied to it. The -n option
suppresses this behavior.

Sed Regular Expressions
The sed regular expressions are basic regular expressions (BRE’s, see
man regex(3) for more information). In addition, sed has the following two
additions to BRE’s:

1. In a context address, any character other than a backslash (“\”)
or newline character may be used to delimit the regular expression
by prefixing the first use of that delimiter with a backslash.
Also, putting a backslash character before the delimiting character
causes the character to be treated literally. For example, in the
context address \xabc\xdefx, the RE delimiter is an “x” and the
second “x” stands for itself, so that the regular expression is
“abcxdef”.

2. The escape sequence \n matches a newline character embedded in the
pattern space. You can’t, however, use a literal newline character
in an address or in the substitute command.

One special feature of sed regular expressions is that they can default
to the last regular expression used. If a regular expression is empty,
i.e. just the delimiter characters are specified, the last regular
expression encountered is used instead. The last regular expression is
defined as the last regular expression used as part of an address or sub-
stitute command, and at run-time, not compile-time. For example, the
command “/abc/s//XXX/” will substitute “XXX” for the pattern “abc”.

Pratical work and exercise

We will use sed command to parse the output of ifconfig command.

Regular expression exercise

1. We will use a String to simulate our output

setenv MyString ” abc cba –bvd (frty) 4546464 abc–”

2. We will search the content of MyString between two indices [start pattern] [stop pattern]

echo $MyString | sed -n -e “s/^.*abc //g” -n -e “s/c-.*//p”

#The index [start pattern] is found
echo $MyString | sed -n -e “s/^.*(//g” -n -e “s/).*//p”
#The index [start pattern] is not found
echo $MyString | sed -n -e “s/^.*( //g” -n -e “s/).*//p”

#The index [start pattern] is found but the start index has moved
echo $MyString | sed -n -e “s/^.*([a-z]//g” -n -e “s/).*//p”
#The index [start pattern] is not found
echo $MyString | sed -n -e “s/^.*([0-9]//g” -n -e “s/).*//p”

3. To know some a little more : Step with awk and grep

#ctrl + c to stop
netstat -w1 | awk ‘/[0-9]/ {print $3,$6}’

kill -9 `ps -aux | grep netstat | awk ‘{print $2}’`

man grep(1)
man awk(1)
man netstat(1)
man kill(1)
man ps(1)

4. PHP bridge : Regular Expression Functions

preg_grep()
preg_match_all()
preg_match()
preg_quote()
preg_replace_callback()
preg_replace()
preg_split()

Final Script exercise

#!/bin/sh
# Copyright (C) 2003 OpenJaguar
#
# This programme is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#

getInterfaces=`ifconfig -lu`

echo ” ”

for Interface in $getInterfaces; do

getInet=`ifconfig $Interface | sed -n -e “s/^.*inet //g” -n -e “s/ netmask.*//p”`
getEther=`ifconfig $Interface | sed -n -e “s/^.*ether //p”`
getmtu=`ifconfig $Interface | sed -n -e “s/^.*mtu //p”`

echo “interface” $Interface

for inet in $getInet; do
getBcast=`ifconfig $Interface | sed -n -e “s/^.*$inet.*.broadcast//p”`

if [ “${inet}” != “–>” ]; then
echo ” inet” $inet

if [ “${getBcast}” != “” ]; then
echo ” broadcast” $getBcast
fi
fi

done

if [ “${getEther}” != “” ]; then
echo ” mac” $getEther
fi

if [ “${getmtu}” != “” ]; then
echo ” mtu” $getmtu
fi

echo ” ”

done

Leave a comment

Your email address will not be published. Required fields are marked *