1
00:00:00,720 --> 00:00:01,740
In this lesson,

2
00:00:01,740 --> 00:00:04,410
we're going to discuss the processing of text files

3
00:00:04,410 --> 00:00:06,570
by using a number of command line tools

4
00:00:06,570 --> 00:00:08,850
to perform operations on text files

5
00:00:08,850 --> 00:00:11,910
and then write those results to the standard output.

6
00:00:11,910 --> 00:00:13,260
By the end of this video,

7
00:00:13,260 --> 00:00:15,630
you're going to be expected to process information

8
00:00:15,630 --> 00:00:16,980
in powerful ways

9
00:00:16,980 --> 00:00:20,460
by restructuring output to generate useful reports,

10
00:00:20,460 --> 00:00:22,350
modifying text in the files,

11
00:00:22,350 --> 00:00:25,200
and many other system administration tasks.

12
00:00:25,200 --> 00:00:26,033
To do this,

13
00:00:26,033 --> 00:00:28,500
we're first going to talk about the echo command.

14
00:00:28,500 --> 00:00:31,050
The echo command is a built-in Linux feature

15
00:00:31,050 --> 00:00:34,170
that prints out arguments as the standard output.

16
00:00:34,170 --> 00:00:36,000
The echo command is commonly used

17
00:00:36,000 --> 00:00:38,160
to display text strings or command results

18
00:00:38,160 --> 00:00:40,200
as messages to the screen.

19
00:00:40,200 --> 00:00:41,280
To run echo,

20
00:00:41,280 --> 00:00:43,830
you simply type in echo and a string.

21
00:00:43,830 --> 00:00:45,690
For example, let's say I typed in

22
00:00:45,690 --> 00:00:47,460
echo "Hello, World!"

23
00:00:47,460 --> 00:00:49,200
The output that's going to be printed to the screen

24
00:00:49,200 --> 00:00:50,940
is Hello, World!

25
00:00:50,940 --> 00:00:52,890
So that means using the echo command

26
00:00:52,890 --> 00:00:55,200
returns the provided string as the output

27
00:00:55,200 --> 00:00:57,510
with absolutely no changes.

28
00:00:57,510 --> 00:00:59,940
The printf command is similar to echo

29
00:00:59,940 --> 00:01:02,580
but it provides the user with a lot more control

30
00:01:02,580 --> 00:01:05,069
over how the output is going to be formatted.

31
00:01:05,069 --> 00:01:07,170
You can supply various format characters

32
00:01:07,170 --> 00:01:08,820
within the text you want to output

33
00:01:08,820 --> 00:01:10,650
by using things like the backslash

34
00:01:10,650 --> 00:01:12,810
to indicate where they're going to be used.

35
00:01:12,810 --> 00:01:13,710
For example,

36
00:01:13,710 --> 00:01:18,710
if I type printf "Hello.\nWhat's your name?",

37
00:01:21,090 --> 00:01:23,400
this will print Hello. What's your name?

38
00:01:23,400 --> 00:01:26,670
because that \n is a new line format character.

39
00:01:26,670 --> 00:01:28,650
It's automatically going to add a new line

40
00:01:28,650 --> 00:01:29,850
wherever it's placed,

41
00:01:29,850 --> 00:01:32,520
in this case after the hello.

42
00:01:32,520 --> 00:01:34,260
Whenever you need to perform operations

43
00:01:34,260 --> 00:01:36,120
like removing repeated characters,

44
00:01:36,120 --> 00:01:38,040
converting uppercase to lowercase,

45
00:01:38,040 --> 00:01:40,470
and basic character replacing and removing,

46
00:01:40,470 --> 00:01:43,110
you might want to look at the tr command.

47
00:01:43,110 --> 00:01:44,580
The tr command can be run

48
00:01:44,580 --> 00:01:47,040
by typing tr, the first character,

49
00:01:47,040 --> 00:01:48,690
and then the second character.

50
00:01:48,690 --> 00:01:50,190
This is where the first character

51
00:01:50,190 --> 00:01:52,140
is the character you want to replace.

52
00:01:52,140 --> 00:01:55,740
So for example, let's say I typed in linuxize,

53
00:01:55,740 --> 00:01:58,290
L-I-N-U-X-I-Z-E,

54
00:01:58,290 --> 00:02:01,080
and then I pipe that over to the tr command,

55
00:02:01,080 --> 00:02:03,420
and I want to remove l,i,n

