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 t
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 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. re
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 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
How to Remove Pattern with Special Character in String in R? Working with strings in R often involves cleaning or manipulating text data to achieve a specific format. One common task is removing patterns that include special characters. R provides several tools and functions to handle this efficiently. This article will guide you through different methods to
3 min read
How to replace multiple characters in a string in PHP ? A string is a sequence of characters enclosed within single or double quotes. A string can also be looped through and modifications can be made to replace a particular sequence of characters in it. In this article, we will see how to replace multiple characters in a string in PHP.Using the str_repla
3 min read
How to Remove Non-ASCII Characters from Data Files in R? Removing non-ASCII characters from data files is a common task in data preprocessing, especially when dealing with text data that needs to be cleaned before analysis. Non-ASCII characters are those that fall outside the 128-character ASCII set. These include characters from other languages, special
3 min read