BASH Programming

How to Extract Part of a String Using Bash cut and split Commands

While programming, there are often when we need to extract a specific portion of a string from a larger text. This can be done using various techniques and tools, and one of the most popular methods is the use of bash/cut/split commands in Linux/Unix.

These commands are powerful and versatile and can be used to extract specific parts of a string based on various delimiters such as spaces, commas, and semicolons. This article will explore how to extract parts of a string using bash/cut/split commands and provide practical examples to help you better understand how to use them effectively.

The cut Command

The cut command is used to extract sections from each line of a file or string. It is a simple and powerful command that can extract fields based on a delimiter or a specific character. The syntax for the cut command is as follows:

cut -d [delimiter] -f [field] [filename]

Here, the -d option specifies the delimiter used in the input file, and the -f option specifies the field(s) to be extracted. The filename argument is the input file to be processed. For example, suppose we have a file called testfile.txt, with the following content:

BMW,M5

Lexus,LS470

VOLVO,X70

If we want to extract the second field from each line, we can use the following shell script:

#!/bin/bash

cat testfile.txt

echo "Here is the Extracted Part:"

cut -d ',' -f 2 testfile.txt

Below is the output of the above-given code that displays the file and its extracted part:

The split Command

The split command is used to split a string into an array of substrings based on a delimiter. It is a built-in command in Bash that can be used to extract part of a string. The syntax for the split command is as follows:

IFS=[delimiter] read -ra [array_name] <<< "$[string]"

Here, the IFS variable specifies the delimiter used in the string, the read command reads the input and splits it into an array, and the <<< operator is used to pass the string as input.

For example, suppose we have a string called “BMW, M5”. If we want to extract the second field, we can use the following bash script:

#!/bin/bash

cat testfile.txt

echo "Here is the Extracted Part:"

IFS=',' read -ra fields <<< "BMW,M5"

echo ${fields[1]}

The Bash split command can also be used to extract multiple fields from a string by using multiple variables in the read command.

Conclusion

Bash provides several methods to extract part of a string, including the cut and split commands. The cut command can be used to extract fields based on a delimiter or a specific character, while the split command can be used to split a string into an array of substrings based on a delimiter. Understanding how to extract part of a string is an important skill for anyone working with Bash scripts.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.