Remove all text from last dot in bash

本小妞迷上赌 提交于 2021-02-04 15:30:41

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/26633623/remove-all-text-from-last-dot-in-bash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!