Lesson 04 Text Files
Lesson 04 Text Files
Class
Type
Materials
Reviewed
head -n 5 /etc/passwd
head -n 10 /etc/passwd | tail -n 1 // show line number 10
tail -n 3 /etc/passwd
tail -f /var/log/messages
egrep '^[[:alpha:]]{3}$' * 2> /dev/null // egrep all lines that are exactly 3
alphabets
grep '^...$' * 2> /dev/null // grep all lines that are exactly 3 c
haracters
$ grep '^endif$' * 2> /dev/null // find exactlty "endif"
grep '\<endif\>' * 2> /dev/null // find exactlty "endif"
grep
vim
awk
sed
POSIX:
The Portable Operating System Interface is a family of standards specified by the
IEEE Computer Society for maintaining compatibility between operating systems.
The goal of POSIX is to ease the task of cross-platform software development by
establishing a set of guidelines for operating system vendors to follow. Ideally, a
developer should have to write a program only once to run on all POSIX-compliant
systems.
$ cat regtext
b
bt
bit
bite
boot
bloat
boat
Anchoring
The caret ^ and the dollar sign $ are meta-characters that respectively match the
empty string at the beginning and end of a
line.
The Backslash Character and Special Expressions
The symbols \< and \> respectively match the empty string at the beginning and end
of a word.
The symbol \b matches the empty string at the edge of a word, and \B
matches the empty string provided it's not at the edge of a word.
4.4 awk
awk is specialized in data extraction and reporting (could be sent to a printer).
awk -F : '{ print $NF }' /etc/passwd // $NF number of fields, print the last fie
ld in the line.
// useful when number of fields are not the sam
e in all lines.
/bin/bash
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/bin/sync
/sbin/shutdown
/sbin/halt
/sbin/nologin
$
$ sed -n 4p sedfile
FOUR