Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
How to remove First and Last character from String in Scala?
Next article icon

Remove Last character from String in Linux

Last Updated : 29 Jun, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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:



Next Article
How to remove First and Last character from String in Scala?
author
pushpender007
Improve
Article Tags :
  • Linux-Unix
  • Linux-text-processing-commands

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
  • How Can I Remove Non-Numeric Characters from Strings Using gsub in R?
    When working with data in R Programming Language, especially text data, there might be situations where you need to clean up strings by removing all non-numeric characters. This is particularly useful when dealing with numeric data that has been stored or formatted as text with extra characters (lik
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences