How to sed remove last character from each line

${_allowed_ips}

foo,

bar,

demo,

foo

bar

demo

ADVERTISEMENTS



sed remove last character from each line

I need to remove the last character from a line and shell variable such as ““. 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.

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} " 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



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

" $IP IP="192.168.2.25," printf "%s

" $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} " ## 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 " \t PermitRootLogin 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 #!/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