Grep Select 代码块中的所有图像

Grep Select ALL Images in code block

我在多个来源中搜索了 Grep 和 RegEx select 或 select 大量乱码和文本集合中的所有图像。我最接近的是 ,它对我不起作用。

我需要 select 我的源文件中所有图像名称的第一次出现(或将所有图像名称复制到一个单独的文件),例如:

/Volumes/Data Drive/joomla-2-wp/wp-content/uploads/2003/12/someurl.com_images_ABanner.gif

只会select

someurl.com_images_ABanner.gif

这是我尝试搜索的文本示例:

[fg-joomla-to-wordpress] Can't copy http://someurl.com/images/banners/ABanner.gif to /Volumes/Data Drive/joomla-2-wp/wp-content/uploads/2003/12/someurl.com_images_banners_ABanner.gif : Not Found
[fg-joomla-to-wordpress] Can't copy http://someurl.com/images/randy.jpg to /Volumes/Data Drive/joomla-2-wp/wp-content/uploads/2003/12/someurl.com_images_randy.jpg : Not Found
[fg-joomla-to-wordpress] Can't copy http://www.differenturl.com/images-body0/logo2.gif to /Volumes/Data Drive/joomla-2-wp/wp-content/uploads/2003/12/www.differenturl.com_images-body0_logo2.gif : Not Found
[fg-joomla-to-wordpress] Can't copy /images/DiffImage.jpg to /Volumes/Data Drive/joomla-2-wp/wp-content/uploads/2003/12/images_DiffImage.jpg : A valid URL was not provided.
[fg-joomla-to-wordpress] Can't copy /images/DSCN0248.jpg to /Volumes/Data Drive/joomla-2-wp/wp-content/uploads/2003/12/images_DSCN0248.jpg : A valid URL was not provided.

我认识到第一次出现的模式包含 /images/ 但有一些例外(例如 /images-body0/imagename.jpg),而目标不包含,这简化了它,但我就是无法理解.

使用 awk

如果我没理解错的话,您在示例文本中查找的是第四个字段最后一个路径元素。在那种情况下:

$ awk '{n=split(,a,"/"); print a[n]}' file
ABanner.gif
randy.jpg
logo2.gif
DiffImage.jpg
DSCN0248.jpg

使用 sed

获取文件名中存在于copyto之间的最后一个元素:

$ sed -E 's|.* copy .*/(.*) to .*||' file
ABanner.gif
randy.jpg
logo2.gif
DiffImage.jpg
DSCN0248.jpg

sed 的扩展 (-E) 正则表达式怎么样?我正在为您输入的行尾 : 之前出现的所有图像(jpg、gif、png)进行选择。

$ sed -nE 's,^.*/([^/]*(jpg|gif|png)) : .*$,,p' file
someurl.com_images_banners_ABanner.gif
someurl.com_images_randy.jpg
www.differenturl.com_images-body0_logo2.gif
images_DiffImage.jpg
images_DSCN0248.jpg

如果文件中的所有行都具有与示例中相同的模式,您可以像这样简单地提取每行的第 7 个字段:

$ cat file
[fg-joomla-to-wordpress] Can't copy http://someurl.com/images/banners/ABanner.gif to /Volumes/Data Drive/joomla-2-wp/wp-content/uploads/2003/12/someurl.com_images_banners_ABanner.gif : Not Found
[fg-joomla-to-wordpress] Can't copy http://someurl.com/images/randy.jpg to /Volumes/Data Drive/joomla-2-wp/wp-content/uploads/2003/12/someurl.com_images_randy.jpg : Not Found
[fg-joomla-to-wordpress] Can't copy http://www.differenturl.com/images-body0/logo2.gif to /Volumes/Data Drive/joomla-2-wp/wp-content/uploads/2003/12/www.differenturl.com_images-body0_logo2.gif : Not Found
[fg-joomla-to-wordpress] Can't copy /images/DiffImage.jpg to /Volumes/Data Drive/joomla-2-wp/wp-content/uploads/2003/12/images_DiffImage.jpg : A valid URL was not provided.
[fg-joomla-to-wordpress] Can't copy /images/DSCN0248.jpg to /Volumes/Data Drive/joomla-2-wp/wp-content/uploads/2003/12/images_DSCN0248.jpg : A valid URL was not provided.

$ cut -d' ' -f7 file | sed '/images/ s#.*/\([^/]*\)##'
someurl.com_images_banners_ABanner.gif
someurl.com_images_randy.jpg
www.differenturl.com_images-body0_logo2.gif
images_DiffImage.jpg
images_DSCN0248.jpg