How delete characters of specific line with sed?

Hi,

I have a text file with some lines like this:

/MEDIA/DISK1/23568742.MOV
/MEDIA/DISK1/87456321.AVI
/MEDIA/DISK2/PART1/45753131.AVI
/IMPORT/44452.WAV
...

I want to remove the last 12 characters in each line that it ends "AVI". Should look like this:

/MEDIA/DISK1/23568742.MOV
/MEDIA/DISK1/
/MEDIA/DISK2/PART1/
/IMPORT/44452.WAV
...

Does anyone know how to do it with sed?

Thank you,

I�aki

Try:

sed 's/.\{9\}AVI$//' file

Try this sed command:

sed 's/[0-9]\{1,\}.AVI$//' filename

Sorry for not having answered before. Neither solution has worked. In fact, the file does not change anything.

Does anyone would think otherwise?

Thank you,

if you want to change in the file itself, then you need to use -i

before that make a copy of the file and apply sed with -i

sed -i 's/.\{9\}AVI$//' file

The solution given by Scrutinizer and spacebar indeed works! if you wanted the result in a file, just redirect the output to another file.

sed 's/.\{9\}AVI$//' file > output_file
sed 's/[0-9]\{1,\}.AVI$//' filename > output_file

I am sorry, but I don not known I am doing wrong:

[root@luke /tmp]$ cat test.txt
/MEDIA/DISK1/23568742.MOV
/MEDIA/DISK1/87456321.AVI
/MEDIA/DISK2/PART1/45753131.AVI
/IMPORT/44452.WAV
[root@luke /tmp]$ sed 's/.\{9\}AVI$//' test.txt
/MEDIA/DISK1/23568742.MOV
/MEDIA/DISK1/87456321.AVI
/MEDIA/DISK2/PART1/45753131.AVI
/IMPORT/44452.WAV
[root@luke /tmp]$ cat test.txt
/MEDIA/DISK1/23568742.MOV
/MEDIA/DISK1/87456321.AVI
/MEDIA/DISK2/PART1/45753131.AVI
/IMPORT/44452.WAV
[root@luke /tmp]$ sed 's/[0-9]\{1,\}.AVI$//' test.txt
/MEDIA/DISK1/23568742.MOV
/MEDIA/DISK1/87456321.AVI
/MEDIA/DISK2/PART1/45753131.AVI
/IMPORT/44452.WAV
[root@luke /tmp]$ cat test.txt
/MEDIA/DISK1/23568742.MOV
/MEDIA/DISK1/87456321.AVI
/MEDIA/DISK2/PART1/45753131.AVI
/IMPORT/44452.WAV
[root@luke /tmp]$ sed -i 's/.\{9\}AVI$//' test.txt
[root@luke /tmp]$ cat test.txt
/MEDIA/DISK1/23568742.MOV
/MEDIA/DISK1/87456321.AVI
/MEDIA/DISK2/PART1/45753131.AVI
/IMPORT/44452.WAV
[root@luke /tmp]$ sed 's/.\{9\}AVI$//' test.txt > test_out.txt
[root@luke /tmp]$ cat test_out.txt
/MEDIA/DISK1/23568742.MOV
/MEDIA/DISK1/87456321.AVI
/MEDIA/DISK2/PART1/45753131.AVI
/IMPORT/44452.WAV
[root@luke /tmp]$ sed 's/[0-9]\{1,\}.AVI$//' test.txt > test_out.txt
[root@luke /tmp]$ cat test_out.txt
/MEDIA/DISK1/23568742.MOV
/MEDIA/DISK1/87456321.AVI
/MEDIA/DISK2/PART1/45753131.AVI
/IMPORT/44452.WAV
[root@luke /tmp]$

Why did not it work?

Thank you.

can you try this..

sed 's/.........AVI//' test.txt

if this is also not working, can you post the output of the below command

od -c test.txt
1 Like

try:

dos2unix infile | sed 's/.\{9\}AVI$//'

Hi Itkamaraj,

This way does work. Thank you very much!!!

---------- Post updated at 06:06 PM ---------- Previous update was at 06:01 PM ----------

Hi Rdrtx1,

converting file to Unix format not working.

Thank you.

In my command, can you add $ to make sure, you are changing the lines with end with AVI

sed 's/.........AVI$//' test.txt

what is your OS ? and what vesrion of SED you are using ?

With $ also works.
My OS is RHELinux WS release 4 (Nahant Update 3) and version of sed is 4.1.2.

I don't think dos2unix and unix2dos are filters; they make changes to the file given by overwriting it. So, piping the output to sed does not make sense here.