inaki
October 10, 2012, 2:14pm
1
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
inaki
October 23, 2012, 10:55am
4
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
Yoda
October 23, 2012, 10:59am
6
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
inaki
October 23, 2012, 11:14am
7
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
rdrtx1
October 23, 2012, 11:44am
9
try:
dos2unix infile | sed 's/.\{9\}AVI$//'
inaki
October 23, 2012, 12:06pm
10
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 ?
inaki
October 23, 2012, 12:18pm
12
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.