Professional Documents
Culture Documents
L5 - Reg Exp
L5 - Reg Exp
$ who
bgeorge pts/16 Oct 5 15:01 (216.87.102.204)
abakshi pts/13 Oct 6 19:48 (216.87.102.220)
tphilip pts/11 Oct 2 14:10 (AC8C6085.ipt.aol.com)
$ who | cut -c1-8,18-
bgeorge Oct 5 15:01 (216.87.102.204)
abakshi Oct 6 19:48 (216.87.102.220)
tphilip Oct 2 14:10 (AC8C6085.ipt.aol.com)
$
Format: cut -cchars file
chars specifies what characters to extract from each line of file.
cut (continue.)
Example: -c5, -c1,3,4 -c-10-15 -c5-
The –d and –f options are used with cut when
you have data that is delimited by a particular
character
Format: cut –ddchars –ffields file
dchar: delimiters of the fields (default: tab
character)
fields: fields to be extracted from file
cut (continue.)
$ cat phonebook
Edward 336-145
Alice 334-121
Sony 332-336
Robert 326-056
$
cut (continue.)
$ cat /etc/passwd
root:x:0:1:Super-User:/:/sbin/sh
daemon:x:1:1::/:
bin:x:2:2::/usr/bin:
sys:x:3:3::/:
adm:x:4:4:Admin:/var/adm:
lp:x:71:8:Line Printer Admin:/usr/spool/lp:
uucp:x:5:5:uucp Admin:/usr/lib/uucp:
listen:x:37:4:Network Admin:/usr/net/nls:
nobody:x:60001:60001:Nobody:/:
noaccess:x:60002:60002:No Access User:/:
oracle:*:101:67:DBA Account:/export/home/oracle:/bin/csh
webuser:*:102:102:Web User:/export/home/webuser:/bin/csh
abuzneid:x:103:100:Abdelshakour Abuzneid:/home/abuzneid:/sbin/csh
$
cut (continue.)
$ cut -d: -f1 /etc/passwd
root
daemon
bin
sys
adm
lp
uucp
nuucp
listen
nobody
oracle
webuser
abuzneid
$
paste
Format: paste files
tab character is a default delimiter
paste (continue.)
Example:
$ cat students
Sue
Vara
Elvis
Luis
Eliza
$ cat sid
578426
452869
354896
455468
335123
$ paste students sid
Sue 578426
Vara 452869
Elvis 354896
Luis 455468
Eliza 335123
$
paste (continue.)
$ paste -s students
Sue Vara Elvis Luis Eliza
$
sed
sed (stream editor) is a program used for editing
data
Unlike ed, sed can not be used interactively
Format: sed command file
command: applied to each line of the specified file
file: if no file is specified, then standard input is
assumed
sed writes the output to the standard output
s/Unix/UNIX command is applied to every line in
the file, it replaces the first Unix with UNIX
sed (continue.)
sed makes no changes to the original input file
‘s/Unix/UNIX/g’ command is applied to every line
in the file. It replaces every Unix with UNIX. “g”
means global
With –n option, selected lines can be printed
Example: sed –n ’1,2p’ file which prints the first
two lines
Example: sed –n ‘/UNIX/p’ file, prints any line
containing UNIX
sed (continue.)
$ sort students
Eliza
Elvis
Luis
Sue
Vara
$
sort (continue.)
The –n option tells sort to eliminate
duplicate lines from the output
sort (continue.)
$ echo Ash >> students
$ cat students
Sue
Vara
Elvis
Luis
Eliza
Ash
Ash
$ sort students
Ash
Ash
Eliza
Elvis
Luis
Sue
Vara
sort (continue.)
The –s option reverses the order of the sort
The –o option is used to direct the input from the
standard output to file
sort students > sorted_students works as sort
students –o sorted_students
The –o option allows to sort file and saves the output
to the same file
Example:
sort students –o students correct
sort students > students incorrect
sort (continue.)
• The –n option specifies the first field for sort
as number and data to sorted arithmetically
sort (continue.)
$ cat data
-10 11
15 2
-9 -3
2 13
20 22
3 1
$ sort data
-10 11
-9 -3
15 2
2 13
20 22
3 1
$
sort (continue.)
To sort by the second field +1n should be used
instead of n. +1 says to skip the first field
+5n would mean to skip the first five fields on
each line and then sort the data numerically
sort (continue.)
Example
$ sort -t: +2n /etc/passwd
root:x:0:1:Super-User:/:/sbin/sh
daemon:x:1:1::/:
bin:x:2:2::/usr/bin:
sys:x:3:3::/:
adm:x:4:4:Admin:/var/adm:
uucp:x:5:5:uucp Admin:/usr/lib/uucp:
nuucp:x:9:9:uucp Admin:/var/spool/uucppublic:/usr/lib/uucp/uucico
listen:x:37:4:Network Admin:/usr/net/nls:
lp:x:71:8:Line Printer Admin:/usr/spool/lp:
oracle:*:101:67:DBA Account:/export/home/oracle:/bin/csh
webuser:*:102:102:Web User:/export/home/webuser:/bin/csh
y:x:60001:60001:Nobody:/:
$
uniq
Used to find duplicate lines in a file
Format: uniq in_file out_file
uniq will copy in_file to out_file removing
any duplicate lines in the process
uniq’s definition of duplicated lines are
consecutive-occurring lines that match
exactly
uniq (continue.)