56
00:02:03,420 --> 00:02:06,030
and replace it with r,e,d.

57
00:02:06,030 --> 00:02:08,130
Each time I see an occurrence of L,

58
00:02:08,130 --> 00:02:10,199
it's going to be replace with the R,

59
00:02:10,199 --> 00:02:11,460
every time I see an I,

60
00:02:11,460 --> 00:02:13,020
it's going to replace with an E,

61
00:02:13,020 --> 00:02:14,490
every time I see that N,

62
00:02:14,490 --> 00:02:16,080
it's going to be replace with a D,

63
00:02:16,080 --> 00:02:17,700
so I'm replacing those three characters

64
00:02:17,700 --> 00:02:19,800
with the r,e,d characters.

65
00:02:19,800 --> 00:02:24,510
This will give me the output of R-E-D-U-X-E-Z-E.

66
00:02:24,510 --> 00:02:26,340
So now instead of linuxize,

67
00:02:26,340 --> 00:02:28,800
I'm getting reduxeze.

68
00:02:28,800 --> 00:02:31,980
Another command we can use is known as wc.

69
00:02:31,980 --> 00:02:34,920
wc is going to allow you to count the number of lines

70
00:02:34,920 --> 00:02:36,390
the words, the characters,

71
00:02:36,390 --> 00:02:39,690
and the bites of any given file or standard input,

72
00:02:39,690 --> 00:02:41,490
and then print the result out.

73
00:02:41,490 --> 00:02:43,410
If multiple files are specified,

74
00:02:43,410 --> 00:02:45,630
the command will display the counts for each file

75
00:02:45,630 --> 00:02:48,150
and the total count for all of the files.

76
00:02:48,150 --> 00:02:49,080
To run this,

77
00:02:49,080 --> 00:02:53,010
simply type in wc, the options, and the file names.

78
00:02:53,010 --> 00:02:55,380
Now, there is a lot of different options you can use

79
00:02:55,380 --> 00:02:57,450
within the wc command.

80
00:02:57,450 --> 00:02:58,320
For example,

81
00:02:58,320 --> 00:03:01,560
you can -c if you want to display the byte count,

82
00:03:01,560 --> 00:03:02,820
if you use -m,

83
00:03:02,820 --> 00:03:04,890
it's going to display the character count,

84
00:03:04,890 --> 00:03:06,240
if you do -l,

85
00:03:06,240 --> 00:03:08,220
it's going to display the new line count,

86
00:03:08,220 --> 00:03:09,750
and if you do -w,

87
00:03:09,750 --> 00:03:12,000
it's going to display the word count.

88
00:03:12,000 --> 00:03:14,550
Another command we have is known as sort.

89
00:03:14,550 --> 00:03:16,920
The sort command is a command line utility

90
00:03:16,920 --> 00:03:19,260
for sorting lines of text files.

91
00:03:19,260 --> 00:03:22,140
The sort command supports sorting alphabetically,

92
00:03:22,140 --> 00:03:23,400
in reverse order,

93
00:03:23,400 --> 00:03:25,230
by number, by month,

94
00:03:25,230 --> 00:03:27,720
and it can also remove duplicates for you.

95
00:03:27,720 --> 00:03:29,850
There are a few options for the sort command

96
00:03:29,850 --> 00:03:31,140
you should be familiar with

97
00:03:31,140 --> 00:03:34,230
including the -k option and the column number.

98
00:03:34,230 --> 00:03:35,220
When you do this,

99
00:03:35,220 --> 00:03:38,580
it's going to specify the field value that you want to use.

100
00:03:38,580 --> 00:03:40,500
So if I use -k2,

101
00:03:40,500 --> 00:03:42,870
this indicates the second field.

102
00:03:42,870 --> 00:03:44,820
If you use the -n option,

103
00:03:44,820 --> 00:03:46,500
this compare and sorts lines

104
00:03:46,500 --> 00:03:48,960
based on a string's numerical value,

105
00:03:48,960 --> 00:03:50,610
if you use the -r option,

106
00:03:50,610 --> 00:03:53,040
it's going to sort the fields in descending order

107
00:03:53,040 --> 00:03:54,480
because the fields are going to be sorted

108
00:03:54,480 --> 00:03:56,220
in ascending order by default,

109
00:03:56,220 --> 00:03:57,510
and so by using -r,

