5

I imported a shapefile that contains track plots of SGY files into QGIS, and I would like to remove '.sgy' from the file names in the attribute table.

In the image, I've highlighted the section of the filename that I would like to delete in bulk

example

1

4 Answers 4

10

Open "Field Calculator", check "Update existing field", select "NAME" field and use the following expression.

substr("NAME", 0, -4)

enter image description here

8

Alternatively, one can use the following expression:

regexp_substr("name",'([^.]+)')

it includes the regexp_substr() function.

7

Another alternative using the file_suffix() function. This expression concatenates a . with the file suffix and replaces it with an empty string.

replace("NAME", '.' || file_suffix("NAME"), '')

QGIS 3.24.0

1
  • very clever and neat!
    – Taras
    Apr 16, 2022 at 12:24
1

Inspired by @Matt solution, there is also a special function called base_file_name():

base_file_name("name")

However, for a filename with multiple . (such as my_compressed_file.tar.gz) this only removes the last file extension (.gz). Whereas file_suffix() finds tar.gz.

2
  • 1
    Looks like I made my edit to include base_file_name at the same time you posted this answer! Would you like to poach my sentence about multiple ., and I can remove it from my answer?
    – Matt
    Apr 16, 2022 at 16:51
  • If you don’t mind I can, I just do not know what is the most honest way in this case :)
    – Taras
    Apr 16, 2022 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.