Windows News and info 15th Anniversary 2009-2024

Other Operating Systems => Linux, gOS, Harmony OS, Moblin, Ubuntu, OpenSuse => Topic started by: javajolt on January 27, 2022, 04:56:23 PM

Title: The 5 Most Useful Linux Text-Manipulation Commands
Post by: javajolt on January 27, 2022, 04:56:23 PM
(http://i.postimg.cc/zDkWvmmT/best-linux-text-manipulation-commands-1.jpg)
Up your Linux game and learn more about text manipulation? Here are the top five Linux command-line tools to begin your journey.

Linux developers follow the philosophy of creating small programs that do one task and do it well. Take Linux text processing tools as an example, they are lightweight and have modular functionality. Even though these text manipulation tools differ in complexity and functionality, they come in handy in an environment where the graphical user interface isn't available.

The article covers the best Linux tools to read files and use regular expressions to perform operations on the selected text. It also covers their most basic functionality and examples for better understanding.

1. grep

grep is a Linux text-manipulating utility that searches for a string of characters or patterns known as regular expressions in a file or text. The grep tool belongs to the family of utilities that include egrep, fgrep, and grep, among which fgrep is the fastest of all, while grep is the easiest.

The general syntax for using grep is as follows:

Quote
grep -options string filename

For example, to search for the word "root" in the /etc/passwd file (http://www.makeuseof.com/etc-passwd-file-linux/):

Quote
grep root /etc/passwd

(http://i.postimg.cc/FRcZ8Y8c/Grep-string-example.png)

Some standard command-line examples to get started are:

(http://i.postimg.cc/pThF1396/Capture.png)

Similarly, you can use the ^ metacharacter with the grep command to display all the matching strings that begin with certain characters.

For instance, the following command pipes the env command output as an input to grep and displays variables that begin with "HO":

Quote
env | grep ^HO

(http://i.postimg.cc/7hcDtQ2m/Grep-env-variable.png)

2. awk

awk is a powerful scripting language and a command-line text-manipulation tool that can perform line-by-line scans and compare lines to patterns. The basic syntax of the awk command is an action defined between a single quotation mark and curly braces followed by the filename.

Quote
awk '{action}' filename

awk '{pattern; action}' filename

The utility searches the file using regular expressions and performs the function defined in the action parameter. awk executes the script on every line if you do not set a pattern, as shown below:

Quote
awk '{print $1}' awk_examples.txt

(http://i.postimg.cc/7Y8qzjqZ/Awk-String-Example.png)

...where $1 displays the first field of the awk_examples.txt file.

The following command performs the print function on the given pattern by replacing the second field "World" with "Alice," and displays the whole line ($0):

Quote
echo "Hello World" | awk '{$2="Alice"; print $0}'

Output:

Quote
Hello Alice

Similarly, you can use the function print $0 from the command above to emulate the grep functionality.

Quote
awk '/john/{print $0}' /etc/passwd

john:x:1001:1001::/home/john:/bin/sh

3. sort

sort is another Linux command-line utility that helps you display the content of the specified text file in a sorted format. For instance, you can pipe the output of the awk command as an input to the sort utility as follows:

Quote
awk '{print $1}' awk_examples.txt | sort > sort_text.txt

cat sort_text.txt

Output:

(http://i.postimg.cc/Znrn3Sg6/Sort-example.png)

4. sed

sed or stream editor takes input as a stream of characters and performs filtering and text transformations (delete, substitute, and replace) on the specified text.

You can use it in a script and edit files non-interactively. Hence, the most basic purpose of the utility is the substitution of string/characters. The general syntax is:

Quote
sed 's/string/substitution/option' file

Create a file using random sentences to practice and understand the working of this utility.

(http://i.postimg.cc/d0dbJVTR/Sed-example-file.png)

Let's replace the occurrence of the word "two" on every line of the file with "2" using the -g flag for global replacement, as follows:

Quote
sed 's/two/2/g' sed_examples.txt > sed_examples2.txt

Similarly, use the -d flag to delete a specific line from the file:

Quote
sed '2d' sed_examples.txt

(http://i.postimg.cc/RhVC3H7x/Sed-deletion-example.png)

You can also replace the string by specifying a line number (4 s/two/2/p) and only printing the replaced line as follows:

Quote
sed -n '4 s/two/2/p' sed_examples2.txt

(http://i.postimg.cc/yNBMYzhw/sed-replace-by-line-and-print-example.png)

The -n flag in the command above disables the automatic printing of the input stream to the output. You can use this option in your favor for replacing the grep utility functionality with sed.

For instance, you can modify the command above by only including a regex pattern /two/p such that the -p flag will only print the lines to the standard output stream.

Quote
sed -n '/two/p' sed_examples2.txt

(http://i.postimg.cc/j2XXXGnJ/sed-as-grep-example.png)

5. cut

The cut is another command-line utility that cuts/extracts parts of text from a line or file. It cuts the text based on a specified field, character, or byte position and pipes the result to the standard output.

The utility takes in the following syntax:

Quote
cut <options> file

Use the -b option to cut section or content using a specified byte or a range of bytes:

Quote
cut -b 1 cut_examples.txt

(http://i.postimg.cc/GtFcSjbD/Cut-Byte-Example.png)

Use the -c flag to extract text by specifying the positions of characters:

Quote
cut -c 1,3,5 cut_examples.txt

(http://i.postimg.cc/VLkThRsc/Cut-Character-Example.png)

Lastly, you can also extract text by specifying fields with the -f option and -d for space or field delimiter:

Quote
cut -d " " -f 1 cut_examples.txt

(http://i.postimg.cc/kMBpCyRN/Cut-Field-Example.png)

Here's the list of ranges with examples and descriptions that you can utilize with the character -c and byte -b options:

(http://i.postimg.cc/HnzxBC05/Capture2.png)

Note that you cannot define the ranges for text extraction by using the field -f option.

Manipulating Text With Linux Commands

Linux offers many programs and tools for handling and working around files or text. Learning them all might not be required as you can easily fill the gap with another once you have a good grip over one, like using sed as grep or awk as grep, but this can't be true for every tool.

Besides, Linux commands have a steep learning curve but once you develop the skill, they can prove to be very useful and effective in the life of any Linux user, especially a system administrator.

source (http://www.makeuseof.com/best-linux-text-manipulation-commands/)