Unix and Shell Lab Programming Week3
Unix and Shell Lab Programming Week3
Session1:
a)Login to the system
b)Use the appropriate command to determine your login shell
Sol: $echo $SHELL
sh
c)Use the /etc/passwd file to verify the result of step b.
Sol: $cat /etc/passwd
d)Use the who command and redirect the result to a file called myfile1. Use the
more command to see the contents of myfile1.
Sol : $who > myfile1 | more
Cat myfile2 :
who
This is the basic who command with no command-line arguments. It shows the names of
users that are currently logged in, and may also show the terminal they're logged in on,
and the time they logged in.
who | more
In this example the output of the who command is piped into the more command. This is
useful when there are a lot of users logged into your computer system, and part of the
output of the who command scrolls off the screen. See the more command for more
examples.
who -a
The -a argument lists all available output of the who command for each user.
Piping:---
To connect the output of the one command directly to the input of the other
command. This is exactly what pipes do. The symbol for a pipe is the vertical bar |
% who | sort
will give the same result as above, but quicker and cleaner.
% who | wc -l
Session 2:
a)Write a sed command that deletes the first character in each line in a file.
Sol: sed 's/^./ /‟ file1.dat
nix is Multiuser OS
nix was developed by Brian Kernighan and KenThomson
b)Write a sed command that deletes the last character in each line in a file.
Sol: sed '$s/.$//' file1.dat
Unix is Multiuser O
Unix was developed by Brian Kernighan and KenThomso
c)Write a sed command that swaps the first and second words in each line in a file.
sed -e 's/\([^ ]\+\) *\([^ ]\+\)/\2 \1/'
sed 's/\([a-z]*\) \([a-z]*\)/\2 \1/' (Modified & working)
(Substrings enclosed with "\(" and "\)" can be referenced with "\n" (n is a digit
from 1 to 9) )
Ref : http://www.grymoire.com/Unix/Sed.html#uh-0