Complement Output (--complement) Using cut Command

–complement: As the name suggests it complement the output. This option can be used in the combination with other options either with -f or with -c.

cut --complement -d " " -f 1 state.txt

–complement

cut --complement -c 5 state.txt

–complement

cut command in Linux with examples

The cut command in linux is a command for cutting out the sections from each line of files and writing the result to standard output. It can be used to cut parts of a line by byte position, character, and field. The cut command slices a line and extracts the text. It is necessary to specify an option with a command otherwise it gives an error. If more than one file name is provided then data from each file is not preceded by its file name.

Table of Content

  • Syntax of cut Command
  • Options Available in cut Command
  • Practical Examples of cut Command
  • Extract Specific Bytes (-b) Using cut Command
  • Cut by Character (-c) Using cut Command
  • Cut by Field (-f) Using cut Command
  • Complement Output (–complement) Using cut Command
  • Output Delimiter (–output-delimiter) Using cut Command
  • Display Version (–version) Using cut Command
  • How to use tail with pipes(|) in cut Command
  • Frequently Asked Questions on cut Command in Linux – FAQs

Similar Reads

Syntax of cut Command

The basic syntax of the cut command is:...

Options Available in cut Command

Here is a list of the most commonly used options with the `cut` command:...

Practical Examples of cut Command

Let us consider two files having name state.txt and capital.txt contains 5 names of the Indian states and capitals respectively....

Extract Specific Bytes (-b) Using cut Command

-b(byte): To extract the specific bytes, you need to follow -b option with the list of byte numbers separated by comma. Range of bytes can also be specified using the hyphen(-). It is necessary to specify list of byte numbers otherwise it gives error....

Cut by Character (-c) Using cut Command

-c (column): To cut by character use the -c option. This selects the characters given to the -c option. This can be a list of numbers separated comma or a range of numbers separated by hyphen(-)....

Cut by Field (-f) Using cut Command

-f (field): -c option is useful for fixed-length lines. Most unix files doesn’t have fixed-length lines. To extract the useful information you need to cut by fields rather than columns. List of the fields number specified must be separated by comma. Ranges are not described with -f option. cut uses tab as a default field delimiter but can also work with other delimiter by using -d option....

Complement Output (--complement) Using cut Command

–complement: As the name suggests it complement the output. This option can be used in the combination with other options either with -f or with -c....

Output Delimiter (--output-delimiter) Using cut Command

–output-delimiter: By default the output delimiter is same as input delimiter that we specify in the cut with -d option. To change the output delimiter use the option –output-delimiter=”delimiter”....

Display Version (--version) Using cut Command

–version: This option is used to display the version of cut which is currently running on your system....

How to use tail with pipes(|) in cut Command

The cut command can be piped with many other commands of the unix. In the following example output of the cat command is given as input to the cut command with -f option to sort the state names coming from file state.txt in the reverse order....

Frequently Asked Questions on cut Command in Linux – FAQs

How do I use the cut command to extract specific columns from a file?...

Conclusion

In this article we discussed the `cut` command in Linux which is a versatile tool for extracting specific sections from files based on byte position, character, or field. It slices lines of text and outputs the extracted data. Failure to specify an option with the cut command results in an error. Multiple files can be processed, but the output does not include the file names. Options such as `-b`, `-c`, and `-f` allow extraction by byte, character, and field, respectively. The --complement option inverts the selection, printing what is not selected, and --output-delimiter changes the output delimiter. The command also includes options for version display and can be used in combination with other commands through pipes for additional processing....