Houston  Sipes

Houston Sipes

1603206000

Play with text in Linux: GREP, CUT, AWK, SED

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.

Linux Text Processing Tools

Here are the some most important test procession tool that we will discuss in this blog

  • Grep
  • Cut
  • Awk
  • Sed

GREP

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.
Example:
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

What is GEEK

Buddha Community

Play with text in Linux: GREP, CUT, AWK, SED
Houston  Sipes

Houston Sipes

1603206000

Play with text in Linux: GREP, CUT, AWK, SED

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.

Linux Text Processing Tools

Here are the some most important test procession tool that we will discuss in this blog

  • Grep
  • Cut
  • Awk
  • Sed

GREP

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.
Example:
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

Hire Dedicated Linux Developer

Looking to develop real-time applications?

Hire Dedicated Linux Developer from HourlyDeveloper.io, we have dedicated developers who have vast experience in developing applications for Linux and UNIX operating systems and have in-depth knowledge of their processes, kernel tools, internal architectures, and development packages.

Consult with experts:- https://bit.ly/2ZQ5ySP

#hire linux dedicated developer #linux developer #linux development company #linux development services #linux development #linux developer

Maud  Rosenbaum

Maud Rosenbaum

1597289640

5 Best CLI Tools to Search Plain-Text Data Using Regular Expressions

This guide takes a tour of some of the best command-line tools that are used for searching matching strings or patterns in text files. These tools are usually used alongside regular expressions – shortened as REGEX – which are unique strings for describing a search pattern.

Without much further ado, let’s dive in.

1. Grep Command

Coming in the first place is the grep utility tool – is an acronym for Global Regular Expression Print, is a powerful command-line tool that comes in handy when searching for a specific string or a pattern in a file.

Grep ships with modern Linux distributions by default and gives you the flexibility to return various search results. With grep, you can perform a vast array of functioning such as:

  • Search for strings or matching patterns in a file.
  • Search for strings or matching patterns in Gzipped files.
  • Count the number of string matches.
  • Print the line numbers that contain the string or pattern.
  • Search recursively for the string in directories.
  • Perform a reverse search ( i.e. Display results of strings not matching the search criteria).
  • Ignore case sensitivity when searching for strings.

The syntax for using the grep command is quite simple:

$ grep pattern FILE

For example, to search for the string ‘Linux‘ in a file, say, hello.txt while ignoring case sensitivity, run the command:

$ grep -i Linux hello.txt

Search For String in a File

Search For String in a File

To get more options that you can use with grep, simply read our article that examples more advanced grep command examples.

2. sed Command

Sed – short for Stream Editor – is another useful command-line tool for manipulation text in a text file. Sed searches, filters and replaces strings in a given file in a non-interactive manner.

By default, sed command prints the output to STDOUT (Standard Out), implying that the result of the execution is printed on the terminal instead of being saved in a file.

Sed command is invoked as follows:

$ sed -OPTIONS command [ file to be edited ]

For example, to replace all instances of ‘Unix‘ with ‘Linux‘, invoke the command:

$ sed 's/Unix/Linux' hello.txt

Replace String in a File

Replace String in a File

If you want to redirect output instead of printing it on the terminal, use the redirection sign ( > ) as shown.

$ sed 's/Unix/Linux' hello.txt > output.txt

Redirect Output to File

Redirect Output to File

The output of the command is saved to the output.txt file instead of being printed on the screen.

To check out more options that can be used, once again check out the man pages.

$ man sed

3. Ack Command

Ack is a fast and portable command-line tool written in Perl. Ack is considered a friendly replacement for grep utility and outputs results in a visually appealing manner.

Ack command searches the file or directory for the lines that contain the match for the search criteria. It then highlights the matching string in the lines.

Ack has the capacity to distinguish files based on their file extensions, and to a certain extent, the content in the files.

Ack command syntax:

$ ack [options] PATTERN [FILE...]
$ ack -f [options] [DIRECTORY...]

For example, to check for the search term Linux, run:

$ ack Linux hello.txt

Check a String in a File

Check a String in a File

The search tool is quite intelligent and If no file or directory is provided by the user, it searches the current directory and subdirectories for the search pattern.

In the example below, no file or directory has been provided, but ack has automatically detected the available file and searched for the matching pattern provided.

$ ack Linux

Search String in a Directory

Search String in a Directory

To install ack on your system run the command:

$ sudo apt install ack-grep    [On Debian/Ubuntu]
$ sudo dnf install ack-grep    [On CentOS/RHEL]

#awk command #linux commands #sed command #commandline tools #linux tricks #linux

How I Switched from Windows 10 to Linux Mint

This article is all about my journey on switching from Windows 10 to Linux Mint 20, how I got easily adapted to the Linux environment, and some resources that helped me to set up a perfect Desktop environment.

Uncertainty

Ok, now I have decided to switch to Linux but here comes the first question. Which distro will satisfy my needs both in terms of GUI and other aspects? Linux is not something new to me since I have been working with RHEL based distros in my work for the past 4 years with the command-line.

I know RHEL based distros are good for enterprises but not for personalized desktop environments, at least that’s what I am thinking till now. So I started my research to find the distro that should be easy for me to use and at the same time should have good community support if in case I ran into some problem. Among many Linux distros, I drilled down my list to 4 flavors.

Related ArticleThe Best Linux Distributions for Beginners

Before deciding the Distro it is necessary you formulate the list of tools/programs or packages needed and check if the distro you choose provides all those features.

For me, I use Linux for two main purposes: one is for my professional development work, writing articles, and second for my personal use like Video editing and Movies. Most of the popular software are created to be compatible with Windows, macOS, and Linux like Sublime TextVSCodeVLC Media PlayerFirefox/Chromium browser. Other than these software, cloud-based services make our life easy Like Microsoft Office 365 or G Suite.

#linux distros #linux mint #linux distros #linux mint tips #linux

I am Developer

1620616862

How to Delete Directories and Files in Linux using Command Line

In this remove or delete directories and files linux tutorial guide, you will learn how to remove empty directory and non empty directory linux using command line. And as well as how to remove/file files linux using command line.

If you work with Linux then you will need the following:

  • how to remove empty directory in linux,
  • how to remove non empty directory,
  • how to remove directory without confirmation linux
  • how to remove files with and without confirmation in linux.

So, this tutorial guide will show you you how to use the rmunlink, and rmdir commands to remove or delete files and directories in Linux with and without confirmation.

https://www.tutsmake.com/how-to-remove-directories-and-files-using-linux-command-line/

#how to delete directory in linux #how to remove non empty directory in linux #remove all files in a directory linux #linux delete all files in current directory #linux delete all files in a directory recursively #delete all files in a directory linux