Remove Last character in String – Unix / Linux

  • Updated
  • Posted in Programming
  • 1 min read


Q) I’ve a file with bunch of traces. I need to take away the final character in every line from that file. How am i able to obtain this in unix or linux surroundings.

Solution:

1. SED command to take away final character

You can use the sed command to delete the final character from a textual content. The sed command is

sed s/.$// filename

2. Bash script

The beneath bash script can be utilized to take away the final character in a file.

#! /bin/bash
whereas learn LINE
do
echo ${LINE%?}
achieved < filename

3. Using Awk command We can use the built-in features size and substr of awk command to delete the final character in a textual content.

awk '{$0=substr($0,1,size($0)-1); print $0}' filename

4. Using rev and minimize command We can use the mixture of reverse and minimize command to take away the final character. The command is proven beneath:

rev merchandise.txt | minimize -c2- |rev

Leave a Reply