Cut with delimiter in linux

How to define ‘tab’ delimiter with ‘cut’ in Bash?

Here is an example of using cut to break input into fields using a space delimiter, and obtaining the second field: cut -f2 -d’ ‘ How can the delimiter be defined as a tab, instead of a space?

6 Answers 6

Press Ctrl + V and then Tab to use «verbatim» quoted insert.

or write it like this to use ANSI-C quoting:

The $’. ‘ form of quotes isn’t part of the POSIX shell language (not yet), but works at least in ksh, mksh, zsh and Busybox in addition to Bash.

Neither works in Windows (e.g., Git Bash) obviously. Use @Mikel’s answer there (default delimiter is tab, simply omit -d argument).

@poige, that comes from ksh93 , not bash . That’s supported by ksh93, zsh, bash, mksh and FreeBSD sh at least (it might make it to the next major reversion of the POSIX standard specification for sh ).

-d delim Use delim as the field delimiter character instead of the tab character. 

But it is probably always safer to mention such flags explicitly, for both readability and portability. I can imagine some people designing a cut for Windows would not follow the complete standard.

This should be accepted answer: simplest code, simplest explanation. ya nailed it: youtube.com/watch?v=dpNTHl7y45Y

@WillemVanOnsem, if someone writes a version of cut for Windows and doesn’t follow the POSIX specification for it, there is no reason to assume that any POSIX script will work with that system. Stick to POSIX-specified features. Don’t try to allow for hypothetical future non-compliant implementations; that’s not what «portability» means.

This extracts the second tab-delimited field of each line of input from inputfile .

More generically, without requiring any invisible characters: Use tr to convert the delimiters to a format that can be specified more easily to cut .

$ echo -e "a\tb\tc" |tr '\t' ' ' |cut -d' ' -f2 b 

tr is a simple, but powerful, character matching and replacement tool.

But what if the input is abc(space)def(tab)ghi ? Your answer will yield def , but it should yield ghi . Similarly, if the input is ABC(tab)DEF(space)GHI , your answer will yield DEF , but it should yield DEF(space)GHI .

@G-Man: The space delimiter was only an example. Use whatever delimiter is appropriate for your data — a comma for example. echo -e «abc\tdef ghi» |tr ‘\t’ ‘,’ |cut -d’,’ -f2

Alternatively, one could wrap cut in a function.

function getColumns () < local -r delimiter="$" local -r columns="$" if [[ "$delimiter" == '\t' || "$delimter" == "tab" ]]; then cut "--fields=$" return fi cut "--delimiter=$" "--fields=$" > 

I use TAB and cut in these ways:

# quote the whole thing, use TAB escape cut "-d\t" -f 2 # just quote the tab escape cut -d "\t" -f 2 # Use Ctrl-V to quote Ctrl-I (TAB is Ctrl-I, see man ascii) cut -d^V^I -f 2 

You must log in to answer this question.

Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.

Читайте также:  Linux комбинации клавиш язык

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.13.43531

Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Linux cut Command Explained with 6 Examples

The cut command is a command-line utility that allows you to cut out sections of a specified file or piped data and print the result to standard output. The command cuts parts of a line by field, delimiter, byte position, and character.

In this tutorial, you will learn what the cut command is and how to use it.

Linux cut command tutorial.

cut Command Syntax

The cut command takes the following syntax:

Specifying an [option] is necessary. Otherwise, the command outputs an error. Available options are described in the following section.

For [file] , enter the name of the file you want to process. Not specifying a filename instructs the cut command to read from the standard input, in which case cut filters the pipeline. If you specify multiple filenames, the cut command concatenates the requested content.

Note: Another Linux command that produces formatted outputs is the awk command.

cut Command Options

The cut command options provide instructions on using a delimiter, cutting by byte position, field, or character. Use a single option for each cut command you run.

The available options are:

Option Description
-f (—fields=LIST) Select using a specified field, a field set, or a field range.
-b (—bytes=LIST) Select using a specified byte, a byte set, or a byte range.
-c (—characters=LIST) Select using a specified character, a character set, or a character range.
-d (—delimiter) Used to specify a delimiter to use instead of the default TAB delimiter.
—complement When specified, this option instructs cut to display all the bytes, characters, or fields, except the selected.
-s (—only-delimited) The default setting is to print the lines that don’t contain delimiter characters. Specifying the -s option instructs cut not to print the lines that don’t contain delimiters.
—output-delimiter By default, cut uses the input delimiter as the output delimiter. Specifying the —output-delimiter option allows you to specify a different output delimiter.
Читайте также:  Kali linux x64 iso

