Open In App

Shell Script to Separate Colon From the Given Input

Improve
Improve
Like Article
Like
Save
Share
Report

Linux is popular amongst developers and in the computer science world for its ability to provide the terminal/command-line interface (CLI) functionality. Through CLI a user can execute commands to perform desired tasks. Bash often interchangeably referred to as the shell is a popular scripting language in which a set of commands can be executed via CLI.
Bash offers many ways of accepting inputs (As many as one wants) from the user like Command Line Arguments, Prompt based, etc. Often Command Line Arguments inputs are faster in processing as they are provided before the start of script execution. Prompt-based inputs are collected dynamically from the user if they are required. In our problem statement, we are sure we would be getting the input items, so we will go ahead and assume these items are collected as Command Line Arguments.

A delimiter is one in simple language that can be explained as a separator which based on some business logic separates the items. Here in the problem statement, consider Colon (:) as a delimiter. We all are familiar with delimiters as we would have come across .csv (Comma Separated Values) files in which Comma (,) acts as a delimiter. So separating or parsing inputs based on delimiter is usually the first step of any business logic processing, so the following problem statement and its solution will have a varied range of applications in bash scripting.

Problem Statement: For the given input item, separate the items using delimiter as colon. The following are various use cases for the same,

Sample Input:

CASE 1: Simple input items separated by colon. Example: First Names/Words separated by Colon.
./Colon.sh Geek:For:Geeks:Good

CASE 2: Special Characters inclusive input like "@!"£$%." etc. Example: Email Addresses separated by colon 
./Colon.sh "abc@gmail.com:xyz@gmail.com:Exclamation!"

CASE 3: Space is included in the input items/words. Example: Full Names Separated by Colons
./Colon.sh "Geeks for Geeks is good: Open Source Community: Great Contributions"

CASE 4: Space as well as Quotes are Included in the Input Item. Example: Combination of case 2 and 3.
./Colon.sh 'Geeks "ForGeeks":Amazingly "Awesome":Detailed "Examples"'

Approach:

  1. Use the cut command for delimiter-based separation of the given input items with the help of a while loop to traverse through them.
  2. Initially, define a field variable (keeping the name of the variable as a field because the cut command has the option -f which is used in the script which in turn means field number) and set it to 1 as we are starting from the first item.
  3. Start the while loop which will be used to traverse through the given input items individually.
  4. Then echo the input items and pipe ( | ) them through to the cut command which acts as input to cut command and using a delimiter (-d) and field-number (-f) option of cut each item is printed based on field number separated by delimiter as colon (:).
  5. After printing each input item increment the field variable to process/print the next item.
  6. There is a conditional statement to check if the fieldth item is empty (which in turn means all input items are processed/Separated and printed successfully) if that is empty the while loop and shell script are exited successfully else continue with the printing of input current item and incrementing field variable until all input-items are processed.

Shell Script: The following is the Shell Script (Colon.sh) which processes the 1st argument as the given list of items and separates them based on colon “:” as the delimiter. 

#! /bin/bash

# Set initial field for cut to 1
field=1

# Using while loop to traverse through input items
while [ 1 ]
    do

    # Printing each item
    s=`echo $1 | cut -d ":" -f $field`

    # Condition to check if Input is processed, Quotes are 
    # essential for processing special characters in the input
    if [ ! "$s" ]
        then
        # if input processing is finished then exit else continue
        exit
    else

        # Print each item/word from input
        echo $s

        # Incrementing field after each word being processed.
        field=`expr $field + 1`
    fi
done

Output:
 

Output

Figure 1: Sample Output case-wise for Colon-based Separation on given input items.

Explanation:

CASE 1:  ./Colon.sh Geek:For:Geeks:Good
Geek
For
Geeks
Good

The above input is a simple case of plain simple words (only characters A-Z and a-z are present), so there are no special characters or space included in the input, hence even if quotes are not used surrounding the input items that is okay as the script will consider the entire list of items as 1 command-line argument and process it as expected. So Geek:For:Geeks:Good is simply processed to separate the words (practical example could be first names) and printed as output (As seen in the screenshot Figure 1).
 

CASE 2:  ./Colon.sh "abc@gmail.com:xyz@gmail.com:Exclamation!"
abc@gmail.com
xyz@gmail.com
Exclamation!

In the above case, special characters like “@, ‘ “ etc. are considered in the input. In order to successfully make the script understand the input, cover the input items surrounded by double quotes (“). Also in the input finish check in the script (if [ ! “$s” ]) we surround check with quotes for proper parsing. Hence, for the input “abc@gmail.com:xyz@gmail.com:Exclamation!” the correctly separated input items are obtained using delimiter as colon(:) as seen in the screenshot Figure 1.
 

CASE 3:  ./Colon.sh "Geeks for Geeks is good: Open Source Community: Great Contributions"
Geeks for Geeks is good
Open Source Community
Great Contributions

In the above case, space is a valid character for a given input item. Now the problem here Bash assumes that the command line arguments are separated by space so in this case of input items it will assume that “Geeks is 1st Command Line Argument, For is 2nd, Geeks is 3rd and so on. In order to avoid this problem, surround input with double quotes (“). So now the quoted input becomes only 1 Command Line Argument and processed as we desired, the input which is “Geeks for Geeks is good: Open Source Community: Great Contributions” which can be assumed as full names of persons are separated using Colon (:) as a delimiter as seen in the screenshot Figure 1.
 

CASE 4: ./Colon.sh 'Geeks "ForGeeks":Amazingly "Awesome":Detailed "Examples"'
Geeks "ForGeeks"
Amazingly "Awesome"
Detailed "Examples"

In the above case now let’s say double quotes (“) are considered as a valid character for input items themselves, in order to process change the surround of input items with single quotes (‘ ‘) for successful parsing. So the above input ‘Geeks “ForGeeks”:Amazingly “Awesome”:Detailed “Examples”’ which can be thought of as an example of sentences with adjectives in quotes, given in as input and will be processed as desired to give delimiter Colon (:) based separated output as seen in Figure 1. 

Thus, this simple shell script separates given input items using Colon (:) as a Delimiter has a wide range of applications and can be slightly modified to change whatever desired Delimiter like Comma (,), Semi-Colon (;) etc.


Last Updated : 08 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads