Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all text from last dot in bash

Tags:

linux

grep

bash

awk

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?

like image 356
user3845185 Avatar asked Oct 29 '14 14:10

user3845185


People also ask

How can I remove all text after a character in Bash?

In Bash (and ksh, zsh, dash, etc.), you can use parameter expansion with % which will remove characters from the end of the string or # which will remove characters from the beginning of the string. If you use a single one of those characters, the smallest matching string will be removed.

How do you trim the last character in Bash?

To remove the last n characters of a string, we can use the parameter expansion syntax ${str::-n} in the Bash shell. -n is the number of characters we need to remove from the end of a string.

How do I remove the last 4 characters in Linux?

In this method, you have to use the rev command. The rev command is used to reverse the line of string characterwise. Here, the rev command will reverse the string, and then the -c option will remove the first character. After this, the rev command will reverse the string again and you will get your output.

What does %% mean in Bash?

So as far as I can tell, %% doesn't have any special meaning in a bash function name. It would be just like using XX instead. This is despite the definition of a name in the manpage: name A word consisting only of alphanumeric characters and under- scores, and beginning with an alphabetic character or an under- score.


1 Answers

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
like image 179
fedorqui 'SO stop harming' Avatar answered Oct 01 '22 16:10

fedorqui 'SO stop harming'