RH033读书笔记(7)-Lab 8 Introduction to String Processing

Lab 8 Introduction to String Processing

Sequence 1: Exercises in string processing

1. Other than the man page, how could you get a summary of options for the aspell command,
displayed one page at a time?
aspell --help | less

2. How many files are in the directory /usr/bin?
ls /usr/bin | wc -l

3. List the misspelled words in the /usr/share/doc/HTML/index.html file.
aspell list < /usr/share/doc/HTML/index.html

4. How many times does each misspelled word appear?
aspell list < /usr/share/doc/HTML/index.html | sort | uniq -c

5. Display the /etc/passwd line for any account that starts with the letter g
grep '^g' /etc/passwd
cat /etc/passwd | grep '^g'

6. Display the number of lines in /etc/passwd
wc -l /etc/passwd
cat /etc/passwd | wc -l

7. Display a list of usernames (and no other data) from /etc/passwd
cut -d: -f1 /etc/passwd

8. Display the /etc/passwd line for any account that is using the bash shell
grep 'bash$' /etc/passwd

9. Display the /etc/passwd line for any account that is not using the bash shell
grep -v 'bash$' /etc/passwd

10. Display a list of files in /etc/ that contain the word root. Display only the filenames and
do not print errors
grep -l root /etc/* 2> /dev/null

11. Display the shell being used by root, as stored in /etc/passwd.
grep '^root:' /etc/passwd | cut -d: -f7

Sequence 2: diff and patch

1. [student@stationX ~]$ cp /etc/issue ~/issue

2. vi ~/issue
Welcome to (l)!
Red Hat Enterprise Linux Server release 5
Kernel on an m

3. [student@stationX ~]$ diff /etc/issue ~/issue

4. [student@stationX ~]$ diff -u /etc/issue ~/issue > /tmp/issue.patch

5. [student@stationX ~]$ cat /tmp/issue.patch
+ represents a line that should be added
- represents a line that should be removed

6. [root@stationX ~]# patch -b /etc/issue /tmp/issue.patch

7. [student@stationX ~]$ diff -u /etc/issue ~/issue

8. [root@stationX ~]# restorecon /etc/issue
restore file(s) default SELinux security contexts.

9. [root@stationX ~]# patch -R /etc/issue /tmp/issue.patch
[root@stationX ~]# cp /etc.issue.org /etc/issue

Sequence 3: Stream editing with regular expressions

vi cats
cat
catalog
concatenate
polecat
Cat

1. sed 's/cat/dog/' cats
dog
dogalog
condogenate
poledog
Cat

2. sed 's/^[Cc]at/dog/' cats
dog
dogalog
concatenate
polecat
dog

3. sed 's/^cat$/dog/i' cats
dog
catalog
concatenate
polecat
dog

[student@stationX ~]$ sed '10,35s/cat/dog/' pets
[student@stationX ~]$ sed '/digby/,/duncan/s/cat/dog/' pets
[student@stationX ~]$ sed -e 's/cat/dog/g' -e 's/cow/goat/g' pets

vi myedits
s/cat/dog/g
s/cow/goat/g

[student@stationX ~]$ sed -f myedits pets

原文地址:https://www.cnblogs.com/thlzhf/p/3440836.html