简体   繁体   中英

Remove all text from last dot in bash

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM