HomeLinux8 Minimize Command Examples

8 Minimize Command Examples [Cut Parts of Lines in File]


As Linux customers, we work together with the textual content recordsdata frequently. One of many frequent operations we carry out on these recordsdata is textual content filtering. Linux offers many command-line utilities for textual content filtering, resembling – grep, fgrep, sed, awk, and the record goes on.

Nonetheless, on this information, we’ll focus on another text-filtering utility known as minimize, which is used to take away a specific part from the enter line. The minimize command performs filtering based mostly on the byte place, character, discipline, and delimiter.

On this newbie’s information, we’ll be taught concerning the minimize command with examples within the Linux command line. After following this information Linux command line customers will have the ability to use the minimize command effectively of their day-to-day life.

minimize Command Syntax

The syntax of the minimize command is rather like any different Linux command:

$ minimize <OPTIONS>... [FILE-1] [FILE-2] ...

Within the above syntax, the angular bracket (<>) represents the necessary arguments whereas the sq. bracket ([]) represents the non-compulsory parameters.

Now that we’re acquainted with the syntax of the minimize command. Subsequent, let’s create a pattern file to make use of for example:

$ cat file.txt
Linux Sample Text File
Linux Pattern Textual content File

1. Print First Byte of File

The minimize command permits us to extract the textual content based mostly on byte place utilizing the -b choice.

Let’s use the beneath command to extract the primary byte from every line of the file:

$ minimize -b 1 file.txt
Print First Byte from File
Print First Byte from File

On this instance, we are able to see that the minimize command reveals solely the primary character as a result of all characters are one byte lengthy.

2. Print A number of Bytes of File

Within the earlier instance, we noticed the way to choose a single byte from the file. Nonetheless, the minimize command additionally permits us to pick a number of bytes utilizing the comma.

Allow us to use the beneath command to pick the primary 4 bytes from the file:

$ minimize -b 1,2,3,4 file.txt
Print Multiple Bytes from File
Print A number of Bytes from File

On this instance, now we have chosen the consecutive bytes however that’s not necessary. We will use any legitimate byte place with the minimize command.

3. Print a Vary of Bytes of File

Within the earlier instance, we used commas to pick consecutive bytes. Nonetheless, that method is just not appropriate if we need to choose a lot of bytes consecutively. In such instances, we are able to use the hyphen (-) to specify the byte vary.

To grasp this, let’s use the 1-12 as a byte vary to pick the primary twelve bytes from every line:

$ minimize -b 1-12 file.txt

In an analogous means, we are able to choose a byte vary from the center as effectively. For instance, the beneath command selects bytes from column numbers 17 to 30:

$ minimize -b 17-30 file.txt
Print Range of Bytes from File
Print Vary of Bytes from File

4. Print Specify Begin Byte Place of File

Generally, we need to extract all of the textual content from a sure byte place. In such instances, we are able to omit the top byte place.

For instance, we are able to use the next command to print all bytes ranging from place 17:

$ minimize -b 17- file.txt 
Print Text from Certain Byte Position
Print Textual content from Sure Byte Place

Within the above command, 17 represents the beginning byte place whereas the hyphen (-) represents the top of the road.

5. Print Specify Finish Byte Place of File

In an analogous means, we are able to additionally specify the top byte place solely. For instance, the beneath command prints all of the bytes from the beginning of the road until the twelfth column:

$ minimize -b -12 file.txt
Priint Specify End Byte Position of File
Print Specify Finish Byte Place of File

Within the above command, the hyphen (-) represents the beginning of the road whereas 12 represents the top byte place.

6. Minimize First Byte by Character Place

In the previous few sections, we noticed the way to carry out textual content extraction based mostly on byte place. Now, let’s see the way to carry out the textual content extraction by character place.

To realize this, we are able to use the -c choice to chop the primary byte from the next multi-byte string:

$ echo école | minimize -b 1



Within the above output, we are able to see that, the minimize command reveals a query mark as a substitute of the character é. This occurs as a result of we try to print the primary byte from the multi-byte character.

Now, let’s use the -c choice to chop the identical multi-byte character and observe the consequence:

$ echo école | minimize -c 1

é

Within the above output, we are able to see that now the minimize command reveals the anticipated output.

It is very important word that, not all variations of the minimize instructions help the multi-byte characters. For instance, Ubuntu and its derivatives don’t help multi-byte characters.

To grasp this, let’s run the identical command on the Linux Mint which is derived from Ubuntu:

Cut Byte by Character Position
Minimize Byte by Character Place

Right here we are able to see that, the minimize command treats each multi-byte characters and single-byte characters the identical. Therefore it doesn’t generate the anticipated output.

7. Minimize a String by Delimiter in Linux

By default, the minimize command makes use of a TAB character as a delimiter. Nonetheless, we are able to override this default habits utilizing the -d choice.

Oftentimes, the -d choice is utilized in mixture with the -f choice which is used to pick a specific discipline.

To grasp this, let’s use the area character as a delimiter and print the primary two fields utilizing the -f choice:

$ minimize -d " " -f 1,2 file.txt
Cut String By Delimiter in File
Minimize String By Delimiter in File

Within the above instance, now we have used the comma with the -f choice to pick a number of fields.

8. Print All Bytes or Characters Besides Chosen Ones

Generally, we need to print all of the characters besides just a few ones. In such instances, we are able to use the --complement choice. Because the title suggests, this selection prints all of the columns besides the required ones.

$ minimize -c 1 --complement file.txt
Print Not Selected Fields in File
Print Not Chosen Fields in File

Within the above output, we are able to see that the --complement choice prints all of the characters besides the primary character.

It is very important word that, on this instance, now we have used commas to pick a number of fields. Nonetheless, we are able to additionally use the opposite supported ranges as effectively. We will discuss with the primary few examples of this tutorial to grasp extra concerning the ranges.

Conclusion

On this newbie’s information, we mentioned the sensible examples of the minimize command with textual content filtering based mostly on the byte place, character place, and delimiter.

Are you aware of another finest instance of the minimize command in Linux? Tell us your views within the feedback beneath.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments