Shell Script to Split a String - GeeksforGeeks

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
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
simranarora5sos

Picked


Shell Script


Linux-Unix

Writing code in comment? 
                                    Please use ide.geeksforgeeks.org, 
                                    generate link and share the link here.
                                
scp command in Linux with Examples
nohup Command in Linux with Examples
mv command in Linux with examples
chown command in Linux with Examples
Docker - COPY Instruction
Thread functions in C/C++
nslookup command in Linux with Examples
SED command in Linux | Set 2
Named Pipe or FIFO with example C program
uniq Command in LINUX with examples