luofeiyu_cu 发表于 2013-12-31 15:46

sed 中的数字含义

sed '1 i\
<!DOCTYPE html>\
<html lang=\"en\">\
<head>\
<title>The Rime of the Ancyent Mariner (1798)</title>\
</head>\
<body>
s/^/<h1>/
s/$/<\/h1>/
q'rime.txt

请问,这里的 sed '1 i\    #此处的数字1 是什么含义?

luofeiyu_cu 发表于 2013-12-31 16:12

如果是在第一行后面插入,为何下面的命令,运行后rime.txt仅仅剩下一行了?

$   sed '3 i\
>   <!DOCTYPE html>\
>   <html lang=\"en\">\
>   <head>\
>   <title>The Rime of the Ancyent Mariner (179</title>\
>   </head>\
>   <body>
>   s/^/<h1>/
>   s/$/<\/h1>/
>   q'-i rime.txt

sanya@ligong-a691b08a /cygdrive/c/workspace
$ cat rime.txt
<h1>THE RIME OF THE ANCYENT MARINERE, IN SEVEN PARTS.</h1>
我给出数字3是要在原文第三行,插入一些内容,为何那些内容没有了,仅仅剩下一行了?

井蛙夏虫 发表于 2013-12-31 19:22

查查sed的q命令是做什么的

syre 发表于 2014-01-01 21:40

man sed
就有sed的很详细的文档

我学sed就是这么学的,有啥东西忘了怎么写也是这么查的。

luofeiyu_cu 发表于 2014-01-01 21:50

$   sed '3 i\
>   <!DOCTYPE html>\
>   <html lang=\"en\">\
>   <head>\
>   <title>The Rime of the Ancyent Mariner (179</title>\
>   </head>\
>   <body>
>   s/^/<h1>/
>   s/$/<\/h1>/
>   q'-i rime.txt

我这样理解,sed 在第三行后面插入
   <!DOCTYPE html>\
   <html lang=\"en\">\
   <head>\
   <title>The Rime of the Ancyent Mariner (179</title>\
   </head>\
   <body>

然后,再插入<h1>   <\h1>,然后退出,为何
   <!DOCTYPE html>\
   <html lang=\"en\">\
   <head>\
   <title>The Rime of the Ancyent Mariner (179</title>\
   </head>\
   <body>

这个内容没有了呢?

井蛙夏虫 发表于 2014-01-02 00:31

你应当去看看sed的执行流程,info sed中就有
你这个的执行流程,我是这样理解的:
读人rime.txt第一行,执行引号内的sed命令,第一条命令因为只适用于第3行,所以跳过;
第二、三条命令分别在这一行的前后添加内容,然后q命令退出。引号内命令执行完后,
因为没有使用"-n",所以会输出这行的内容。又因为使用了q命令,所以不会再继续处理文件内容。
而"-i"导致使用输出的内容替换掉原文件的内容,所以只剩下一句内容了。
页: [1]
查看完整版本: sed 中的数字含义