How to use Sed command

Hi UNIX community builders,

I have a task to do using Sed command, which is the following:

I am doing ls to file names >> test.txt ( to print directories name to a txt file )
I want to remove the extension of the files from the txt file.

So I want to use sed to perform such thing
Like if I have in the txt file the following file name : standalone_app.png
I want it to be : standalone_app

So please how to do that using sed ?

@fnasser,
I see you had a number of recent threads with the sed-based solutions you accepted as "Solved".
It should be relatively easy to come up with at least a rough skeleton.
Have you tried anything on your own?

@vgresh99 I couldnt find a way to remove the file extension from the txt file

Could you at least share what you've tried and where it exactly failed?

@vgresh99 Actually I found the solution,

echo "standalone_app.png" | sed 's/[.].*$//'

Thank you.

3 Likes

Perhaps sufficient.
A special case is

echo "standalone.app.png" | sed 's/[.].*$//'

where the greedy .* (anything any times) expands from the $ end till the leftmost dot.
The following expands from the end to the rightmost dot:

echo "standalone.app.png" | sed 's/[.][^.]*$//'

The [^.]* is anything but a dot any times.

2 Likes