110
00:03:57,510 --> 00:03:59,310
we're going to reverse that.

111
00:03:59,310 --> 00:04:01,740
Another option you can use is -t,

112
00:04:01,740 --> 00:04:04,770
which allows you to separate one field from another.

113
00:04:04,770 --> 00:04:06,120
To run the sort command,

114
00:04:06,120 --> 00:04:10,080
simply type in sort, your options, and your file name.

115
00:04:10,080 --> 00:04:13,080
Another command we need to talk about is the cut command.

116
00:04:13,080 --> 00:04:16,140
The cut command extracts specific lines of text

117
00:04:16,140 --> 00:04:17,519
from a file.

118
00:04:17,519 --> 00:04:20,610
Common cut command options include -c.

119
00:04:20,610 --> 00:04:22,590
The -c option is going to specify

120
00:04:22,590 --> 00:04:25,680
the number of characters to cut from each line.

121
00:04:25,680 --> 00:04:28,110
If you use the -d and a delimiter,

122
00:04:28,110 --> 00:04:30,240
it's going to separate one field from another

123
00:04:30,240 --> 00:04:31,590
by using that delimiter.

124
00:04:31,590 --> 00:04:34,980
For instance, a comma for comma-separated values.

125
00:04:34,980 --> 00:04:37,290
If you use -f and the field number,

126
00:04:37,290 --> 00:04:39,600
this is going to specify the field numbers to cut

127
00:04:39,600 --> 00:04:41,490
as separated by the delimiter.

128
00:04:41,490 --> 00:04:44,670
For example, -f2 will indicate the field

129
00:04:44,670 --> 00:04:47,970
between the first and second instances of the delimiter

130
00:04:47,970 --> 00:04:51,300
or those commas in a comma-separated value file.

131
00:04:51,300 --> 00:04:52,133
And finally,

132
00:04:52,133 --> 00:04:55,470
s is an option we can use to suppress a line

133
00:04:55,470 --> 00:04:57,780
if a delimiter is not found.

134
00:04:57,780 --> 00:04:59,070
To run the cut command,

135
00:04:59,070 --> 00:05:02,760
simply type in cut, the options, and the file name.

136
00:05:02,760 --> 00:05:05,100
Another one we have is the paste command.

137
00:05:05,100 --> 00:05:07,110
The paste command is used to merge lines

138
00:05:07,110 --> 00:05:09,240
from text files horizontally,

139
00:05:09,240 --> 00:05:11,460
each line of an initial file in a row

140
00:05:11,460 --> 00:05:13,170
is going to be in the first column.

141
00:05:13,170 --> 00:05:16,290
In using paste, you specify a second file,

142
00:05:16,290 --> 00:05:18,030
and every line of the second file

143
00:05:18,030 --> 00:05:20,820
becomes a row in a new second column.

144
00:05:20,820 --> 00:05:21,720
By default,

145
00:05:21,720 --> 00:05:24,780
the paste command uses a tab space deliminator

146
00:05:24,780 --> 00:05:26,550
to separate each column.

147
00:05:26,550 --> 00:05:29,010
So you can use the -d option

148
00:05:29,010 --> 00:05:30,780
to specify a different deliminator,

149
00:05:30,780 --> 00:05:32,490
like a comma if you wanted to,

150
00:05:32,490 --> 00:05:35,280
but by default, it is going to use a tab.

151
00:05:35,280 --> 00:05:37,920
So, for example, let's say I have two files:

152
00:05:37,920 --> 00:05:40,050
the first file is a name of cities

153
00:05:40,050 --> 00:05:44,340
and I have New York, Tokyo, London, and Lima in it,

154
00:05:44,340 --> 00:05:46,740
then I have another file called countries

155
00:05:46,740 --> 00:05:51,030
and it has United States, Japan, England, and Peru.

156
00:05:51,030 --> 00:05:54,690
So if I wanted to combine those two files into one list,

157
00:05:54,690 --> 00:05:57,390
I could do that by running the paste command.

158
00:05:57,390 --> 00:06:02,130
I would type paste -d , cities countries,

159
00:06:02,130 --> 00:06:03,270
and hit Enter.

160
00:06:03,270 --> 00:06:04,830
This then creates the output

161
00:06:04,830 --> 00:06:07,020
of New York, United States;

162
00:06:07,020 --> 00:06:08,730
Tokyo, Japan;

163
00:06:08,730 --> 00:06:10,200
London, England;

