• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

Day 17/ 30 Days of Linux Mastery: Grep Command

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
Table of Contents

  • Introduction
  • What is grep?
  • Core grep Commands
  • Real-World Scenario: Using grep Commands
  • Conclusion
  • Let's Connect


Introduction


Welcome back to day 17!. Today, we are talking about a core command in Linux, the grep command.

When I think of grep I simply think of a filter.

If you have ever needed to search for a word inside a file, filter logs, or analyze output from a command, grep is your go-to tool.

Let’s get into it!


What is grep?


grep stands for Global Regular Expression Print. It searches for lines in a file or input that match a given pattern and prints them out.

You can use it to:

  • Search through config files
  • Analyze logs
  • Combine it with other commands to extract exactly what you need


Core grep Commands


Before we list the core grep commands, here is the basic syntax;


grep [options] pattern [file...]

Here are the grep commands and their use;

grep CommandDescription
-iCase-insensitive search
-vInvert match (show lines that do not match)
-r or -R Recursive search through directories
-nShow line numbers of matches
-cCount the number of matching lines
-lList only the filenames that contain the match
-eUse multiple patterns
--color=autoHighlight the match in color for readability


Real-World Scenario: Using grep Commands

  • Let's say we have an error log file.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • We decide to filter the file and select only the lines with ERROR

grep "ERROR" demologs.txt


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • Show line numbers of the errors found

grep -n "ERROR" demoerrorlog.txt


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • Let's imagine there was a word 'error', but not in uppercase format. We want to filter for errors but make it case-insensitive.

grep -i "error" demologs.txt


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



  • Exclude Certain Matches

grep -v "ERROR" demologs.txt - # Case-Sensitive
grep -iv "error" demologs.txt - # Case-Insensitive


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



Note:
grep -v "error"
This excludes all lines that contain the exact string "error" (case-sensitive).
So, it will only remove lines that match "error" in lowercase. It will still show lines like "Error" or "ERROR".

grep -iv "error"
This adds the -i flag, which makes the search case-insensitive, so it excludes lines containing error, Error, ERROR, or any variation in letter case.
The result is cleaner if you are trying to remove all forms of "error" regardless of case.

Conclusion


This is a job-ready skill. As a Linux or DevOps engineer, being able to search logs, filter noise, and pinpoint issues quickly with grep can save valuable time in production environments.

If this is helpful to you, feel free to bookmark, comment, like and follow me for Day 18!


Let's Connect!


If you want to connect or share your journey, feel free to reach out on

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

.
I am always happy to learn and build with others in the tech space.

#30DaysLinuxChallenge #Redhat#RHCSA #RHCE #CloudWhistler #Linux #Rhel #Ansible #Vim #CloudComputing #DevOps #LinuxAutomation #IaC #SysAdmin#CloudEngineer


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу