sed替换文本并放到行尾

时间:2013-10-02 18:31:36

标签: regex sed

获得以下输出:

<iframe  src="http://domain.xy/load.php?file=2260301" frameborder="0" scrolling="no"></iframe>|http://img1.domain.xy/t/301/320/5_2260301.jpg;http://img1.domain.xy/t/301/320/1_2260301.jpg;http://img1.domain.xy/t/301/320/2_2260301.jpg;http://img1.domain.xy/t/301/320/3_2260301.jpg;http://img1.domain.xy/t/301/320/4_2260301.jpg;http://img1.domain.xy/t/301/320/6_2260301.jpg;http://img1.domain.xy/t/301/320/7_2260301.jpg;http://img1.domain.xy/t/301/320/8_2260301.jpg;http://img1.domain.xy/t/301/320/9_2260301.jpg;http://img1.domain.xy/t/301/320/10_2260301.jpg|
<iframe  src="http://domain.xy/load.php?file=2260279" frameborder="0" scrolling="no"></iframe>|http://img9.domain.xy/t/279/320/10_2260279.jpg;http://img9.domain.xy/t/279/320/1_2260279.jpg;http://img9.domain.xy/t/279/320/2_2260279.jpg;http://img9.domain.xy/t/279/320/3_2260279.jpg;http://img9.domain.xy/t/279/320/4_2260279.jpg;http://img9.domain.xy/t/279/320/5_2260279.jpg;http://img9.domain.xy/t/279/320/6_2260279.jpg;http://img9.domain.xy/t/279/320/7_2260279.jpg;http://img9.domain.xy/t/279/320/8_2260279.jpg;http://img9.domain.xy/t/279/320/9_2260279.jpg|
<iframe  src="http://domain.xy/load.php?file=2259929" frameborder="0" scrolling="no"></iframe>|http://img9.domain.xy/t/929/320/1_2259929.jpg;http://img9.domain.xy/t/929/320/2_2259929.jpg;http://img9.domain.xy/t/929/320/3_2259929.jpg;http://img9.domain.xy/t/929/320/4_2259929.jpg;http://img9.domain.xy/t/929/320/5_2259929.jpg;http://img9.domain.xy/t/929/320/6_2259929.jpg;http://img9.domain.xy/t/929/320/7_2259929.jpg;http://img9.domain.xy/t/929/320/8_2259929.jpg;http://img9.domain.xy/t/929/320/9_2259929.jpg;http://img9.domain.xy/t/929/320/10_2259929.jpg|

并需要以下内容:

<iframe  src="http://domain.xy/load.php?file=2260301" frameborder="0" scrolling="no"></iframe>|5_2260301.jpg;1_2260301.jpg;2_2260301.jpg;3_2260301.jpg;4_2260301.jpg;6_2260301.jpg;7_2260301.jpg;8_2260301.jpg;9_2260301.jpg;10_2260301.jpg|http://img1.domain.xy/t/301/320/|
<iframe  src="http://domain.xy/load.php?file=2260279" frameborder="0" scrolling="no"></iframe>|10_2260279.jpg;1_2260279.jpg;2_2260279.jpg;3_2260279.jpg;4_2260279.jpg;5_2260279.jpg;6_2260279.jpg;7_2260279.jpg;8_2260279.jpg;9_2260279.jpg|http://img9.domain.xy/t/279/320/|
<iframe  src="http://domain.xy/load.php?file=2259929" frameborder="0" scrolling="no"></iframe>|1_2259929.jpg;2_2259929.jpg;3_2259929.jpg;4_2259929.jpg;5_2259929.jpg;6_2259929.jpg;7_2259929.jpg;8_2259929.jpg;9_2259929.jpg;10_2259929.jpg|http://img9.domain.xy/t/929/320/

我使用此命令只获取jpg filname,

sed 's|http://img*......................||g'
这不优雅。我将寻找另一个解决方案。我需要知道如何保存目录网址并将每个网址放在行http://img9.domain.xy/t/929/320/

的末尾

请求帮助

2 个答案:

答案 0 :(得分:0)

如果您想要网址的最后一部分,例如“http://img1.domain.xy/t/301/320/5_2260301.jpg”,您可以使用此正则表达式:/([0-9_]+\.[a-z]{3,4}+)

适用于所有号码和所有分机(.jpeg,.png,...)

答案 1 :(得分:0)

while IFS= read -r line; 
do 
  var=$(echo "$line" | grep -oE "http://img[0-9].domain.xy/t/[0-9][0-9][0-9]/[0-9][0-9][0-9]/" | uniq); 
  echo "$line" | sed -e 's|http://img[0-9].domain.xy/t/[0-9][0-9][0-9]/[0-9][0-9][0-9]/||g' -e "s|.*|&"${var}"|g" >> newFile; 
done < file;

使用while然后使用grep模式http://img...读取文件的每一行,将其放入变量中,使用sed将该模式删除并放入变量在最后一行。

结果写入新文件:

<iframe  src="http://domain.xy/load.php?file=2260301" frameborder="0" scrolling="no"></iframe>|5_2260301.jpg;1_2260301.jpg;2_2260301.jpg;3_2260301.jpg;4_2260301.jpg;6_2260301.jpg;7_2260301.jpg;8_2260301.jpg;9_2260301.jpg;10_2260301.jpg|http://img1.domain.xy/t/301/320/
<iframe  src="http://domain.xy/load.php?file=2260279" frameborder="0" scrolling="no"></iframe>|10_2260279.jpg;1_2260279.jpg;2_2260279.jpg;3_2260279.jpg;4_2260279.jpg;5_2260279.jpg;6_2260279.jpg;7_2260279.jpg;8_2260279.jpg;9_2260279.jpg|http://img9.domain.xy/t/279/320/
<iframe  src="http://domain.xy/load.php?file=2259929" frameborder="0" scrolling="no"></iframe>|1_2259929.jpg;2_2259929.jpg;3_2259929.jpg;4_2259929.jpg;5_2259929.jpg;6_2259929.jpg;7_2259929.jpg;8_2259929.jpg;9_2259929.jpg;10_2259929.jpg|http://img9.domain.xy/t/929/320/
相关问题