Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed - get only text without extension

Tags:

regex

sed

How do I remove the extension in this SED statement?

Through

sed 's/.* - //'

File content

2021-04-21_@fluffyban_6953588770591509765.mp4 - Filename.mp4

Actual

Filename.mp4

Desired

Filename
like image 784
Lacer Avatar asked Dec 08 '22 09:12

Lacer


1 Answers

With your shown samples only. This could be done with simple codes in awk,sed and perl as follows.

1st solution: Using sed, perform simple substitutions and you will get desired output.

sed 's/.*- //;s/\.mp4$//' Input_file

2nd solution: Using awk its more simpler, creating different field separator and just print appropriate 2nd last column.

awk -F'- |.mp4' '{print $(NF-1)}' Input_file

3rd solution: Using substitution method in awk to get the required value as per OP's requirement.

awk '{gsub(/.*- |\.mp4$/,"")} 1' Input_file

4th solution: With perl one liner we could grab the appropriate needed value by setting field separators as dash spaces and .mp4 as follows:

perl -a -F'-\s+|\.mp4'  -ne 'print "$F[$#F-1]\n";' Input_file
like image 79
RavinderSingh13 Avatar answered Jan 07 '23 19:01

RavinderSingh13