Hello and welcome to this introductory video on Oracle Linux.
This video discusses and demonstrates useful tools available on Oracle Linux.
After watching this video, you will learn about:
grep
sed
awk
And finally, we'll look at redirecting output, input, and errors.
Grep stands for global regular expression print.
It is a command for line editing one edit at a time.
Grep accepts a regular expression as an argument which is used to match.
Grep can be used to search and match for text in processes, filenames, directories,
etc.
Let's first display a listing of all of the local files in our current working
directory with the ls command.
Let's use the cat command to take a look inside of the file myfile.
Use grep to search for the letter 'e' within myfile.
The results show all of the lower case instances of letter e in highlights.
Let's try searching for a number, this time the number 1.
Two instances of the number one are highlighted.
Another useful use case is searching for whole words.
Let's search for the word hello in myfile by using the quotes around hello.
Unfortunately, there wasn't a match.
However, recall there is a hello in the first line of the file.
Therefore, let's use the -i option to remove case sensitivity from grep.
The hello is returned.
Note, by default grep is case sensitive.
Lastly, let's search for a common word "the" in multiple files using the -r option.
The results show that we can search for word strings in not only one file but
multiple files as well.
Moving on to sed, sed is a stream-oriented editor that is non-interactive and uses regular
expressions for pattern matching.
Input from either a file or the keyboard is directed to standard output.
By default, standard output is sent to the screen but can also be directed to
a file.
Simply, sed interprets pattern matching or a script and then performs the actions listed
in the script.
The three primary purposes for sed are:
Automating editing actions for one or more files
Simplifying tasks performed on multiple files
Writing conversion programs.
Let's take a look at a couple sed examples.
Returning to the file myfile, let's replace any occurrence of world with earth.
We can also replace numbers as well using the following matching pattern
by reversing an entire number.
You can also create a script for sed to execute instead of entering in on
multiple lines.
Let's use the cat command to look at the script before executing.
Execute the sed command with the -f to tell it the name of the script filename
followed by the myfile.
The original values have returned to myfile therefore the command executed correctly.
From a high level point of view, awk is used with structured data for
manipulating data in a file or files.
Awk is written in scripts invoked by a shell wrapper or also can be called by
command line.
Some of the possibilities with awk are data processing, spell checking, And
an index processing.
Similar to sed, awk is stream oriented and also uses pattern matching with the
help of regular expressions.
Let's now look at a few examples of awk.
The first example reads the myfile contents and matches the lines with a 1.
Next, let's search for lines with an e and then prints out the output
awk can also read instructions from a file.
In the next example, I'll use a file called contacts as shown with the cat
command.
Again, use the cat command to view the awk_script before running.
As shown, the script is searching for letters, A, B, C to match in the contacts
file.
Use the awk command and the -f option to use the filename called contacts.
After running the awk command, the first field with matching letters A, B, and C
in the file contacts is matched and then sent to the output screen.
Linux commands have the power to redirect input and output using specific characters.
To redirect an output to another command use a pipe.
A simple example is redirecting a listing of files recursively to more using a pipe.
This creates a paging effect allowing you page through the output at your
discretion using the space bar.
To cancel, hit q.
Another type of redirection is the greater than character.
You can redirect text meant for a terminal screen instead to a file using the
echo command and the > greater than character.
Use the cat command to verify the contents of the new file myfile2.
Alternatively, the less than character can be used to read data from a file.
In this case the contents of myfile2 are read and redirected to the wc
command.
The -m option counts the number of characters read.
For this example, 13 characters are in the myfile2 file.
Lastly, error redirection can be used if an error is detected after running a
command.
Using the find command, let's search for files in all directories with the word
print in them.
As you can see, some of the files returned are not permitted because of access
rights.
Therefore, by using the 2> characters, any permission denied output is sent to
/dev/null a place to discard output with no return output.
And finally, instead of /dev/null we can send error messages to a file called
find_errors for later viewing if need be.
As you can see you, only the permission denied messages were sent to this file.
This concludes the video.
Thank you for watching.