Remove all text from last dot in bash

16,681

With awk:

awk 'BEGIN{FS=OFS="."} NF--' file

In case there are no empty lines, this works. It sets input and output field separators to the dot .. Then, decreases the number of fields in one, so that the last one is kept out. Then it performs the default awk action: {print $0}, that is, print the line.

With sed:

sed 's/\.[^.]*$//' file

This catches the last block of . + text + end of line and replaces it with nothing. That is, it removes it.

With rev and cut:

rev file | cut -d'.' -f2- | rev

rev reverses the line, so that cut can print from the 2nd word to the end. Then, rev back to get the correct output.

With bash:

while ISF= read -r line
do
  echo "${line%.*}"
done < file

This perform a string operation consisting in replacing the shortest match of .* from the end of the variable $line content.

With grep:

grep -Po '.*(?=\.)' file

Look-ahead to print just what is before the last dot.

All of them return:

abc.cde.ccd.eed.12345.5678
abcd.cdde.ccdd.eaed.12346.5688
aabc.cade.cacd.eaed.13345.5078
abzc.cdae.ccda.eaed.29345.1678
abac.cdae.cacd.eead.18145.2678
aabc.cdve.cncd.ened.19945.2345
Share:
16,681
user3845185
Author by

user3845185

Updated on June 23, 2022

Comments

  • user3845185
    user3845185 almost 2 years

    I have a file named test.txt which has:

    abc.cde.ccd.eed.12345.5678.txt
    abcd.cdde.ccdd.eaed.12346.5688.txt
    aabc.cade.cacd.eaed.13345.5078.txt
    abzc.cdae.ccda.eaed.29345.1678.txt
    abac.cdae.cacd.eead.18145.2678.txt
    aabc.cdve.cncd.ened.19945.2345.txt
    

    If I want to remove everything beyond the first . like:

    cde.ccd.eed.12345.5678.txt
    cdde.ccdd.eaed.12346.5688.txt
    cade.cacd.eaed.13345.5078.txt
    cdae.ccda.eaed.29345.1678.txt
    cdae.cacd.eead.18145.2678.txt
    cdve.cncd.ened.19945.2345.txt
    

    Then I will do

    for i in `cat test.txt`; do echo ${i#*.}; done
    

    but If I want to remove everything after the last . like:

    abc.cde.ccd.eed.12345.5678
    abcd.cdde.ccdd.eaed.12346.5688
    aabc.cade.cacd.eaed.13345.5078
    abzc.cdae.ccda.eaed.29345.1678
    abac.cdae.cacd.eead.18145.2678
    aabc.cdve.cncd.ened.19945.2345
    

    what should I do?