164
00:06:10,200 --> 00:06:11,760
and Lima, Peru

165
00:06:11,760 --> 00:06:13,740
because I'm using that comma I specified

166
00:06:13,740 --> 00:06:15,510
as my delineator.

167
00:06:15,510 --> 00:06:18,420
Another command for us to look at is the diff command.

168
00:06:18,420 --> 00:06:21,330
Now the diff command is used to compare text files

169
00:06:21,330 --> 00:06:23,820
and it's going to find the difference between them.

170
00:06:23,820 --> 00:06:25,920
This command is going to display the two files

171
00:06:25,920 --> 00:06:27,510
and the differences between them.

172
00:06:27,510 --> 00:06:30,120
It's really useful when you're trying to do version control

173
00:06:30,120 --> 00:06:32,190
across different code bases.

174
00:06:32,190 --> 00:06:33,900
If you're using various symbols,

175
00:06:33,900 --> 00:06:35,220
the output can actually suggest

176
00:06:35,220 --> 00:06:37,440
how you can change one file to another

177
00:06:37,440 --> 00:06:39,540
to make it identical to each other.

178
00:06:39,540 --> 00:06:41,880
Each symbol now has a special meaning.

179
00:06:41,880 --> 00:06:44,610
If you have a less than symbol with a line after it,

180
00:06:44,610 --> 00:06:47,460
this means the line should be removed from the first file

181
00:06:47,460 --> 00:06:50,010
because it doesn't appear in the second file.

182
00:06:50,010 --> 00:06:52,830
If you have a greater than symbol with the line after it,

183
00:06:52,830 --> 00:06:55,560
this means that line should be added to the second file

184
00:06:55,560 --> 00:06:57,480
because it doesn't exist there.

185
00:06:57,480 --> 00:06:58,710
In addition to this,

186
00:06:58,710 --> 00:07:01,170
the diff command can also denote the line numbers

187
00:07:01,170 --> 00:07:02,670
for each line in the file

188
00:07:02,670 --> 00:07:04,950
that would be affected by the deletion, addition,

189
00:07:04,950 --> 00:07:06,840
or change operations.

190
00:07:06,840 --> 00:07:08,250
To run the diff command,

191
00:07:08,250 --> 00:07:11,970
type diff, file name 1 and file name 2.

192
00:07:11,970 --> 00:07:14,250
Now the diff command also has some various options

193
00:07:14,250 --> 00:07:15,676
that we're going to talk about:

194
00:07:15,676 --> 00:07:19,352
b is the option that ignores spacing differences,

195
00:07:19,352 --> 00:07:22,246
i ignores case differences,

196
00:07:22,246 --> 00:07:26,477
t is going to expand tab characters in the output files,

197
00:07:26,477 --> 00:07:30,378
w is going to ignore spacing differences in tabs,

198
00:07:30,378 --> 00:07:33,780
c is going to display a list of all the differences

199
00:07:33,780 --> 00:07:35,670
with three lines of context,

200
00:07:35,670 --> 00:07:39,330
while -u is going to output the results in a unified mode,

201
00:07:39,330 --> 00:07:42,060
which presents a more streamlined format.

202
00:07:42,060 --> 00:07:43,920
The next command we're going to talk about is grep,

203
00:07:43,920 --> 00:07:47,880
and grep in its most basic form is a search tool.

204
00:07:47,880 --> 00:07:49,650
But unlike find or locate,

205
00:07:49,650 --> 00:07:52,470
it's not limited to finding file names.

206
00:07:52,470 --> 00:07:56,310
Instead, it's often used to search the contents of the file

207
00:07:56,310 --> 00:07:58,470
for a particular string of text

208
00:07:58,470 --> 00:07:59,400
By default,

209
00:07:59,400 --> 00:08:02,160
grep is going to display each full line of the file

210
00:08:02,160 --> 00:08:04,740
that your search pattern is found inside of.

211
00:08:04,740 --> 00:08:05,760
In this way,

212
00:08:05,760 --> 00:08:08,460
you can use grep to both process a text file

213
00:08:08,460 --> 00:08:11,370
and read the contents that are most pertinent to you,

214
00:08:11,370 --> 00:08:14,160
and we use grep a lot in the cybersecurity world

215
00:08:14,160 --> 00:08:15,630
when we're looking through log files

216
00:08:15,630 --> 00:08:18,450
and trying to find specific instances of things.

217
00:08:18,450 --> 00:08:19,380
For example,

218
00:08:19,380 --> 00:08:21,690
you may want to audit the user's login events

219
00:08:21,690 --> 00:08:23,550
by looking at an access log.

220
00:08:23,550 --> 00:08:25,320
Instead of reading the entire log

221
00:08:25,320 --> 00:08:27,900
and stepping through a search term in a text editor,

222
00:08:27,900 --> 00:08:29,190
you can instead simply print

223
00:08:29,190 --> 00:08:31,110
all the relevant lines to the screen

224
00:08:31,110 --> 00:08:32,970
by using the grep command.

225
00:08:32,970 --> 00:08:34,860
The proper way to use the grep command

226
00:08:34,860 --> 00:08:38,010
is to type in grep, the options, the search pattern,

227
00:08:38,010 --> 00:08:39,450
and the file name.

228
00:08:39,450 --> 00:08:41,190
The grep command has a couple of options

229
00:08:41,190 --> 00:08:42,390
we need to talk about,

230
00:08:42,390 --> 00:08:45,990
the first one is -E and a pattern.

231
00:08:45,990 --> 00:08:48,150
This is used to use pattern matching

232
00:08:48,150 --> 00:08:50,340
using an extended regular expression

233
00:08:50,340 --> 00:08:52,920
instead of just using a standard search term.

234
00:08:52,920 --> 00:08:55,410
If you use -F in a pattern,

235
00:08:55,410 --> 00:08:56,700
this is used to match a pattern

236
00:08:56,700 --> 00:08:58,890
as a list of fixed strings.

237
00:08:58,890 --> 00:09:01,320
The option -f and a file name

238
00:09:01,320 --> 00:09:02,700
will be used to match patterns

239
00:09:02,700 --> 00:09:05,340
that are contained in a specified file.

240
00:09:05,340 --> 00:09:08,850
The option -i is going to be used to ignore casing,

241
00:09:08,850 --> 00:09:11,730
and the option -v is used to output lines

242
00:09:11,730 --> 00:09:15,090
only if those lines don't match the provided pattern,

243
00:09:15,090 --> 00:09:15,923
so essentially,

244
00:09:15,923 --> 00:09:17,340
show me everything that doesn't match

245
00:09:17,340 --> 00:09:18,810
what I'm looking for.

246
00:09:18,810 --> 00:09:21,300
The option -c is going to be used to print

247
00:09:21,300 --> 00:09:23,520
only the numbers of the matching lines

248
00:09:23,520 --> 00:09:25,800
and not the actual lines themself,

249
00:09:25,800 --> 00:09:28,830
and the option -l is going to print only the files

250
00:09:28,830 --> 00:09:30,330
that have matching lines,

251
00:09:30,330 --> 00:09:32,130
not the lines themself.

252
00:09:32,130 --> 00:09:33,750
This is helpful if you're trying to find

253
00:09:33,750 --> 00:09:37,350
is there this keyword across all the files on my system

254
00:09:37,350 --> 00:09:39,840
and it'll tell you all the files that match that keyword

255
00:09:39,840 --> 00:09:41,280
inside of them,

256
00:09:41,280 --> 00:09:43,500
and the option -o is going to print

257
00:09:43,500 --> 00:09:45,240
only the matching part of a line

258
00:09:45,240 --> 00:09:47,280
instead of the entire line.

259
00:09:47,280 --> 00:09:49,860
Now, in addition to searching the contents of the files,

260
00:09:49,860 --> 00:09:52,260
you can also use grep to search a directory

261
00:09:52,260 --> 00:09:54,000
in order to locate a certain file

262
00:09:54,000 --> 00:09:56,640
that contains those keywords or patterns.

263
00:09:56,640 --> 00:09:58,950
For example, if I wanted to find files

264
00:09:58,950 --> 00:10:00,870
with the keyword audit in them,

265
00:10:00,870 --> 00:10:03,420
I can use the ls -l command

266
00:10:03,420 --> 00:10:06,780
and then pipe that into grep with the word audit.

267
00:10:06,780 --> 00:10:09,300
This would return a long listing of any files

268
00:10:09,300 --> 00:10:10,530
in the current directory

269
00:10:10,530 --> 00:10:13,050
whose name contains audit.

270
00:10:13,050 --> 00:10:14,610
The next command we're going to talk about

271
00:10:14,610 --> 00:10:16,770
is the awk command.

