مرکزی مواد پر جائیں
eLearner.app
ماڈیول 3 · سبق 2 از 2کورس میں 7/11~15 min
ماڈیول اسباق (2/2)

پائپ لائنز اور فلٹرز (گریپ اور |)

One of the most powerful features of the Linux command line is the ability to connect different commands so that the output of one becomes the input for the next. This technique is accomplished using pipelines and is commonly paired with the text-filtering command grep.


Filtering Text: grep

The grep (Global Regular Expression Print) command searches for a specific string of text within one or more files and prints the matching lines:

Bash
grep "string" [file_name]

Useful Options for grep

  • grep -i "error" server.log: Performs a case-insensitive search (ignores the difference between uppercase and lowercase, finding "Error", "error", or "ERROR").
  • grep -n "connection" server.log: Displays the line number along with the matching line.
  • grep -v "INFO" server.log: Inverts the search, displaying only the lines that do not contain the specified keyword.

Chaining Commands: The Pipeline |

The pipeline operator is represented by the vertical bar |. It sends the standard output of the command on the left directly to the standard input of the command on the right:

Bash
[command1] | [command2]

For instance, if a log file is huge and you want to search for a specific word without printing the whole file, you can combine cat and grep:

Bash
cat logs/server.log | grep ERROR

This reads the entire server.log file with cat, but instead of printing it to the screen, it pipes the stream to grep, which filters and displays only the lines containing the word "ERROR".


Usage Examples and Common Errors

If you search for a lowercase word (e.g. "post") in a file that contains it only in uppercase (e.g. "POST"), grep will not return anything:

Bash
grep "post" logs/access.log
# Output: (no results, search is case-sensitive by default)

To find the line regardless of its case, use the -i flag:

Bash
grep -i "post" logs/access.log
# Output:
# 127.0.0.1 - - [22/May/2026:10:10:00 +0000] "POST /login HTTP/1.1" 401 234

If the specified file does not exist, grep will return an error:

Bash
grep "ERROR" nonexistent_file.log
# Output:
# grep: nonexistent_file.log: No such file or directory

Try it yourself

Exercise 1: Search for errors in log files

ورزش#linux.m3.l2.e1
کوششیں: 0لوڈ ہو رہا ہے…

Filter the file 'logs/server.log' to extract and display only the lines containing the word 'ERROR' (case-sensitive).

ایڈیٹر لوڈ ہو رہا ہے…
اشارہ دکھائیں۔

Use the grep command followed by the word to search for and the file path.

3 کوششوں کے بعد حل دستیاب ہے۔

Exercise 2: Pipelines and filters

ورزش#linux.m3.l2.e2
کوششیں: 0لوڈ ہو رہا ہے…

Use the cat command to read the file 'logs/server.log' and pipe (|) its output to the grep command to filter and print only the lines containing the word 'WARNING'.

ایڈیٹر لوڈ ہو رہا ہے…
اشارہ دکھائیں۔

Connect cat logs/server.log to grep WARNING using the pipe '|' character.

3 کوششوں کے بعد حل دستیاب ہے۔

Exercise 3: Case-insensitive search

ورزش#linux.m3.l2.e3
کوششیں: 0لوڈ ہو رہا ہے…

Search for all lines containing the word 'post' (either uppercase or lowercase) in the 'logs/access.log' file using the appropriate grep flag.

ایڈیٹر لوڈ ہو رہا ہے…
اشارہ دکھائیں۔

Use grep with the -i option to ignore case sensitivity.

3 کوششوں کے بعد حل دستیاب ہے۔