sed 正则表达式疑惑

2018-03-05 11:44:15 +08:00
 heiybb

是学校的 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 都没见过这种用法,非常疑惑

希望有大佬能解惑

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


剩下的问题都很简单,作为学计算机的怎么连 l 和 1 都分不清😓
heiybb
2018-03-05 14:35:16 +08:00
@Arnie97 #1 注意到了已经不能 EDIT 了,非常感谢,已经理解了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/434895

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX