How to sed remove last character from each line

I need to remove the last character from a line and shell variable such as “${_allowed_ips}“. For example, each line is as follows in my file:

I need output as follows:
How can I use sed to delete the last character?

The sed is a free and open-source stream editor for Linux, macOS, *BSD, and Unix-like systems. It is perfect for removing/deleting the last character and perform other options on your files or shell variables.

sed remove last character from each line

With sed, everything is rather easy when it comes to text manipulation. The syntax is as follows for find and replace text with sed:

sed 's/search/replace/' input > output
sed -i'.BAK' 's/search/replace/' input
sed 's/search/replace/' <<<"${var}"

For example, remove the last digit (say 6) form an input line as follows:
echo "this is a test6" | sed 's/.$//'
The “.” (dot) indicates any character in sed, and the “$” indicates the end of the line. In other words “.$” means, delete the last character only. Next, we will create a file as follows using the cat command:
cat > demo.txt
Append the text as follows:

this is a test,
I love Unix,
I like Linux too,

To remove the last character “,” from demo.txt for each line use the sed command:
sed 's/.$//' demo.txt > output.txt
## Linux GNU/sed version syntax ##
sed -i 's/.$//' demo.txt

Verify it:
cat demo.txt
cat output.txt

Removing the last character from each line using sed on my Linux desktop

A note about the conditional removal of the last character

By default, sed command so far used would remove any last character. But, say you can restrict removal to digits only as follows:
echo "I love Pizza6" | sed 's/[0-9]$//'
echo "The best blog in Unixverse3" | sed 's/[[:digit:]]$//'
echo "I love Pizza6Times" | sed 's/[0-9]$//'

The “[0-9]“/”[[:digit:]]” indicates all digits such as 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. Similarly, we can use the following to delete all alphabets:
echo "I love donuts two times a dayz" | sed 's/[[:alpha:]]$//'
echo "I love donuts 2 times a dayZ" | sed 's/[[:upper:]]$//'

Sed bracket expressions

Character Classes Description
[[:alnum:]] Alphanumeric characters: ‘[[:alpha:]]’ and ‘[[:digit:]]’; in the ‘C’ locale and ASCII character encoding, this is the same as ‘[0-9A-Za-z]’.
[[:alpha:]] Alphabetic characters: ‘[[:lower:]]’ and ‘[[:upper:]]’; in the ‘C’ locale and ASCII character encoding, this is the same as ‘[A-Za-z]’.
[[:blank:]] Blank characters: space and tab.
[[:cntrl:]] Control characters. In ASCII, these characters have octal codes 000 through 037, and 177 (DEL). In other character sets, these are the equivalent characters, if any.
[[:digit:]] Digits: 0 1 2 3 4 5 6 7 8 9.
[[:graph:]] Graphical characters: ‘[[:alnum:]]’ and ‘[[:punct:]]’.
[[:lower:]] Lower-case letters; in the ‘C’ locale and ASCII character encoding, this is a b c d e f g h i j k l m n o p q r s t u v w x y z.
[[:print:]] Printable characters: ‘[[:alnum:]]’, ‘[[:punct:]]’, and space.
[[:punct:]] Punctuation characters; in the ‘C’ locale and ASCII character encoding, this is ! ” # $ % & ‘ ( ) * + , – . / : ; ? @ [ \ ] ^ _ ` {
[[:space:]] Space characters: in the ‘C’ locale, this is tab, newline, vertical tab, form feed, carriage return, and space.
[[:upper:]] Upper-case letters: in the ‘C’ locale and ASCII character encoding, this is A B C D E F G H I J K L M N O P Q R S T U V W X Y Z.
[[:xdigit:]] Hexadecimal digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f.

sed delete last character from shell variable

Let us define a shell variable named IP as follows:

printf "%s\n" $IP

Let us say you want to remove the last character named ‘,‘, run:

## sed remove last character from $IP ##
sed 's/,$//' <<<"${IP}"
## OR ## 
echo $IP | sed 's/.$//' 
## Append more IPs separated by ',' ##
## Update IP variable by deleting last ',' ##
IP=$(sed 's/,$//' <<<"${IP}")
echo "${IP}"

How to shave off last character using sed

Here is another small shell script snippet run when a new AWS EC2/Google GCP/Linode (StackScripts) cloud instance launched by us:

## define our VPN auth IPs for sysadmins and backup agents ##
## Turn on firewall on each Debian/Ubuntu box ##
yes | ufw enable 
## Allow ssh login to $_admin_ips only ##
for e in $_admin_ips
 ufw allow from "${e%%|*}" to any port 22 proto tcp comment "Open TCP SSH PORT for ${e##*|}"
 _ip="${e%%|*},${_root_ip}"  ## Build IP list ## 
## sed delete last character from the list to avoid sshd errors  ##
_ip="$(sed 's/.$//'<<<${_root_ip})"
## Okay, update our sshd config ##
## Disable root login (if enabled by default) for all and only allow ssh key based login ##
sed -i'.BAK' -e 's/PermitRootLogin yes/PermitRootLogin no/g' -e 's/#PasswordAuthentication yes/PasswordAuthentication no/g'  /etc/ssh/sshd_config
## Allow root users and backup agents to login as root from selected IPs with ssh keys only ##
echo "Match Address ${_root_ip}" >>/etc/ssh/sshd_config 
echo -e "\tPermitRootLogin yes" >>/etc/ssh/sshd_config
## Run other init tasks ##
task_add_user "admin" "${_service_url}/encrypted_passwd.txt" "${_service_url}/"
task_apply_security_settings "${_service_url}/"
task_apply_patches "${os}" "${_service_url}/${os}/"
## Rest of config will be done by sysadmin or Ansbile or our Docker scripts##
## Reboot the box ##


You learned how to remove the last character from text file and each line, including shell variables on Linux and Unix-like systems. Of course, we can use the awk command, cut command, Perl/Python and so on. However, I prefer to use sed as it is installed by default on all systems, and it is made for this kind of job. See sed man page by typing the following man command or read it online here.
man sed

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 2 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
2 comments… add one
  • ivo Nov 17, 2020 @ 9:05

    thanks … good tutorial

  • Pawan Feb 17, 2021 @ 13:49

    Thank you, you are doing a great job, Keep it up.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum