For a good reference:
UNIX Made Easy, Second Edition, John Muster and Associates
sed & awk, Dougherty & Robbins, O'Reilly
$ cp /etc/passwd /tmp/sample
$ diff sample sample.new
$ sed 's/mail/junkmail/' sample
> sample.new
$ grep mail sample.new
junkmail:x:8:12:mail:/var/spool/mail:/sbin/nologin
junkmailnull:x:47:47::/var/spool/mqueue:/dev/null
$ sed 's/mail/junkmail/g' sample > sample.new
$ grep mail sample.new
junkmail:x:8:12:junkmail:/var/spool/junkmail:/sbin/nologin
junkmailnull:x:47:47::/var/spool/mqueue:/dev/null
$ grep 'mail\>' sample.new
$ sed 's/junkmail/mail/g' sample.new
| grep mail
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
mailnull:x:47:47::/var/spool/mqueue:/dev/null
/dev/sda8 /
ext2 defaults
1 1
/dev/sda5 /boot
ext2 defaults
1 2
/dev/cdrom /mnt/cdrom
iso9660 noauto,owner,ro 0 0
/dev/sda6 /var
ext2 defaults
1 2
/dev/sda7 swap
swap defaults
0 0
/proc
/proc proc defaults
0 0
fs1:/usr2/jack /home/jack
nfs rw,auto,nouser
fs1:/usr2/jill /home/jill
nfs rw,auto,nouser
fs1:/usr2/peter /home/peter
nfs rw,auto,nouser
fs2:/usr2/redhat /usr2/redhat nfs
rw,auto,nouser
$ sed '/fs1/s/usr2/usr3/g' /tmp/fstab | grep fs[1,2]
fs1:/usr3/jack /home/jack
nfs rw,auto,nouser
fs1:/usr3/jill /home/jill nfs
rw,auto,nouser
fs1:/usr3/peter /home/peter
nfs rw,auto,nouser
fs2:/usr2/redhat /usr2/redhat nfs
rw,auto,nouser
$ sed '/LABEL=\//s/ext3/ext2/'
fstab | less
$ sed '/news/d' /tmp/sample |
grep news
$ chmod +x topp
$ ./topp /etc/passwd
$ sed -e '/^#/d' -e '/^$/d' /etc/inittab
$ grep
'^[^#$]' /etc/inittab
$ sed 'y/aeiou/AEIOU/' /etc/fstab
$ sed -f sed.cmd fstab
sed 's/fries/chips/g' file1sed '/start/,$ d' file2
sed -e 's/initdefault/mychange/g' -e '/^$/ d' /etc/inittab
sed -f changes projectA
sed '/^$/q' /etc/inittab
Assume you have just updated your kernel and want the new kernel to be your default kernel in lilo.conf.
image=/boot/vmlinuz-2.4.18
label=linux-new
initrd=/boot/initrd-2.4.18.img
read-only
root=/dev/hda1
image=/boot/vmlinuz-2.4.19
label=linux
initrd=/boot/initrd-2.4.19.img
read-only
root=/dev/hda1
#! /bin/bash
LOGFILE=/tmp/sedlog
if sed -e 's/label=linux$/label=linux-old/'
\
-e 's/label=linux-new/label=linux/' \
/tmp/lilo.conf > /tmp/lilo.conf.new ; then
:
else
echo Sed command failed... |tee -a $LOGFILE 2>&1
echo Check /tmp/lilo.conf for errors... |tee -a $LOGFILE 2>&1
fi
Make your script executable
$ chmod +x /tmp/lilomodExecute it and look at the differences between the two files
$ diff lilo.conf lilo.conf.newWhat does the tee command and the redirector 2>&1 do in the above script?
1. Create the directory /tmp/sysconfig
$ mkdir /tmp/sysconfig2. Copy /etc/sysconfig/network file to /tmp/sysconfig
$ cp /etc/sysconfig/network /tmp/sysconfig3. Create a shell script named /tmp/namehost that looks like the following:
#! /bin/bash
CUSTOM_NAME=stationXX
if [ $CUSTOM_NAME !=
"" ]; then
cp /tmp/sysconfig/network
/tmp/sysconfig/network.svd
sed '/HOSTNAME/d'
/tmp/sysconfig/network \
> /tmp/sysconfig/network.new
mv /tmp/sysconfig/network.new
/tmp/sysconfig/network
echo "HOSTNAME=$CUSTOM_NAME"
>> /tmp/sysconfig/network
fi
4. Make the script executable
$ chmod +x /tmp/namehost5. Execute the script
$ /tmp/namehost6. Explain what happened
Extract a value on the end of a line
1. Look at the contents of /proc/cpuinfo
$ less /proc/cpuinfo2. Extract the model name and assign it the variable name model
$ model=`cat /proc/cpuinfo | grep
'model name' \
| sed 's/^model name.*: *//'`
$ echo $modelExtract a value surrounded by other text
Pentium II (Klamath)
1. look at the output of ifconfig
$ ifconfig eth0 | grep 'inet addr'2. Extract the IP number of your machine from this output
$ echo $ipaddressSaving user input to a file using sed
192.168.1.20
Awkmany lines
$ sed -e 's/^//' > /tmp/input
stuff
more stuff
my input lines here
^d
$ cat /tmp/input
stuff
more stuff
my input lines here
The more familiar way
$ cat > /tmp/input
stuff
more stuff
my input lines here
^d
one line
$ sed -e 's/^//' -e 'q' > /tmp/input
one line of input
$ cat /tmp/input
one line of input
The more familiar way
$ echo "one line of input" > /tmp/input
$ awk '/home/ {print $3}'
/etc/fstab
$ awk '{print $0}' /etc/passwd
$ awk '{print $3}' /etc/passwd
$ awk -F:
'{print $1, $3, $4}' /etc/passwd | less
== equal to != not equal to < less than <= less than or equal to > greater than >= greater than or equal to && and || or ~ a match for (regular expression) !~ not a match for (regular expression)
$ awk '/uucp/ || /daemon/ {print}' /etc/passwd
$ ls -l /etc | awk '$5 < 1024'
$ awk -F: '$6 ~/home/
&& $7 ~/bash/' /etc/passwd
$ ls -l | awk '{x+=$5} END {print x}'
2036654
$ ps aux | less
USER PID
%CPU %MEM VSZ RSS
TTY STAT START TIME COMMAND
root
1 0.0 0.2 1372 432 ?
S Nov12 0:04 init [3]
$ ps aux | awk '{x+=$6} END {print
x}'
300448
Using Special Variables in Awk
|
|
Number of current record |
|
|
Meaningless |
|
|
Number of field in current record |
|
|
prints last field in a line |
What does the following commands return?
$ ls -l | awk '{print $NF}'
$ ls -l | awk '{print NF}'
$ ls -l | awk '{print NR}'
$ ls -l | sort -nr +4 | head
$ cp /var/log/messages /tmp/test
$ wc -l /tmp/test
231635 /tmp/test
$ awk 'NR>231435' /tmp/test > /tmp/test2
$ wc -l /tmp/test2
200 test2
1. Write a command line using sed and awk that prints the name of any partition (not device) on your system who's capacity is at 95% or greater. Make sure your command line works. Test it!One possible solution:
$ df | sed 's/%//'| awk '$5>95 && $5 ~/[0-9]/ {print $6,$5"%"}'
Be prepared to write a similar command next Wednesday in class for a quiz.