2012-12-08 23 views
1

我有以下文本文件:与前桑达模式斜杠

  • 内容的这一/媒体/新闻/ SECTION3/S02 /基本/的file.mp4的名称,然后545756.
  • Content/of/media/news/section3/S02/file.mp4的名称然后是42346.
  • Content/of this/media/news/random3/S02/basic/file.mp4的名称然后是543.
  • Content/of this/media/news/random3/S02/basic/file.mp4然后789的名称。

我正在寻找摆脱“此内容/媒体/新闻/第3节”或“ - 这个/媒体/新闻/随机3的内容”和“然后* *号码”。 我想留下只有“文件名称.mp4” 也有时文件的名称也打印这样的“Name.of.the.file.mp4”

我试过不同的看到的方式,但我只是一个初学者,它变得非常混乱,特别是与正斜杠。 任何帮助,将不胜感激。

+0

难道这只是存储在一个文本文件,你说,或者是你的计算机上的这些实际MP4文件? –

回答

0

这并不直接回答你的问题,但它可能会做你需要它无论如何:

如果这些是你所描述的在您的计算机上mp4文件,你可以得到的文件的名称为如下:

find /path/to/some/base/dir -type f -name "*.mp4" -exec basename {} \; 

这会给你的文件/path/to/some/base/dir下的所有文件mp4的名称(不带目录路径前缀)。


如果这些实际上是从一个文件,你需要操纵线,下面应该工作,虽然有点哈克:

awk 'BEGIN{FS="/"} {print $NF}' input_file.txt | awk '{$NF=$(NF-1)=""; print}' 
1

尝试:

sed 's/.*\/\(.*mp4\).*/\1/' /path/to/your/file.txt 
0

假设你的文件被命名为files.txt,并且假设您只对mp4文件感兴趣,则以下sed命令应该可以使用,无论是否带有点的名称在其中:

sed -i "s/^.*\/\(.*mp4\).*$/\1/g" files.txt 

我叫我的文件files.txt,这些都是它的内容,前后上述命令后:

以前

Content-of this /media/news/section3/S02/basic/Name of the file.mp4 then 545756. 
Content-of this /media/news/section3/S02/Name of the file.mp4 then 42346. 
Content-of this /media/news/random3/S02/basic/Name.of.the.file.mp4 then 543. 
Content-of this /media/news/random3/S02/basic/Name of the file.mp4 then 789. 

Name of the file.mp4 
Name of the file.mp4 
Name.of.the.file.mp4 
Name of the file.mp4 
0

另一种解决方案:

awk '{gsub(/[^.]*\//,""); for(i=1;i<=NF-2;i++) {printf "%s ", $i} print ""}' file 
0

没有必要对awksed。你可以简单地使用grep

grep -o "[^/]*\.mp4" file 

说明:

-o, --only-matching 
     Print only the matched (non-empty) parts of a matching line, with each 
     such part on a separate output line. 

[^/]* Match anything not a forward slash any number of times 

\.mp4 Remember to escape the dot metacharacter. 
0

为了避免混淆与斜线它有助于知道的sed s命令未绑定到/:虽然通常形式的s命令为s/pattern/replacement/,则可以用其他字符替换正斜杠,例如s,pattern,replacement,。因此,要改写 @ adayzdone的答案,你可以这样写:

sed 's,.*/\(.*mp4\).*,\1,' /path/to/your/file.txt