Play with text in Linux: Linux is a widely-used open-source operating system that provides a large number of text processing tools. In our everyday work, we need to search text, extract parts of the text, modify the text, and sort text. Linux shell has a number of useful tools that help us do various text processing tasks. In this blog, we are going to learn some most important text processing tools.
Here are the some most important test procession tool that we will discuss in this blog
GREP is a multi-purpose file search tool that uses Regular Expressions. The grep stands for “global regular expression print,” processes text line by line, and prints any lines which match a specified pattern. The grep command is used for searching the text from the file according to the regular expression. By default, grep displays the matching lines. Grep is considered to be one of the most useful commands on Linux and Unix-like operating systems. grep is a powerful file pattern searcher in Linux.
Syntax:
grep [options] pattern [files]
Most important Options:
-c: Count the number of lines that match a pattern.
-h: Display the matched lines, but do not display the filenames.
-i: Ignore case for matching.
-l: Displays list of a filenames only.
-n: Display the matched lines with line numbers.
-v: Prints all the lines that do not match the pattern.
-e exp: Specifies expression with this option. Can use multiple times.
-f file: Takes patterns from a file.
-E: Treats pattern as an extended regular expression (ERE).
-w: Match whole word.
-o : Print only the matched parts of a matching line.
Consider the below file as an input:
$cat > grepExample.txt
GREP is a multi-purpose file search tool that uses Regular Expressions.
The grep command is used for searching the text from the file according to the regular expression.
grep is a powerful file pattern searcher in Linux.
1\. Case insensitive search
$ grep -i "GRep" grepExample.txt
output:
GREP is a multi-purpose file search tool that uses Regular Expressions.
The grep command is used for searching the text from the file according to the regular expression.
grep is a powerful file pattern searcher in Linux.
2\. Displaying the count of the number of matches
$ grep -c "grep" grepExample.txt
output: 2
3\. Search the whole words in a file
$ grep -w "grep" grepExample.txt
output:
The grep command is used for searching the text from the file according to the regular expression.
grep is a powerful file pattern searcher in Linux.
4\. Displaying only the matched pattern
$ grep -o "grep" grepExample.txt
output:
grep
grep
#devops #linux #awk #cut #grep #sed #shellscript