272
00:10:16,770 --> 00:10:20,670
The awk command performs pattern matching on files.

273
00:10:20,670 --> 00:10:23,970
It's based on the AWK programming language.

274
00:10:23,970 --> 00:10:27,390
The awk keyword is going to be followed by the pattern,

275
00:10:27,390 --> 00:10:28,710
the action to be performed,

276
00:10:28,710 --> 00:10:30,150
and the file name.

277
00:10:30,150 --> 00:10:31,620
Now, the action to be performed

278
00:10:31,620 --> 00:10:33,600
is given within curly braces,

279
00:10:33,600 --> 00:10:35,730
and the pattern and the action to be performed

280
00:10:35,730 --> 00:10:38,640
should be specified within a single quote.

281
00:10:38,640 --> 00:10:40,500
If the pattern is not specified,

282
00:10:40,500 --> 00:10:43,170
the action is performed on all input data.

283
00:10:43,170 --> 00:10:45,960
However, if the action is not specified,

284
00:10:45,960 --> 00:10:48,360
the entire line is going to be printed.

285
00:10:48,360 --> 00:10:51,390
The awk command can be executed from the command line

286
00:10:51,390 --> 00:10:54,750
or from within an awk script file.

287
00:10:54,750 --> 00:10:57,420
The awk command can be used to process text

288
00:10:57,420 --> 00:10:59,250
in a variety of different ways,

289
00:10:59,250 --> 00:11:02,310
including extracting text that matches a certain pattern,

290
00:11:02,310 --> 00:11:04,770
deleting text that matches a certain pattern,

291
00:11:04,770 --> 00:11:06,810
adding text that matches a certain pattern,

292
00:11:06,810 --> 00:11:08,100
and much more.

293
00:11:08,100 --> 00:11:09,600
To run awk,

294
00:11:09,600 --> 00:11:14,600
simply type in awk, options, patterns, actions,

295
00:11:14,640 --> 00:11:16,260
and file names.

296
00:11:16,260 --> 00:11:17,970
In awk scripts,

297
00:11:17,970 --> 00:11:21,240
you can also provide patterns along with blocks of code.

298
00:11:21,240 --> 00:11:23,790
If pattern matching any line in the input file,

299
00:11:23,790 --> 00:11:26,880
the code blocks in the script can then be executed.

300
00:11:26,880 --> 00:11:29,550
The pattern can include regular_expressions as well

301
00:11:29,550 --> 00:11:31,920
that will retrieve all the records that match it.

302
00:11:31,920 --> 00:11:36,920
For example, if I use / bracket abc bracket /,

303
00:11:37,020 --> 00:11:41,130
it's going to find any record that begins with a, b, or c.

304
00:11:41,130 --> 00:11:43,620
If you use the pattern relational_expressions,

305
00:11:43,620 --> 00:11:45,270
this will retrieve all the records

306
00:11:45,270 --> 00:11:47,320
whose first field contains the value abc.

307
00:11:48,270 --> 00:11:51,777
For example, $1 == "abc".

308
00:11:52,800 --> 00:11:54,500
The pattern pattern_1 && pattern_2

309
00:11:56,300 --> 00:11:58,110
is going to retrieve all the records

310
00:11:58,110 --> 00:12:00,810
whose first field contains the value abc

311
00:12:00,810 --> 00:12:03,780
and the second field contains the value 01.

312
00:12:03,780 --> 00:12:04,830
So for example,

313
00:12:04,830 --> 00:12:09,830
if I have $1 == "abc" && $2 == "01",

314
00:12:13,080 --> 00:12:15,030
this creates that pattern.

315
00:12:15,030 --> 00:12:17,100
If I want to create an OR condition,

316
00:12:17,100 --> 00:12:19,980
I can do that by using two pipe characters.

317
00:12:19,980 --> 00:12:23,617
So if I use the pattern of pattern_1 || pattern_2,

318
00:12:25,290 --> 00:12:26,970
this will retrieve all the records

319
00:12:26,970 --> 00:12:29,340
that satisfy the conditions for the first field

320
00:12:29,340 --> 00:12:33,030
that contains the values of pattern 1 or abc,

321
00:12:33,030 --> 00:12:36,990
or the second field that contains the value of 01,

322
00:12:36,990 --> 00:12:38,640
which was my second pattern,

323
00:12:38,640 --> 00:12:40,680
or even both of them.

