V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
heiybb
V2EX  ›  问与答

sed 正则表达式疑惑

  •  
  •   heiybb · 2018-03-05 11:44:15 +08:00 · 784 次点击
    这是一个创建于 2216 天前的主题,其中的信息可能已经有所发展或是发生改变。

    是学校的 SKILL TEST 题目
    Q5. Unix is famous for 'one-liners'. Complex commands that achieve a lot in one line. In the next few questions we will look at some. Consider the following line - between the {}'s. { ls -1 | sed -e 's@^\(.*\)$@<li><a href="\1">\1</a><br>@' -e '1i<html><body><ol>' -e '$a</ol></body></html>' >index.html }. Say in a few words what this line does. Run it and see.

    当前目录下有文件 package.xml test 键入命令后

    cat index.html:

    <html><body><ol> <li><a href="index.html">index.html</a><br> <li><a href="package.xml">package.xml</a><br> <li><a href="test">test</a><br> </ol></body></html>

    以前都是用 python 处理正则从来不用 sed

    sed -e '$a</ol></body></html>'能理解是在最后一行存入</ol></body></html>

    不能理解为什么'1i<html><body><ol>'在第一行且 li 丢失

    's@^\(.*\)$@<li><a href="\1">\1</a><br>@'这一段中为什么\l可以代表文件名还有其中的 @意思是什么一直搜不到 google 和 stackoverflow 及 github 都没见过这种用法,非常疑惑

    希望有大佬能解惑

    2 条回复    2018-03-05 14:35:16 +08:00
    Arnie97
        1
    Arnie97  
       2018-03-05 13:53:30 +08:00 via Android
    我解答一个不好搜的问题: s@xxxx@yyyy@ 相当于 s/xxxx/yyyy/,因为文本自身包含很多 /,如果用 / 作为分隔符就要把文本自身的 / 全换成 \/,不方便。


    剩下的问题都很简单,作为学计算机的怎么连 l 和 1 都分不清😓
    heiybb
        2
    heiybb  
    OP
       2018-03-05 14:35:16 +08:00
    @Arnie97 #1 注意到了已经不能 EDIT 了,非常感谢,已经理解了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2740 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 12:40 · PVG 20:40 · LAX 05:40 · JFK 08:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.