Monday, February 29, 2016

Week 20: Day 051 - Working With Text Files


Hello, today I will be continuing my blog posts, after a long snowstorm which kept us out of school for two weeks. In this chapter, I will take the exercises put it on here, and then comment on what I do. In the end, I will answer the review questions.


In this exercise, you apply some basic less skills working with file contents and command output.

1. From a terminal, type less /etc/passwd. This opens the /etc/passwd file in the less pager.


When I typed this command, it showed up with the accounts of everyone (since I'm in rackspace).

2. Type G to go to the last line in the file.


This goes to the last line, but I already happened to be there.

3. Type /root to look for the text root. You’ll see that all occurrences of the textroot are highlighted.


This will search for all words that say "root", and will highlight them.

4. Type q to quit less.

Just like vim.

5. Type ps aux | less. This sends the output of the ps aux command (which shows a listing of all processes) to less. Browse through the list.


This is like task manager, except not real time.

6. Press q to quit less.


In this exercise, you learn how to use head and tail to get exactly what you want.

1. Type tail -f /var/log/messages. You’ll see the last lines of /var/log/messages being displayed. The file doesn’t close automatically.


It doesn't, how do we do it?

2. Type Ctrl+C to quit the previous command.

This will exit you out of the pager.

3. Type head -n 5 /etc/passwd to show the first five lines in /etc/passwd.

This shows the first five lines of the file "passwd".

4. Type tail -n 2 /etc/passwd to show the last two lines of /etc/passwd.

This shows the last two lines, rather than the first.

5. Type head -n 5 /etc/passwd | tail -n 1 to show only line number 5 of the /etc/passwd file.

This focuses on one line, line 5, rather than showing the first five lines.


In this exercise, you work through some common grep options.

1. Type grep ‘^#’ /etc/sysconfig/sshd. This shows that the file /etc/sysconfig/sshd contains a number of lines that start with the comment sign #.



2. To view the configuration lines that really matter, type grep -v ‘^#’ /etc/sysconfig/sshd. This shows only lines that do not start with a #.




3. Now type grep -v ‘^#’ /etc/sysconfig/sshd -B 5. This shows lines that are not starting with a # sign but also the five lines that are directly before that line, which is useful because in these lines you’ll typically find comments on how to use the specific parameters. However, you’ll also see that many blank lines are displayed.



4. Type grep -v -e ‘^#’ -e ‘^$’ /etc/sysconfig/sshd. This excludes all blank lines and lines that start with #.



Review Questions


1. Which command enables you to see the results of the ps aux command in a way that you can easily browse up and down in the results?

ps aux | less

2. Which command enables you to show the last five lines from ~/samplefile?

tail -n 5 ~/samplefile

3. Which command do you use if you want to know how many words are in ~/samplefile?

wc ~/samplefile

4. After opening command output using tail -f ~/mylogfile, how do you stop showing output?

Ctrl-C

5. Which grep option do you use to exclude all lines that are starting with either a # or a ;?

grep -v -e ‘^#’ -e ‘^;’ filename


6. Which regular expression do you use to match one or more of the preceding characters?

?

7. Which grep command enables you to see text as well as TEXT in a file?

grep -i text file

8. Which grep command enables you to show all lines starting with PATH, as well as the five lines just before that line?

grep -A5 ‘PATH’ filename

9. Which sed command do you use to show line 9 from ~/samplefile?

sed -n 9p ~/samplefile

10. Which command enables you to replace the word user with the word users in ~/samplefile?

sed -i ‘s/user/users/g’ ~/samplefile

No comments:

Post a Comment