Remove Last character from String in Linux
Last Updated :
29 Jun, 2021
In this article, we will discuss how to remove the last character from the string in Linux.
In Linux, there are various commands and techniques present by which you can do this task in an easier way if you have some basic knowledge about the commands in Linux. Here, you will see the different commands by which you can remove the last character of a string in Linux.
Method 1: Using the cut command
The cut command is used to extract some specific section from the string or from a file and prints the result to standard output. You can also use this command for removing the characters from a string. There are two ways to remove the last character from the string using the cut command. These are as follows:
- When you know the length of a string
Syntax:
cut <character> <range>
Example:
$ echo "linux" | cut -c 1-4
This method works when you know the length of the given string. It first takes the string and pipes it with the cut command. You have to provide a range from the start of the string to the 2nd last character of the string. It extracts the characters from that range, leaving the last character, and prints the result to standard output.
Output:

Another way is to use the complement option of the cut command.
Syntax:
cut --complement <complement pattern>
Example:
$ echo "linux" | cut --complement -c 5
In this method, you have to use the complement option present in the cut command. The complement command can work with bytes, characters, and fields. Here we are using character -c. So here we have provided (length of string)-1 after the -c command, which will cut the last character and print the result, leaving the last character of the string.
Output:

- When you don’t know the length of the string
Syntax:
$ echo "linux" | rev | cut -c2- | rev
In this method, you have to use the rev command. The rev command is used to reverse the line of string characterwise. Here, the rev command will reverse the string, and then the -c option will remove the first character. After this, the rev command will reverse the string again and you will get your output.
Output:

Method 2: Using the sed command
The sed command stands for stream editor. It is a powerful tool that is used for manipulating and editing streams of text. It also supports regular expressions which can be used for pattern matching. You can also use the sed command to remove the characters from the strings.
$ echo "linux" | sed 's/.$//'
In this method, the string is piped with the sed command and the regular expression is used to remove the last character where the (.) will match the single character and the $ matches any character present at the end of the string.
Output:

Method 3: Using awk
The awk is a scripting language that is used for pattern matching and text processing. Mostly it is used as a reporting and analyzing tool. So now you will see how we can use awk to remove the last character from the given string.
$ echo "linux" | awk '{ print substr( $0, 1, length($0)-1 ) }'
This method works as our given string pipes with the awk and then in awk, the string is processed. Here the length($0)-1 means removing the last character by deducting ‘1’ from the string’s total length. Through this process, the command will print the string from the 1st character up to the 2nd character.
Output:

Similar Reads
Remove a Character From String in JavaScript
In JavaScript, a string is a group of characters. Strings are commonly used to store and manipulate text data in JavaScript programs, and removing certain characters is often needed for tasks like: Removing unwanted symbols or spaces.Keeping only the necessary characters.Formatting the text.Methods
3 min read
How to Remove Last Character from String in Ruby?
Removing the last character from a string in Ruby is a common task in various programming scenarios. Whether you need to manipulate user input, process file paths, or clean up data, there are several approaches to achieve this. This article focuses on discussing how to remove the last character from
2 min read
Removing trailing newline character from fgets() Input
fgets() reads a line from the specified stream and stores it into the string pointed by str. It stops when either (n - 1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first. However, fgets() also reads the trailing newline character and ends up r
3 min read
How to remove First and Last character from String in Scala?
In this article, we will explore different approaches to removing the first and last character from a string in Scala. Table of Content Using substring methodUsing drop and dropRight methodsUsing substring methodIn this approach, we are using the substring method which takes two arguments the starti
1 min read
How to remove the first character of string in PHP?
Remove the very first character of a given string in PHP Examples: Input : GeeksforgeeksOutput : eeksforgeeksInput :, Hello geek!Output : Hello geek!Explanation: In PHP to remove characters from the beginning we can use ltrim but in that, we have to define what we want to remove from a string i.e. r
3 min read
Program for removing i-th character from a string
Given a string S along with an integer i. Then your task is to remove ith character from S. Examples: Input: S = Hello World!, i = 7Output: Hello orld!Explanation: The Xth character is W and after removing it S becomes Hello orld! Input: S = GFG, i = 1Output: GGExplanation: It can be verified that a
5 min read
How to Remove the Last Character From a Table in SQL?
SQL (Structured Query Language) allows for efficient data manipulation and retrieval. A common task in SQL involves removing the last character from a specific column within a table. This can be achieved using string functions like SUBSTRING() and LEN(). In this article, we will demonstrate how to a
5 min read
How to Remove Special Characters from a String in Ruby?
In this article, we will discuss how to remove special characters from a string in Ruby. In Ruby, special characters can be removed from a string using various methods. Special characters typically include punctuation marks, symbols, and non-alphanumeric characters. Let us explore different methods
2 min read
Deletion of character in String
Given a string str and an integer position pos, the task is to delete the character at the specified position pos from the string str. Examples: Input: str = "GeeksforGeeks", pos = 5Output: GeeksorGeeks Input: str = "HelloWorld", pos = 0Output: elloWorld Deletion of character in String using Loop:Tr
4 min read
Replace Last Comma in Character with &-Sign in R
A string in R is a sequence of characters which contains numbers, characters, and special symbols. The characters can be modified, inserted as well as deleted in the strings. R provides a large variety of functions and external packages to carry out the string amendments. In this article, we are goi
4 min read