Table of Contents
1. Overview
In this article, we will explore different ways to remove characters in String in different scenarios such as removing specific characters, removing first character, removing last character, and removing first and last characters.
2. Using Parameter Expansion
Bash’s built-in parameter expansion is best suited for performing string manipulation operations. Let’s explore parameter expansion for various scenarios.
2.1 Removing Specific Character from a String
1 2 3 4 5 6 7 |
#!/bin/bash org_string="hello world" new_string="${org_string//o/}" echo "This is Original string: $org_string" echo "This is New string: $new_string" |
1 2 3 4 |
This is Original string: hello world This is New string: hell wrld |
In the above example, the ${org_string//o/}
syntax is used to replace all occurrences of character o
in the string with nothing. Here is a breakdown of each part of the expression:
${org_string}
: This refers to the value of the Bash variableorg_string
.//
: This is the pattern substitution operator. It tells Bash to replace all instances of a given pattern with a new string.o
: This is the matched pattern, that is charactero
""
: This is the replacement string. In this case, it is simply an empty string, which means any instance of ao
character found in thestring
will be removed.
2.2 Removing the First Character from the String
1 2 3 4 5 6 7 |
#!/bin/bash org_string="hello world" new_string="${org_string:1}" echo "This is Original string: $org_string" echo "This is New string: $new_string" |
1 2 3 4 |
This is Original string: hello world This is New string: ello world |
${org_string:1}
is example of parameter expansion in bash, used to extract String excluding the first character.
In this specific case:
:1
: This indicates that substring should start at index 1 for org_string
.
Therefore, ${org_string%?} removes the first character of org_string
, whatever that character may be.
2.3 Removing the Last Character of a String
1 2 3 4 5 6 7 |
#!/bin/bash org_string="hello world" new_string="${org_string%?}" echo "This is Original string: $org_string" echo "This is New string: $new_string" |
1 2 3 4 |
This is Original string: hello world This is New string: hello worl |
${org_string%?}
is used to remove last character from the String.
In this case:
%? indicates that we want to remove a pattern from the end of org_string.
? is a wildcard character that matches any single character.
Therefore, ${org_string%?} removes the last character of org_string
, whatever that character may be.
2.4 Remove the First and Last Characters of a String
1 2 3 4 5 6 7 8 |
#!/bin/bash org_string="hello world" new_string="${org_string:1}" new_string="${new_string%?}" echo "This is Original string: $org_string" echo "This is New string: $new_string" |
1 2 3 4 |
This is Original string: hello world This is New string: ello worl |
Here, we have used combination of removing first character and last character of the String.
3. Using sed
Command:
The sed (Stream Editor) is a powerful and versatile text processing tool that performs text transformations on an input stream (a file or input from a pipeline).
Let’s use sed
commands for all different scenarios:
3.1 Removing Specific Character from the String
1 2 3 4 5 6 7 |
#!/bin/bash org_string="hello world" new_string=$(echo "$org_string"| sed 's/o//g') echo "This is Original string: $org_string" echo "This is New string: $new_string" |
1 2 3 4 |
This is Original string: hello world This is New string: hell wrld |
Above, the sed
command removes all the occurrences of the character o
from the "hello world"
string. In the above example, sed
is used to search for the character o
in a string and replace it with nothing. Of course, we can also replace 'o'
with any other character we want to remove from the string.
Here, s/o//g
is a regular expression pattern used in the sed
command to remove all occurrences of the character o
from String. This is how it works:
s
: Indicates that we want to perform a substitution operation./
: This separates the different parts of the substitution command.o
: This is the character we want to remove from the string.//
: We want to replace the matched pattern with nothing, effectively removing it.g
: This stands forglobal
and means we want to perform the substitution globally throughout the string, i.e. removing all occurrences of the charactero
instead of just the first one.
3.2 Removing the First Character of a String
1 2 3 4 5 6 7 |
#!/bin/bash org_string="hello world" new_string=$(echo "$org_string" | sed 's/^.//') echo "This is Original string: $org_string" echo "This is New string: $new_string" |
1 2 3 4 |
This is Original string: hello world This is New string: ello world |
In this example, the sed
command removed the first character of the "hello world"
string using the regular expression 's/^.//')
.
Let’s break it down to understand:
s
: Indicates that we want to perform a substitution operation.^
: This special character matches the beginning of the line..
: Matches any single character.//
: Indicates that we want to replace the matched pattern with nothing.
So, the s/^.//
in sed
is used to search for the string’s first character (which is matched by ^.
) and replace it with nothing, effectively removing it from the string.
3.3 Removing the Last Character of a String
Use the sed
command to remove last character from String in Bash.
1 2 3 |
echo "Hello World" | sed 's/.$//' |
1 2 3 |
Hello Worl |
In this example, the sed
command removed last character of the string using the pattern 's/.$//'
. Here, .$
matches the line’s last character (.
, followed by $
to indicate the end of the line).
3.4 Removing the First and Last Characters of a String
Use the sed
command to remove the first and last characters from String in Bash.
1 2 3 |
echo "Hello World" | sed 's/^.//' | sed 's/.$//' |
1 2 3 |
ello Worl |
The two sed
commands are chained together with a |
symbol, which means that the output of the first command is used as an input for the second command. So in this example, the string’s first character is removed by sed 's/^.//'
. After that, sed 's/.$//'
removes last character.
4. Using awk
command
The awk is a powerful scripting language for text processing and is typically used for data extraction.
Let’s use awk command in different scenarios:
4.1 Removing Specific Character from the String
1 2 3 4 5 6 7 |
#!/bin/bash org_string="hello world" new_string=$(echo "$org_string" | awk '{gsub(/o/,"")}1') echo "This is Original string: $org_string" echo "This is New string: $new_string" |
1 2 3 4 |
This is Original string: hello world This is New string: hell wrld |
We can observe the awk
command used the gsub()
function to remove all occurrences of the character o
from the input string. The gsub()
is a built-in function used for global substitution. It is used to search and replace all occurrences of a given regular expression in a string.
The syntax for using the gsub()
function in awk
is gsub(regexp, replacement, target)
. There are three parameters in gsub
that are briefly explained below:
regexp
is used to match regular expression patternsreplacement
is the string that will replace matched patternstarget
is the string containing the text to search for and replace.
In the above example, the gsub(/o/,"")
function replaces all matches of the regular expression /o/
in the input line and replaces it with the empty string. The 1
is a shorthand for print the current line
, ensuring the modified string is printed.
4.2 Removing the First Character of a String
1 2 3 4 5 6 7 |
#!/bin/bash org_string="hello world" new_string=$(echo "$org_string" | awk '{print substr($0, 2)}') echo "This is Original string: $org_string" echo "This is New string: $new_string" |
1 2 3 4 |
This is Original string: hello world This is New string: ello world |
The awk
command used the substr()
function to remove the first character of a string "hello world"
.
In the above example, the substr()
function takes two arguments, $0
represents the input string and the 2
denotes the starting position, which means starting from the second character (i.e., skipping the first character).
We can also replace 2
with any number to remove the character from the beginning of the string to that position. For example, substr($0, 4)
will remove the first three characters from the org_string
.
4.3 Removing the Last Character of a String
1 2 3 4 5 |
#!/bin/bash new_string=$(echo "Hello, world" | awk '{print substr($0, 1, length($0)-1)}') echo "Removed First and Last Character: $new_string" |
1 2 3 |
Removed Last Character: Hello, worl |
We can observe last character of "Hello, world"
string is removed using the substr()
function in awk
. Here, the substring is extracted from the input string starting from position 1
by skipping last character of the input string.
The length of the substring is determined using the length()
function, which calculates the length of the string and subtracts 1
to exclude last character.
4.4 Removing the First and Last Characters of a String
Use the awk
command to remove the first and last character from String in Bash.
1 2 3 4 5 |
#!/bin/bash new_string=$(echo "Hello, world" | awk '{print substr($0, 2, length($0)-2)}') echo "Removed First and Last Character: $new_string" |
1 2 3 |
Removed First and Last Character: ello, worl |
In this example, the first and last characters of the string are removed. The substr()
function is used to extract the substring from input string $0
starting from character 2
until the second last character by skipping the first and last character of the string. Here, length($0)-2
is used to calculate the length of the input string $0
, subtracting 2
to exclude the first and last characters.
Further reading:
5. Using Cut
Command
The cut command is typically used to cut sections from each line of files.
5.1 Remove Specific Character from a String
cut isn’t ideal for removing specific characters; it’s better suited for removing by field or position.
5.2 Removing the First Character of a String
We can use cut -c 2-
command to remove first character of a String.
1 2 3 4 5 6 7 |
#!/bin/bash org_string="hello world" new_string=$(echo "$org_string" | cut -c 2-) echo "This is Original string: $org_string" echo "This is New string: $new_string" |
1 2 3 4 |
This is Original string: hello world This is New string: ello world |
In this example, the cut
removed the first character of the "hello world"
string. Here, the -c 2-
means extracting the string from the second position until the end, hence, removing the first character.
5.2 Removing the Last Character of a String
The idea is to reverse the string first, remove the first character and reverse string again. This will result in removing last character from a String.
1 2 3 4 5 |
#!/bin/bash new_string=$(echo "hello world" | rev | cut -c2- | rev) echo "This is New string: $new_string" |
1 2 3 |
This is New string: hello worl |
In this example, the rev
command is first used to reverse the "hello world"
string. Then, the cut
removes the string’s first character, which is last character, and after that string is reversed again.
5.3 Removing the First and Last Characters of a String
This is combination of previous two sections.
1 2 3 4 5 |
#!/bin/bash new_string=$(echo "hello world"| cut -c 2- | rev | cut -c 2- | rev) echo "This is New string: $new_string" |
1 2 3 |
This is New string: ello worl |
Here, the cut
command first removed the string’s first character using the -c
parameter. Then, the rev
command reversed the string, now cut
again removed the first character of the revered string, which is initially last character. After that string is reversed back using the rev
command.
6. Using tr
Command
The tr
command is generally used for translating or deleting characters from the String. It is simple and efficient way to remove characters from String.
6.1 Remove Specific Character from String
1 2 3 4 5 6 7 |
#!/bin/bash org_string="Hello, wohhrld!" new_string=$(echo "$org_string" | tr -d 'h') echo "This is Original string: $org_string" echo "This is New string: $new_string" |
1 2 3 4 |
This is Original string: hello, wohhrld! This is New string: Hello, world! |
In this example, the tr
command is used with the -d
option to delete all occurrences of character 'h'
from the input string "hello, wohhrld!"
. The tr
command is case-sensitive, as it only removes the lowercase letter h
; if we want to remove both lower and uppercase characters, consider the below example.
1 2 3 4 5 6 7 |
#!/bin/bash org_string="Hello, wohhrld!" new_string=$(echo "$org_string" | tr -d 'Hh') echo "This is Original string: $org_string" echo "This is New string: $new_string" |
1 2 3 4 |
This is Original string: Hello, wohhrld! This is New string: ello, world! |
Here, the tr
command used the -d
parameter to delete all the occurrences, either it is in lowercase or uppercase.
6.1 Removing the First Character of a String
The tr
command with the -d
option can be used to remove the first character of the input string "Hello, World!"
, which is obtained using parameter expansion ${string:0:1}
.
1 2 3 4 5 6 7 |
#!/bin/bash org_string="Hello, World!" new_string=$(echo "$org_string" | tr -d "${org_string:0:1}") echo "This is Original string: $org_string" echo "This is New string: $new_string" |
1 2 3 4 |
This is Original string: Hello, World! This is New string: ello, World! |
6.2 Removing the Last Character of a String
Use the tr
command to remove last character from String in Bash.
1 2 3 4 5 6 7 |
#!/bin/bash org_string="Hello, World" new_string=$(echo "$org_string" | tr -d "${org_string:11:-1}") echo "This is Original string: $org_string" echo "This is New string: $new_string" |
1 2 3 4 |
This is Original string: Hello, World This is New string: Hello, Worl |
In this example, the tr
command deleted the character from the input string starting from position 11
till the end, which is last character of the "Hello, World"
string.
6.3 Removing the Range of Characters
The tr
command is useful to remove range of characters from String. We can pass range to -d
option and it will remove range of characters from String.
1 2 3 4 5 6 7 |
#!/bin/bash org_string="hello, World" new_string=$(echo "$org_string" | tr -d 'e-l') echo "This is Original string: $org_string" echo "This is New string: $new_string" |
1 2 3 4 |
This is Original string: hello, World This is New string: o, Word |
In this example, the tr
command deleted the character from the input string starting that is in the range of e-l
(e
, f
, g
, h
, i
, j
, k
, l
) in the input string "hello, World"
.
7. Conclusion
In this article, we have discussed different ways to remove characters from String in Bash.
Parameter Expansion generally Offers the best performance for simple tasks as it doesn’t spawn a subshell or invoke an external command. tr
command is extremely efficient for set based operation.
sed and awk can be used for complex tasks but slightly slower due to their text-processing nature. cut
can be fast for positional removals but limited in functionality.