Ch 4 Using Common Text File-Related Tools Flashcards

(63 cards)

1
Q

What command opens text file in a pager (to the console/terminal/stdout), allowing for easier reading?

A

less

(e.g.,

sudo dmesg | less
pipes the output of dmesg to a pager

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what command dumpts contents of a text file to screen

A

cat
(short for concatenate…chaining things together)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

because of the limitations of how much data you can see in a shell window, cat will only give you the last lines of a file. What command do you use if you want to see the first lines?

A

tac (cat spelled backwards)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what command will dump the first ten lines of a file to stdout?

A

head

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what command will dump the last ten lines of a file to stdout?

A

tail

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does this command do?

head -n 11 /etc/passwd | tail -n 1

A

The n flag tells it how many lines to give. So first it outputs the first 11 lines of /etc/passwd.

It pipes that to tail, which takes those 11 lines and only shows that last one

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the -f flag for the tail command do?

A

shows you the last ten lines of a file, but sends any additional lines to stdout as that process runs,

e.g. tail -f /var/log/messages

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what are the 11 main commands (well, one is a tech) used for working with text files?

A

less
cat
tac
tail
head
sort
cut
grep
regex
awk
sed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what does the cut command do, broadly?

A

filters out specific fields.

e.g. list of all users in /etc/passwd/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is the -d flag for cut?

A

-d is for delimiter. pick something like a comma or a colon. must be followed by -f, for field (and this will be a number)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is this command doing?

cut -d : -f 1 /etc/passwd

A

looking at the /etc/passwd file, sort using the 1st field and use : as the delimiter

this filters OUT the first field of the file, which will give you just the usernames

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Translate this into plain english:

Cut -d : -f 1 /etc/passwd

A

Parse the etc/password file and, using a colon as a delimiter, take only the first field

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What would be the output of

Sort /etc/passwd?

A

It will print out the contents of the file, but in byte order – or how characters appear in an ASCII text table – looks like it’s alphabetic, but ISNT

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Is the output of sort alphabetical?

A

No

It’s based off the ASCII text table order. So since all capitals come first, Z would come before a, because it’s capitalized

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What command often goes with cut?

A

Sort

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What are different options with the sort command?

A

-n for numeric order
-rn for reverse numeric

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

what does the command

sort -k3 -t : /etc/passwd do?

A

This sorts along a column of your choice – in this case, it’s filtering along the 3rd column in the /etc/passwd file, using : as the field delimiter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

what does this command do?

ps aux | sort -k 4 -n

A

sort the output of the ps aux alone the 4th column, and sort it numerically (don’t need a delimiter because it’s a command’s output, not a file’s?)

overall, this command tells us which processes are using the most memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What command will give you a word count?

A

wc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

what are the three different results you could get from the wc command?

A
  1. number of lines
  2. number of words
  3. number of characters
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What would be the simplest/easiest/fastest command to figure out how many processes are running right now?

A

ps aux | wc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

what does this command do ?

grep anna /etc/passwd

A

looks for the word anna in the /etc/passwd file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
what is a regex line anchor?
^ as in ^anna as in "starts with anna"
26
what would the following command do? grep ash$ /etc/passwd
grab any line in the /etc/passwd file that ends in ash
27
what characters need to be escaped in regex?
* $ ?w
28
which is better and why? grep ^anna /etc/passwd or grep '^anna' /etc/passwd
grep '^anna' /etc/passwd because '^anna" allows you to escape the bash command line interpreting the ^ in anna. We only want the grep command to interpret it.
29
what are the regex wildcards and multipliers
. -- one specific character (r.t matches rat and rut) * -- matches zero or more of a character \{2\} -- or whatever number. match that number of characters (regardless of what they are) ? -- character is optional - match zero or one of character
30
What does this mean in regex? re\{2\}d
match a word that begins with re, ends with d, and has any 2 other characters in between
30
what are some characters in extended regex that base regex doesn't have?
+ -- a character should occur one or more times ? -- a character should occur zero or more times
30
what flag does grep need to use to have the extra features of the extended regex?
grep -E OR just use egrep
31
^text
match a line that starts with 'text'
32
text$
match a line that ends with 'text'
33
.
wildcard -- match any single character (match one, but no more than one, unlike +, which is 1 or more)
34
[abc]
match a, b, or c
35
?
extended regex that matches 0 or more of character preceding
36
+
extended regex that matches one or more of character preceding
37
*
matches zero to an infinite number of the preceding character
38
\{2\}
match exactly 2 of the previous character
39
\{1,3\}
match a min of one and a max of three of the previous character
40
*
match zero to an infinite number of the previous character
41
what does this mean in regex? colou?r
match the word color or colour, because the ? means the u is optional
42
(...) in regex
the () are used to group multiple characters so that the regex can be applied to the group
43
what selinux manpage has an example of regex being used for selinux?
semanage-fcontext
44
what does this mean: "/web(/.*)?"
this regex is referring to the text /web this is followed by the regex (/.*)? It's basically saying, in the /web directory, look for subdirectory files (/web/.* but the .* are optional)?
45
what does grep stand for?
general regular expression parser
46
list 7 of grep's most useful flags
-i -- ignore case -v -- show lines that DON'T have the regular expression -r -- search files recursively -e -- search for lines matching more than one regex, with an addnl -e flag before each regex to be used -E -- interpret using extended regex -A -- show of lines AFTER the matching regex -B -- show of lines BEFORE the matching regex
47
grep -v '^#' /etc/services -B 5
show lines that DON'T start with #, and then the 5 preceding lines to that
48
grep -v -e '^#' -e '^$' /etc/services
excludes all blank lines and lines that start with # (because blank lines start with #$ or, for lines that start with # and also start with a $ after that, exclude them
49
vim option to show hidden characters
:set list, or :set nolist to hide
50
What has made sed and awk historically so powerful and useful?
they were developed in a time when computers didn't have screens, so it's good at creating and parsing text files in a scriptlike way
51
what does this do? awk -F : '{ print $4 }' /etc/passwd
print the fourth field (column) in the /etc/passwd file?
52
what sets awk apart from cut?
awk is better at knowing which fields to use. So if cut fails, use awk it can also do what grep does
53
what does awk -F : ' /user/ { print $4 }' /etc/passwd do?
search the /etc/passwd file for the text 'user' -- if any lines match, print only the fourth field
54
what does sed stand for?
stream editor
55
what does sed -n 5p /etc/passwd do?
56
what does sed do?
filters text from text files (like grep) but also allows you to make modifications as well
57
what does sed -i s/old-text/new-text/g ~/myfile
search for old-text in myfile, and replace with new-text (ALL occurrences...that's what the g is for)
58
what does the -i flag in sed do?
write the output to the output file instead of STDOUT (the default)
59
what does sed -i -e '2d' ~/myfile do?
delete the second line in myfile
60
what does sed -i -e '2d;20,25d' ~/myfile do?
delete lines 2, 20, and 25 from myfile
61