324
00:12:40,680 --> 00:12:41,820
So for example,

325
00:12:41,820 --> 00:12:43,627
I might have somebody that looks like

326
00:12:43,627 --> 00:12:48,627
$1 == "abc" || $2 == "01",

327
00:12:50,760 --> 00:12:53,820
and this would fulfill this OR condition.

328
00:12:53,820 --> 00:12:55,560
Now, another pattern we can use

329
00:12:55,560 --> 00:12:57,390
is basically an if-then,

330
00:12:57,390 --> 00:12:58,470
and we do this by having

331
00:12:58,470 --> 00:12:59,903
pattern_1 ? pattern_2 : pattern_3.

332
00:13:03,570 --> 00:13:04,410
So what's going to happen

333
00:13:04,410 --> 00:13:06,720
is it's first going to evaluate pattern 1,

334
00:13:06,720 --> 00:13:08,250
and if it matches pattern 1,

335
00:13:08,250 --> 00:13:09,870
it's going to go to pattern 2,

336
00:13:09,870 --> 00:13:11,460
and if it matches pattern two,

337
00:13:11,460 --> 00:13:13,320
it's going to print it to the screen.

338
00:13:13,320 --> 00:13:16,740
But if it evaluates pattern 1 and it doesn't match,

339
00:13:16,740 --> 00:13:18,660
it's going to then evaluate pattern 3,

340
00:13:18,660 --> 00:13:20,220
and if it matches pattern 3,

341
00:13:20,220 --> 00:13:22,080
it'll display that to the screen.

342
00:13:22,080 --> 00:13:23,640
So what does this look like?

343
00:13:23,640 --> 00:13:24,960
Well, let's say I have something

344
00:13:24,960 --> 00:13:29,960
like $1 == "10" ? $5 == "20" : $9 == "30".

345
00:13:35,460 --> 00:13:38,820
This means that if field 1 equals 10,

346
00:13:38,820 --> 00:13:42,210
I'm going to evaluate field 5 and see if it equals 20.

347
00:13:42,210 --> 00:13:44,880
If it does, I'll print that line to the screen.

348
00:13:44,880 --> 00:13:48,870
But if I evaluated field 1 and it didn't equal 10,

349
00:13:48,870 --> 00:13:50,910
I'm then going to evaluate field 9

350
00:13:50,910 --> 00:13:52,500
and see if it equals 30,

351
00:13:52,500 --> 00:13:54,810
and if it does, I'll print it to the screen.

352
00:13:54,810 --> 00:13:57,390
In the case that any of those are not going to happen,

353
00:13:57,390 --> 00:13:59,280
we are then going to print nothing to the screen

354
00:13:59,280 --> 00:14:01,410
and evaluate the next line.

355
00:14:01,410 --> 00:14:03,120
The last pattern we need to cover

356
00:14:03,120 --> 00:14:06,030
is known as pattern_1, pattern 2,

357
00:14:06,030 --> 00:14:07,920
and this will print a range of records

358
00:14:07,920 --> 00:14:10,050
starting from the record whose first field

359
00:14:10,050 --> 00:14:12,540
contains the value of pattern 1

360
00:14:12,540 --> 00:14:16,200
and going until it finds the field of pattern 2.

361
00:14:16,200 --> 00:14:17,190
So for example,

362
00:14:17,190 --> 00:14:22,190
if I have $1 == "01", $1 == "02",

363
00:14:24,060 --> 00:14:26,490
this tells me I'm going to check the first field

364
00:14:26,490 --> 00:14:28,380
for any values of 01

365
00:14:28,380 --> 00:14:31,920
until I find ones that have 02.

366
00:14:31,920 --> 00:14:33,390
The next thing I want to talk about

367
00:14:33,390 --> 00:14:37,230
is known as a stream editor or sed command.

368
00:14:37,230 --> 00:14:40,050
The sed command is a program that you can use

369
00:14:40,050 --> 00:14:43,980
to modify text files according to various parameters.

370
00:14:43,980 --> 00:14:46,140
The sed command can also be used

371
00:14:46,140 --> 00:14:48,540
for global search and replace actions.

372
00:14:48,540 --> 00:14:51,480
Some common options here are going to be option d,

373
00:14:51,480 --> 00:14:52,890
which is used to delete lines

374
00:14:52,890 --> 00:14:55,770
that match a specific pattern or line number,

