Shell Script to Split a String
Last Updated :
20 Jun, 2021
Shell Scripting or Shell Programming is just like any other programming language. A shell is a special program that provides an interface between the user and the operating system.
In Linux/Unix the default shell used is bash and in windows, it is cmd(command prompt). We use the terminal to run a shell command. Shell scripts are saved with .sh file extension, and we can run the script with the help of the following command.
bash path/to/the/filename.sh
Example:
A simple shell program to print Hello Geeks!!.
#! /bin/bash
# echo is used to print
echo "Hello Geeks!!"
Here, #! /bin/bash is used to let know the Linux/Unix that it is a Bash Script and use the bash interpreter which resides in /bin to execute it.
Output:
First Shell Program
Shell Script to Split a String:
Let's discuss how we can split a string take the following string for an example:
string = "Lelouch,Akame,Kakashi,Wrath"
And we would like to split the string by "," delimiter so that we have :
- name="Lelouch"
- name ="Akame"
- name="Kakashi"
- name="Wrath"
Approach 1: Using IFS (Input Field Separator).
IFS stands for Internal Field Separator or Input Field separator variable is used to separate a string into tokens.
Original String :
string="Lelouch,Akame,Kakashi,Wrath"
After Splitting :
name = Lelouch
name = Akame
name = Kakashi
name = Wrath
String holds the input string value and IFS variable the delimiter on that we are going to separate the string. read -ra arr <<< "$string" convert the separated strings into an array, after that we are using for loop to traverse the array and print the value."@" in arr subscript shows that we are going through the whole array.
Script :
#! /bin/bash
# Given string
string="Lelouch,Akame,Kakashi,Wrath"
# Setting IFS (input field separator) value as ","
IFS=','
# Reading the split string into array
read -ra arr <<< "$string"
# Print each value of the array by using the loop
for val in "${arr[@]}";
do
printf "name = $val\n"
done
To run the script use the following command :
bash path/to/the/filename.sh
Output:
Script Output
Approach 2: Without using the IFS
Suppose input string is containing a particular word "anime" and we want to split the string on seeing "anime"
string = "anime Bleach anime Naruto anime Pokemon anime Monster anime Dororo"
Output :
Anime name is = Bleach
Anime name is = Naruto
Anime name is = Pokemon
Anime name is = Monster
Anime name is = Dororo
We can do this using Parameter Expansion.
Script :
#! /bin/bash
# Given string
string="anime Bleach anime Naruto anime Pokemon anime Monster anime Dororo"
# Syntax to replace all occurrences of "anime" with " "
arr=(${string//"anime"/ })
# Print each value of the array by using the loop
for val in "${arr[@]}";
do
printf "Anime name is = $val\n"
done
Output:
Script Output
Similar Reads
Bash Scripting - Split String In this article, we will discuss how to split strings in a bash script. Dividing a single string into multiple strings is called string splitting. Many programming languages have a built-in function to perform string splitting but there is no built-in function in bash to do so. There are various met
4 min read
Bash Scripting - String Bash String is a data type similar to integer or boolean. It is generally used to represent text. It is a string of characters that may also contain numbers enclosed within double or single quotes. Example: "geeksforgeeks", "Geeks for Geeks" or "23690" are strings Creating a String A basic declarati
2 min read
String Manipulation in Shell Scripting String Manipulation is defined as performing several operations on a string resulting change in its contents. In Shell Scripting, this can be done in two ways: pure bash string manipulation, and string manipulation via external commands. Basics of pure bash string manipulation: 1. Assigning content
4 min read
Batch Script - Strings A Bash script is a plain text file. This file contains different commands for step-by-step execution. These commands can be written directly into the command line but from a re-usability perceptive it is useful to store all of the inter-related commands for a specific task in a single file. We can u
4 min read
Bash Scripting - Substring sIn this article, we will discuss how to write a bash script to extract substring from a string. Extracting an Index-Based Substring There are various ways to obtain substring based on the index of characters in the string: Using cut commandUsing Bash substringUsing expr substr commandUsing awk comm
4 min read
How to Substring a String in Python A String is a collection of characters arranged in a particular order. A portion of a string is known as a substring. For instance, suppose we have the string "GeeksForGeeks". In that case, some of its substrings are "Geeks", "For", "eeks", and so on. This article will discuss how to substring a str
4 min read