Pregunta

I have file names in the following format:

My.File.d01.h01.txt
My.New.File.d01.h02.txt
My.Another.File.d01.h03.txt
My.Yet.Another.File.d01.h04.txt
File.d01.h05.txt

I want to remove the dot between 'd01.h01', 'd01.h02', 'd01.h03', etc.

I imagine I need to use some form of sed, but I need to retain the dXX and hYY values.

Desired output:

My.File.d01h01.txt
My.New.File.d01h02.txt
My.Another.File.d01h03.txt
My.Yet.Another.File.d01h04.txt
File.d01h05.txt

Any help?

¿Fue útil?

Solución

Make use of backreferences:

sed -r 's/(\bd[0-9]+)\.(h[0-9]+)/\1\2/' inputfile

For your sample input, it'd yield:

My.File.d01h01.txt
My.New.File.d01h02.txt
My.Another.File.d01h03.txt
My.Yet.Another.File.d01h04.txt
File.d01h05.txt

Either check for a word boundary before d or ensure that it's preceded by a .:

sed -r 's/(\.d[0-9]+)\.(h[0-9]+)/\1\2/' inputfile

Otros consejos

Here is an awk that may help:

awk -F. '{for (i=1;i<NF;i++) {if ($i~/d[0-9][0-9]/) f=1;printf "%s"(f?"":FS),$i}print FS $NF;f=0}' file
My.File.d01h01.txt
My.New.File.d01h02.txt
My.Another.File.d01h03.txt
My.Yet.Another.File.d01h04.txt
File.d01h05.txt
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top