Redhat Linux 9: Điều hướng stdout, stdin stderr, lọc nội dung với grep



Redhat Linux 9: Điều hướng stdout, stdin stderr, lọc nội dung với grep

Redhat Linux 9:   Điều hướng stdout, stdin stderr, lọc nội dung với grep

Link playlist
Red Hat Enterprise Linux Administration
https://www.youtube.com/playlist?list=PLIpLw6v7Z1qnIUiJipjgYEHwxF16TZbCN
2023 09 23 12 21 16

Filtering output with grep and sed
The grep command is heavily used (and commonly mistyped) in system administration. It helps
when finding a pattern in a line, whether in a file or via standard input (STDIN).
Let’s do a recursive search of the files in /usr with find and put it in /root/usr-files.txt:

[root@rhel-instance ~]# ls -lh usr-files.txt
-rw-r–r–. 1 root root 2,1M Feb 18 12:38 usr-files.txt
As you can see, it’s a 2.1 MB file and it isn’t easy to go through. There is a utility in the system called
gzip and we want to know which files in /usr contain the gzip pattern. To do so, we can run the
following command:
[root@rhel-instance ~]# grep gzip usr-files.txt
/usr/bin/gzip
/usr/lib64/python3.9/__pycache__/gzip.cpython-39.opt-2.pyc
/usr/lib64/python3.9/__pycache__/gzip.cpython-39.opt-1.pyc
/usr/lib64/python3.9/__pycache__/gzip.cpython-39.pyc
/usr/lib64/python3.9/gzip.py
/usr/share/licenses/gzip
/usr/share/licenses/gzip/COPYING
/usr/share/licenses/gzip/fdl-1.3.txt
/usr/share/man/man1/gzip.1.gz
Filtering output with grep and sed 81
/usr/share/doc/gzip
/usr/share/doc/gzip/AUTHORS
/usr/share/doc/gzip/ChangeLog
/usr/share/doc/gzip/NEWS
/usr/share/doc/gzip/README
/usr/share/doc/gzip/THANKS
/usr/share/doc/gzip/TODO
/usr/share/info/gzip.info.gz
/usr/share/mime/application/gzip.xml
As you can see, we have found all the files with gzip under the /usr directory by creating a file with
all the content and searching through it with grep. Could we do the same without creating the file?
We sure could – by using a pipe. We can redirect the output of find to grep and get the same output:
[root@rhel-instance ~]# find /usr/ | grep gzip
/usr/bin/gzip
/usr/lib64/python3.9/__pycache__/gzip.cpython-39.opt-2.pyc
/usr/lib64/python3.9/__pycache__/gzip.cpython-39.opt-1.pyc
/usr/lib64/python3.9/__pycache__/gzip.cpython-39.pyc
/usr/lib64/python3.9/gzip.py
/usr/share/licenses/gzip
/usr/share/licenses/gzip/COPYING
/usr/share/licenses/gzip/fdl-1.3.txt
/usr/share/man/man1/gzip.1.gz
/usr/share/doc/gzip
/usr/share/doc/gzip/AUTHORS
/usr/share/doc/gzip/ChangeLog
/usr/share/doc/gzip/NEWS
/usr/share/doc/gzip/README
/usr/share/doc/gzip/THANKS
/usr/share/doc/gzip/TODO
/usr/share/info/gzip.info.gz
/usr/share/mime/application/gzip.xml
In this command, the standard output from find was sent to grep to process it. We can even count
the number of instances of files with wc, but this time, using the -l option to count the lines:
[root@rhel-instance ~]# find /usr/ | grep gzip | wc -l
18
Basic Commands and Simple Shell Scripts 82
We have now concatenated two pipes, one to filter the output and another to count it. We will find
ourselves doing this kind of plumbing often when searching for and finding information in the system.
Some very common options for grep are as follows:
• -i: for ignore-case. This will match the pattern whether it’s uppercase, lowercase, or a
combination thereof.
• -v: for invert match. This will show all entries that do not match the pattern being searched for.
• -r: for recursive. We can tell grep to search for a pattern in all the files within a directory
while going through all of them (if we have permission).
There is also a way to also filter columns in the output provided. Let’s say we have a list of files in our
home directory and we want to see its size. We run the following command:
[root@rhel-instance ~]# ls -l
total 1888
-rw——-. 1 root root    1393 Feb 18 19:45 anaconda-ks.cfg
-rw-r–r–. 1 root root      52 Feb 16 12:17 error.txt
-rw-r–r–. 1 root root       0 Feb 16 12:08 non-listing.txt
-rw-r–r–. 1 root root 1917837 Feb 16 12:40 usr-files.txt
-rw-r–r–. 1 root root     360 Feb 16 12:12 var-files.txt
Let’s say we only want the size, which is the fifth column, of the content that has files in its name.
We can use awk for that:
[root@rhel-instance ~]# ls -l | grep files | awk ‘{ print $5}’
1917837
360
The awk tool will help us to filter according to the correct column. It is very useful for finding identifiers
in processes or for getting a specific list of data from a long output.
Tip
Consider that awk is super powerful for processing output and that we will use the minimal
capability for it.
We could replace the separator with -F and get a list of available users in the system:

The awk and grep tools are very common processing tools in the life of a Linux sysadmin, and it is
important to understand them well to manage the output provided by the system. We have applied the
base knowledge to filter the output received by row and column. Let’s now move on to how to manage
files in a system so that we can better handle the stored output we have just generated.