Search code examples
bashshellsplitifs

Split a namefile in shell


I'm here to know how to split a file name that was found by a ls comand :D. Let me explain better...

I have a variable

images=$( ls | grep .img )

And then i want to split the result of the search, because i just want the name before the .img, so a nice idea is use IFS.

IFS=. read -r disk <<< $image

Pretty nice, but when a do an echo with the $disk variable, what i see is a ".img" just that, i want to recover where is before that dot.

Thank you all, and sorry for any mistake :)


Solution

  • Use the stream editor sed! Example:

    echo "myImage.jpg" | sed 's/.jpg//'
    

    That s means "substitute", and you substitute the part between the first two slashes for the part between the second and third slash, so in the example, ".jpg" for the empty string.

    That's all!