Useful Commands For Filtering Text for Effective File Operations in Linux
Last Updated :
09 May, 2022
In this article, let us learn about different commands used in Linux for filtering text to implement effective file operations on Linux Machines.
What is a filter in Linux?
In Linux Operating System, Filters act as a specialized program to get desired output from the client/user by taking the help of other files and pipes to perform a series of operations for the specified results. This helps in processing information in an effective way for desired output generation.
Syntax:
[filter method] [filter option] [data | path | location of data]
Let us discuss different filter commands which follow the above syntax and provide desired results for end users, namely:
pr command
pr command helps in generating the input in a printable format, with a properly defined column structure.
Syntax of pr command:
pr [options][filename}
Sample code:
Let's say we created a file abc.txt that lists numbers from 1-to 10.
cat > file.txt
Output:
Now, let us apply a filter on the above file using the pr command :
pr -3 file.txt
Output:
Explanation :
By using the pr command in the above file, we filtered the contents of file.txt into 3 columns.
tr command
tr command is used, for translation or deletion of input string data, and also we can change the case of letters from upper to lower and vice versa. It helps in formatting the case of text data whenever needed.
Syntax of tr command:
tr [OPTION] SET1 [SET2]
Sample code:
Uppercasing the inputted string using tr command
echo "www.geeksforgeeks.com" | tr [:lower:] [:upper:]
Output:
Explanation:
Here we are converting the lowercase string into the uppercase format by using tr command.
find command
The find command can be used as a filter to search files seamlessly on the server console in Linux Machines, given on one condition that users are aware of the file name for which they are applying this filter command in Linux Machines.
Syntax of find command:
find [where to start searching from]
[expression determines what to find] [-options] [what to find]
Sample code:
Finding the file.txt text file in / (root) directory using the find command.
find / -name "file.txt"
Output :
Explanation:
Here, we are finding the file.txt formatted file in the root directory Linux system by using the find command.
uniq command:
The uniq command is used for filtering out the unique characters from a file containing duplicate input data.
Syntax of uniq command:
uniq [OPTION] [INPUT[OUTPUT]]
Sample code:
Let us try creating a text file names.txt that contains several duplicate names of input data.
cat > names.txt
Now, let's see the functionality of uniq command in filtering the above duplicate data :
uniq -c names.txt
Output:
Explanation:
We have only displayed the unique data or contents from the names.txt file by using uniq command.
sort command
The sort command is used for sorting the text data in ascending order.
Syntax of sort command:
sort filename
Sample code:
Let us create a text file chr.txt which has some random letters in random order :
cat > chr.txt
sort chr.txt
Output:
Explanation:
We have sorted the names.txt file in ascending order format by using the sort command along with the filename.
grep command
The Grep command will extract the matched string data from the target file.
Note: Grep, Egrep, Fgrep, Rgrep are all commands performing the same function i.e extracting the matched string data from the target input file in Command Line Interface, hence we discuss only the grep command here.
Syntax of grep command:
grep [options] pattern [files]
Sample code:
Let us create a text file that contains some sentences in text.
cat > files11.txt
Now, we will apply the grep filter on some text data in the above file to retrieve the desired value.
grep -i "ravi" files11.txt
Output:
Explanation:
Here, we are matching the particular string data with the contents of files11.txt.
sed command
sed Command, used for filtering and transforming complex data, acts as a powerful stream editor and is mostly applied in the shell or dev jobs for filtering out complex data. Using the sed command we can retrieve only the required lines of input text data by specifying the required index value.
Syntax of sed command:
sed OPTIONS... [SCRIPT] [INPUTFILE...]
Sample code:
Let us create a sample text file that contains some user names and their mail ids.
cat > data.txt
Now, let's implement the sed command to retrieve input values starting from the 2nd index to the 5th index value only.
sed -n '2,5p' data.txt
Output:
Explanation:
In this section, we have filtered and transformed the data.txt file contents and outputted less complex data which can be easily viewed by the users.
fmt command
fmt command is a Linux Filtering Command, which helps in reformatting input data and printing it with the result of standard output.
Syntax of fmt command
fmt [-WIDTH] [OPTION]... [FILE]...
Sample code:
Let us consider we had a text file that contains data of some names and we can reformat them using this fmt command.
cat data.txt
Now, let's reformat the data in the above file using fmt:
fmt -w 1 data.txt
Output :
Explanation:
We have reformatted the contents of data.txt file by using the fmt command. Our contents of data.txt have been formatted and now the contents of data.txt are in a more readable format.
more command
more command is used for retrieving analysis of text data in the file, it takes big size file and displays it in page format and one important thing we need to note is, page down and page up keys do not work and we need to click on enter key for the display of new records on screen. To apply more command and view its working functionality, we need to take any system folder on Linux Machines that contain a huge set of text data. For this reason, I have chosen /var/log/messages folder.
Syntax of more command:
more [-options] [-num] [+/pattern] [+linenum] [file_name]
what is /var/log/messages ?
/var/log/messages is a system-managed folder in Linux that contains all system messages and notifications recorded at System Boot, This folder has all logs of mail, kernel, auth, daemon, etc.
Sample code:
cat /var/log/messages | more
Output:
Explanation:
Here we can see the output as static text data representing a complex chunk of the input text data, in contrast, if we use the normal cat /var/log/messages command, we will see a continuous running logs output dynamically, we cannot capture that output in a picture and show you here, but you can try it useful with above-mentioned command, surely you would notice the difference and also please note that we have a "more" option, by clicking enter we can see the continuation of the output data.
less command
The working of less command is similar to that of more command but the only difference is the less command will display much faster in speed the desired output for which applied and also page up and page down option exists for the less command which is not in the case of more command discussed in above lines.
Syntax of less command:
less filename
Sample code:
To test the working of less command, let us take the same /var/log/messages which we implemented our more command.
cat /var/log/messages | less
Output:
Explanation:
Please note that we get the same output result like more command but only a difference in display speed and, we had page up, page down options through which we can scroll for continuing the output result logs by either using the mouse cursor or keyboard buttons.
Similar Reads
How to Find a File in Linux | Find Command The find command in Linux is used to search for files and directories based on name, type, size, date, or other conditions. It scans the specified directory and its sub directories to locate files matching the given criteria.find command uses are:Search based on modification time (e.g., files edited
9 min read
How to sort lines in text files in Linux | sort Command The sort command in Linux is used to sort a file, arranging the records in a particular order. By default, the sort command sorts file assuming the contents are ASCII. Using options in the sort command can also be used to sort numerically.sort is a command that sorts text files alphabetically, numer
7 min read
How to View the Content of File in Linux | cat Command The cat command in Linux is more than just a simple tool, it's a versatile companion for various file-related operations, allowing users to view, concatenate, create, copy, merge, and manipulate file contents. Let's see the details of some frequently used cat commands, understanding each example alo
7 min read
How to Use the grep Command in Linux with Examples? Grep is a very powerful utility in Linux that is used for searching patterns within files or a stream of text. It's one of those essential tools that system administrators and developers use for parsing logs, cleaning up data, or otherwise dealing with large text apa. This tutorial will walk you thr
4 min read
Useful and time saving bash commands in Linux There are multiple helpful Bash scripts that will make your Linux terminal experience fast and seamless. In Linux, you can automate and execute numerous day-to-day tasks using plain commands â without laying hands on the GUI. These bash script examples assist you in automating file operations, list
5 min read
How to Find all Files Containing Specific Text (string) on Linux Suppose you are looking for a file in Linux, but you have forgotten its name. You only remember the contents of the file. How will you find the file in this case? Well, there are some useful methods that will help you find a file containing a specific text (or string) in Linux. The string needs to b
4 min read
file command in Linux with examples The 'file' command in Linux is a vital utility for determining the type of a file. It identifies file types by examining their content rather than their file extensions, making it an indispensable tool for users who work with various file formats. The file type can be displayed in a human-readable f
3 min read
How to List Open Files in Linux | lsof Command In the world of Linux, understanding and managing open files is crucial for system administrators and users alike. The Linux operating system provides a powerful utility called lsof (List Open Files) that allows users to gain insights into the files currently open on their system. In this article, w
7 min read
How to Edit Text Files in Linux: Proven 3 Methods Need to tweak a file in Linux but not sure where to start? Editing text files in Linux is a handy skill that can help you fix settings, write scripts, or just note down notes all from your keyboard. This blog is here to help you get started, even if youâre new to Linux. Weâll show you simple ways to
4 min read
fgrep command in Linux with examples The 'fgrep' filter is used to search for the fixed-character strings in a file. There can be multiple files also to be searched. This command is useful when you need to search for strings that contain lots of regular expression metacharacters, such as "^", "$", etc. This makes 'fgrep' particularly v
4 min read