The -f , b , and -c options take the LIST argument, which is one of the following:

  • An integer N representing a byte, field or character, starting from 1.
  • Multiple integers, comma-separated.
  • A range of integers.
  • Multiple integer ranges, comma-separated.

Each range can be one of the following:

  • N- — Starts from the integer N (field, byte or character) up to the end of the line.
  • N-M — From the integer N up to integer M , inclusive.
  • -M — From the first field, byte, or character, up to the specified M field, byte, or character.

Linux cut Examples

Below are the most common cut command use cases.

Important: Pay attention to the locale of the file/command output you are processing. Cutting characters or bytes in a language other than English may produce an incorrect output if the character in question is longer than one byte.

Cut by Bytes

The -b option allows you to extract data using bytes. The syntax is:

The [LIST] argument are the bytes to extract from each line of [file] .

Depending on what you want to extract, you can cut a single byte, multiple bytes, or a byte range. To cut from a specific file, specify the filename at the end of the command. For example, we will use employees.txt for testing:

Using the cat command to show a file

Note: Use the cat command to show a file’s contents in the terminal.

To extract the first byte from each input line, run:

Extracting a specific byte from a file.

The command prints only the first byte from each file input line.

The following example shows how to pipe into the who command and extract the first 5 bytes from the output:

Extracting bytes from the who command output.

Cut by Characters

To cut by characters, specify the -c option. Cutting by characters is similar to cutting by bytes, except you need to specify the character position rather than the byte position. The syntax is:

The [LIST] argument specifies the characters to be extracted from each line of [file] .

Extracting specific character from a file using cut.

The command extracts everything from character 10 until the end of the line from each line of employees.txt. The results are printed in standard output.

The following example shows the result when a file isn’t specified and cut reads its input from standard input. Take a look at the who command output:

An example of the who command output.

The output indicates that one user is currently logged in. Use the cut command to extract the logged-in user’s username from the who command’s output:

Extracting usernames from who command output using cut.

In the example above, we instruct cut to extract characters 1 through 8 from each line of input. In case of multiple results, sort the results by appending the command with | sort .

Note: To know the character position, run the who command first and count out the appropriate character positions. The who command’s output has a fixed format, so the character positions don’t change. However, data isn’t always organized in a fixed manner in every command output, so make sure to check the output before piping it.

Читайте также:  Ibm lotus notes linux

Additionally, use cut to extract multiple different characters from a line. For example, display the username and login time of all logged-in users:

Extracting multiple characters from a line using cut.

Cut Based on a Delimiter

If the fields aren’t separated by the default tab character, use the -d option to specify a different delimiter. That means that the character specified after the -d option is considered the separator in the lines. The syntax is:

In place of the [delimiter] argument, specify the delimiter you want. You can use any character as a delimiter. Using the cut command to extract fields from a file without specifying the -d option means that the default delimiter is the tab character.

In the following example, we use whitespace as a delimiter and print the second field:

echo "phoenixNAP is a global IT services provider" | cut -d ' ' -f 2

Extracting fields from a file using cut and a delimiter.

Note: The echo command prints out a text string you provide as an output message.

Cut by Fields

When piping into commands whose output doesn’t have a fixed format (e.g., the who command), the -c option isn’t helpful. Using the -f option to separate by fields is a better choice in that case.

Extracting text from a file using fields.

In the example above, we used the -f option to extract the second field from the employees.txt file.

To cut specific fields from a file, specify a different delimiter. For example, the /etc/passwd file output contains all the users on your system, id numbers, home directory, etc.

Output of the /etc/passwd file.

The data in the /etc/passwd file isn’t aligned in the same way as the data in the who command output. Thus, you cannot extract all the users on the system by relying on the character number.

However, the fields in the /etc/passwd file are delimited by a colon. Hence, count the number of colons to extract the same fields. For example:

Filtering the /etc/passwd file output using the cut command.

The output returns each user in the system and their home directory, corresponding to fields 1 and 6, respectively.

Complement a Selection

The —complement option prints everything except for the character/byte/field at the specified position. For example, the following command prints all fields except the first and third:

cut employees.txt -f 1 --complement

An example of the cut --complement option.

Specify an Output Delimiter

When specifying multiple characters/bytes/fields, the cut command concatenates the output without a delimiter. Specify a delimiter in the output using the —output-delimiter option.

For example, to set the output delimiter to _ (underscore), use:

cut employees.txt -f 1,3 --output-delimiter='_'

Specifying an output delimiter in the cut command.

Note: If you are just starting with Linux commands, take a look at the comprehensive overview in our article Linux Commands All Users Should Know.

You now know what the Linux cut command is and how to use it to process a file or command output. Feel free to test out the different options to get used to them and maximize your use of the command line for manipulating data and command outputs.

Источник

Оцените статью
Adblock
detector