Skip to content

Latest commit

ย 

History

History
162 lines (129 loc) ยท 4.28 KB

File metadata and controls

162 lines (129 loc) ยท 4.28 KB

>Grep Paste Command Example โœ‚๏ธ

๐Ÿ”— Paste Command in Linux with Examples

>Grep Cut Command Example โœ‚๏ธ

๐Ÿ”— Cut Command in Linux with Examples


1๏ธโƒฃ Examples of grep Command (Explanation in English) ๐Ÿง

Example 1๏ธโƒฃ: Searching for a specific pattern in a file ๐Ÿ”

$ grep "error" system_logs.txt

๐Ÿ“ Explanation:
This command searches for the word "error" in the file system_logs.txt and displays the matching lines.

๐ŸŽฏ Output (Example):

2024-12-28 10:22:30 Error: File not found
2024-12-28 11:01:02 Error: Permission denied
2024-12-29 14:10:05 Error: Out of memory

Example 2๏ธโƒฃ: Using Regular Expressions (Regex) with grep ๐Ÿ”ค

$ grep -E "^2024-12-28" system_logs.txt

๐Ÿ“ Explanation:
This command displays only those lines from system_logs.txt that start with 2024-12-28. Here, -E enables regular expressions.

๐ŸŽฏ Output (Example):

2024-12-28 10:22:30 Error: File not found
2024-12-28 11:01:02 Error: Permission denied

2๏ธโƒฃ Examples of cut Command (Explanation in English) โœ‚๏ธ

Example 1๏ธโƒฃ: Extracting specific columns from a CSV file ๐Ÿ“Š

$ cut -d ',' -f 1,3 employees.csv

๐Ÿ“ Explanation:

  • -d ',' โ†’ Defines comma (,) as the delimiter.
  • -f 1,3 โ†’ Extracts 1st and 3rd columns from employees.csv.

๐ŸŽฏ Output (Example):

John, Developer
Alice, Manager
Bob, Tester

Example 2๏ธโƒฃ: Extracting columns from a space-separated file ๐Ÿ“„

$ cut -d ' ' -f 2,4 data.txt

๐Ÿ“ Explanation:
This command extracts 2nd and 4th columns from data.txt, using space as the delimiter.

๐ŸŽฏ Output (Example):

Developer, New York
Manager, California
Tester, Texas

3๏ธโƒฃ Examples of paste Command (Explanation in English) ๐Ÿ“

Example 1๏ธโƒฃ: Merging two files side-by-side ๐Ÿ†š

$ paste file1.txt file2.txt

๐Ÿ“ Explanation:
This command joins the contents of file1.txt and file2.txt side-by-side, separating columns with a tab (\t).

๐ŸŽฏ Output (Example):

Line 1 from file1    Line 1 from file2
Line 2 from file1    Line 2 from file2
Line 3 from file1    Line 3 from file2

Example 2๏ธโƒฃ: Merging columns using tabs ๐Ÿ“‘

$ paste -d '\t' column1.txt column2.txt

๐Ÿ“ Explanation:
This command merges column1.txt and column2.txt using a tab (\t) as a separator.

๐ŸŽฏ Output (Example):

John    Developer
Alice   Manager
Bob     Tester

4๏ธโƒฃ Combining grep, cut, and paste Together ๐Ÿ› ๏ธ

Example 1๏ธโƒฃ: Filtering and formatting logs ๐Ÿ“œ

$ grep "error" system_logs.txt | cut -d ' ' -f 1,3 | paste -s -d ',' -

๐Ÿ“ Explanation:

  1. grep "error" system_logs.txt โ†’ Searches for "error" in system_logs.txt.
  2. cut -d ' ' -f 1,3 โ†’ Extracts 1st and 3rd columns (date and error type).
  3. paste -s -d ',' - โ†’ Joins output into a single line, separated by commas.

๐ŸŽฏ Output (Example):

2024-12-28, Error
2024-12-29, Error

Example 2๏ธโƒฃ: Extracting specific lines and filtering important entries ๐Ÿ“„

$ paste file1.txt file2.txt | cut -d '\t' -f 1 | grep "important"

๐Ÿ“ Explanation:

  1. paste file1.txt file2.txt โ†’ Merges file1.txt and file2.txt.
  2. cut -d '\t' -f 1 โ†’ Extracts only the first column.
  3. grep "important" โ†’ Filters lines containing "important".

๐ŸŽฏ Output (Example):

important log entry 1
important log entry 2

Example 3๏ธโƒฃ: Processing and merging filtered logs ๐Ÿš€

$ grep "successful" server_logs.txt | cut -d ' ' -f 1,2 | paste -d ',' - results.txt

๐Ÿ“ Explanation:

  1. grep "successful" server_logs.txt โ†’ Filters lines containing "successful".
  2. cut -d ' ' -f 1,2 โ†’ Extracts date and time columns.
  3. paste -d ',' - results.txt โ†’ Merges filtered data with results.txt, separating by commas.

๐ŸŽฏ Output (Example):

2024-12-28, 12:00:00, success
2024-12-28, 14:30:00, success

โšก