375
00:14:55,770 --> 00:14:59,820
option -n,p will be used to print only the lines

376
00:14:59,820 --> 00:15:01,530
that contain the pattern,

377
00:15:01,530 --> 00:15:03,990
option s is going to be used to substitute

378
00:15:03,990 --> 00:15:06,180
the first occurrence of the string in the file,

379
00:15:06,180 --> 00:15:08,700
and the option s,g is going to be used

380
00:15:08,700 --> 00:15:11,190
to globally substitute the original string

381
00:15:11,190 --> 00:15:12,570
with the replacement string

382
00:15:12,570 --> 00:15:14,520
for each occurrence in the file.

383
00:15:14,520 --> 00:15:16,290
The proper way to use this command

384
00:15:16,290 --> 00:15:19,680
is to type sed, the option, address, or action,

385
00:15:19,680 --> 00:15:21,660
and then the file names.

386
00:15:21,660 --> 00:15:25,050
Another command we need to talk about is the link command.

387
00:15:25,050 --> 00:15:27,060
This is known as ln.

388
00:15:27,060 --> 00:15:30,630
The ln command is used to create a link to a file.

389
00:15:30,630 --> 00:15:34,200
Linking enables a file in one directory, the link,

390
00:15:34,200 --> 00:15:37,470
to point to a file in another directory, the target.

391
00:15:37,470 --> 00:15:40,020
A link does not contain data on its own,

392
00:15:40,020 --> 00:15:42,540
only a reference to the target file.

393
00:15:42,540 --> 00:15:43,980
Any changes to the link

394
00:15:43,980 --> 00:15:46,770
will reflect into that target file as well.

395
00:15:46,770 --> 00:15:48,690
If you don't specify the link name,

396
00:15:48,690 --> 00:15:50,550
the ln command will create the link

397
00:15:50,550 --> 00:15:52,710
in your current working directory.

398
00:15:52,710 --> 00:15:54,210
The syntax of this command

399
00:15:54,210 --> 00:15:58,530
is ln, the options, the target name, and the link name.

400
00:15:58,530 --> 00:16:00,630
The ln command has various different options

401
00:16:00,630 --> 00:16:03,810
that you can use as well including -backup,

402
00:16:03,810 --> 00:16:07,151
which is used to back up existing destination files,

403
00:16:07,151 --> 00:16:09,300
F, which will be used to remove

404
00:16:09,300 --> 00:16:11,330
existing destination files,

405
00:16:11,330 --> 00:16:15,360
s, to make a symbolic link instead of a hard link,

406
00:16:15,360 --> 00:16:18,900
and -i, to prompt to remove destination files,

407
00:16:18,900 --> 00:16:23,400
and finally, -v, to print the name of a file before linking.

408
00:16:23,400 --> 00:16:24,870
Using the ln command,

409
00:16:24,870 --> 00:16:27,060
you can create two different types of links:

410
00:16:27,060 --> 00:16:30,000
these are called hard links and symbolic links.

411
00:16:30,000 --> 00:16:32,760
Now a hard link is a reference to another file

412
00:16:32,760 --> 00:16:35,640
and it enables the files data to have more than one name

413
00:16:35,640 --> 00:16:38,700
in different locations in the same file system.

414
00:16:38,700 --> 00:16:40,380
If the original file is deleted

415
00:16:40,380 --> 00:16:42,240
after the hard link is created,

416
00:16:42,240 --> 00:16:44,400
all of its contents will still be available

417
00:16:44,400 --> 00:16:45,960
in the linked file.

418
00:16:45,960 --> 00:16:48,360
A symbolic link also called a soft link

419
00:16:48,360 --> 00:16:50,250
is a reference to a file or directory

420
00:16:50,250 --> 00:16:52,890
that can span multiple different file systems.

421
00:16:52,890 --> 00:16:54,990
But if the original file or directory

422
00:16:54,990 --> 00:16:57,570
is deleted after the symbolic link is created,

423
00:16:57,570 --> 00:17:00,060
the original content is lost.

424
00:17:00,060 --> 00:17:01,770
In order to create a hard link,

425
00:17:01,770 --> 00:17:04,410
you would simply type in ln, the file you want to do,

426
00:17:04,410 --> 00:17:05,400
and then the link itself

427
00:17:05,400 --> 00:17:07,829
because the ln command uses hard links by default

428
00:17:07,829 --> 00:17:09,093
when creating a new link.

