foo,
bar,
demo,
I need output as follows:
foo
bar
demo.
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:
IP="192.168.2.25," 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 ',' ## IP="192.168.2.200,$IP" IP="192.168.5.253,$IP" ## 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:
#!/bin/bash ## define our VPN auth IPs for sysadmins and backup agents ## _admin_ips="202.5.1.1|MUM_VPN 1.2.3.4|DEL_VPN 1.1.1.4|NYC_VPN 203.1.2.3|CGP_BACKUP_AGENT 3.1.2.3|AWS_BACKUP_AGENT" Β _ip="" Β ## Turn on firewall on each Debian/Ubuntu box ## yes | ufw enable Β ## Allow ssh login to $_admin_ips only ## for e in $_admin_ips do ufw allow from "${e%%|*}" to any port 22 proto tcp comment "Open TCP SSH PORT for ${e##*|}" _ip="${e%%|*},${_root_ip}" ## Build IP list ## done Β ## 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 ## _service_url="https://1.254.254.254/${project_id}/${service_provider}" task_add_user "admin" "${_service_url}/encrypted_passwd.txt" "${_service_url}/id_ed25519.pub" task_apply_security_settings "${_service_url}/security.sh" task_apply_patches "${os}" "${_service_url}/${os}/patches.sh" Β ## Rest of config will be done by sysadmin or Ansbile or our Docker scripts## ## Reboot the box ## reboot
Conclusion
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
π§ 2 comments so far... add one β
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall β’ CentOS 8 β’ OpenSUSE β’ RHEL 8 β’ Ubuntu 16.04 β’ Ubuntu 18.04 β’ Ubuntu 20.04 |
Network Utilities | dig β’ host β’ ip β’ nmap |
OpenVPN | CentOS 7 β’ CentOS 8 β’ Debian 10 β’ Debian 8/9 β’ Ubuntu 18.04 β’ Ubuntu 20.04 |
Package Manager | apk β’ apt |
Processes Management | bg β’ chroot β’ cron β’ disown β’ fg β’ jobs β’ killall β’ kill β’ pidof β’ pstree β’ pwdx β’ time |
Searching | grep β’ whereis β’ which |
User Information | groups β’ id β’ lastcomm β’ last β’ lid/libuser-lid β’ logname β’ members β’ users β’ whoami β’ who β’ w |
WireGuard VPN | Alpine β’ CentOS 8 β’ Debian 10 β’ Firewall β’ Ubuntu 20.04 |
thanks β¦ good tutorial
Thank you, you are doing a great job, Keep it up.