Lab 7 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

  1. How many files are in the directory /usr/bin?

ls /usr/bin | wc -l

  1. List the misspelled words in the /usr/share/doc/HTML/index.html file.

aspell list < /usr/share/doc/HTML/index.html

  1. How many times does each misspelled word appear?

aspell list < /usr/share/doc/HTML/index.html | sort | uniq -c

  1. Display the /etc/passwd line for any account that starts with the letter g

grep ‘^g’ /etc/passwd

cat /etc/passwd | grep ‘^g’

  1. Display the number of lines in /etc/passwd

wc -l /etc/passwd

cat /etc/passwd | wc -l

  1. Display a list of usernames (and no other data) from /etc/passwd

cut -d: -f1 /etc/passwd

  1. Display the /etc/passwd line for any account that is using the bash shell

grep ‘bash$’ /etc/passwd

  1. Display the /etc/passwd line for any account that is not using the bash shell

grep -v ‘bash$’ /etc/passwd

  1. 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

  1. 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

 vi ~/issue

Welcome to \n (\l)!

Red Hat Enterprise Linux Server release 5

Kernel \r on an \m

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

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

[student@stationX ~]$ cat /tmp/issue.patch

+ represents a line that should be added

– represents a line that should be removed

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

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

[root@stationX ~]# restorecon /etc/issue

restore file(s) default SELinux security contexts.

[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

By |2015-01-01T01:42:19+00:00January 1st, 2015|Linux and Unix, Linux/Unix Basics|0 Comments

About the Author:

Leave A Comment

Show Buttons
Hide Buttons