sed a 命令

echo_no_bug 2015-10-10 11:12:48
$sed '/1333/a\' second.txt
1
third
$sed '/1/a\' second.txt
1
third
$
并没有插入 也没有提示插入的地方

[1addr]a\
text Write text to standard output immediately before each attempt to read a line of input, whether by executing the ``N'' function or by beginning a new
cycle.

[2addr]b[label]
Branch to the ``:'' function with the specified label. If the label is not specified, branch to the end of the script.

[2addr]c\
text Delete the pattern space. With 0 or 1 address or at the end of a 2-address range, text is written to the standard output.

[2addr]d
Delete the pattern space and start the next cycle.


d能看懂 a\ c\是什么鬼
The argument text consists of one or more lines. To embed a newline in the text, precede it with a backslash. Other backslashes in text are deleted and the
following character taken literally.
...全文
349 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
echo_no_bug 2015-10-15
  • 打赏
  • 举报
回复
引用 7 楼 vachel66 的回复:
[quote=引用 6 楼 vachel66 的回复:] 下面是我的bash环境。你回复我怎么不引用我回复,而引用你自己的回复呢? $ echo $SHELL /bin/bash $ bash -version GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) Copyright (C) 2009 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
发现我们搞错了,这个问题应该看sed的版本,而不是bash的版本。下面我的sed 版本,不知OS X 上是不是GNU sed $ sed --version GNU sed version 4.2.1 Copyright (C) 2009 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, to the extent permitted by law. [/quote]
引用 7 楼 vachel66 的回复:
[quote=引用 6 楼 vachel66 的回复:] 下面是我的bash环境。你回复我怎么不引用我回复,而引用你自己的回复呢? $ echo $SHELL /bin/bash $ bash -version GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) Copyright (C) 2009 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
发现我们搞错了,这个问题应该看sed的版本,而不是bash的版本。下面我的sed 版本,不知OS X 上是不是GNU sed $ sed --version GNU sed version 4.2.1 Copyright (C) 2009 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, to the extent permitted by law. [/quote] 应该是内嵌入os系统了吧。。。
echo_no_bug 2015-10-15
  • 打赏
  • 举报
回复
引用 6 楼 vachel66 的回复:
[quote=引用 5 楼 naixiyi 的回复:] [quote=引用 3 楼 naixiyi 的回复:] 没有人么 up
你是哪个shell呢?? 我的是bash $ echo $SHELL /bin/bash [/quote] 下面是我的bash环境。你回复我怎么不引用我回复,而引用你自己的回复呢? $ echo $SHELL /bin/bash $ bash -version GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) Copyright (C) 2009 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. [/quote] 手滑。。。。。。。奇怪了 sed的version都打不开 我试着php awk的version又可以。。。。 真心辛苦了 这么晚还回答我问题 ~/Public/pingan/tmp $php --version PHP 5.4.38 (cli) (built: Mar 19 2015 14:49:15) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies ~/Public/pingan/tmp $sed --version sed: illegal option -- - usage: sed script [-Ealn] [-i extension] [file ...] sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...] ~/Public/pingan/tmp $awk --version awk version 20070501
vachel66 2015-10-15
  • 打赏
  • 举报
回复
引用 6 楼 vachel66 的回复:
下面是我的bash环境。你回复我怎么不引用我回复,而引用你自己的回复呢? $ echo $SHELL /bin/bash $ bash -version GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) Copyright (C) 2009 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
发现我们搞错了,这个问题应该看sed的版本,而不是bash的版本。下面我的sed 版本,不知OS X 上是不是GNU sed $ sed --version GNU sed version 4.2.1 Copyright (C) 2009 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, to the extent permitted by law.
vachel66 2015-10-15
  • 打赏
  • 举报
回复
引用 5 楼 naixiyi 的回复:
[quote=引用 3 楼 naixiyi 的回复:] 没有人么 up
你是哪个shell呢?? 我的是bash $ echo $SHELL /bin/bash [/quote] 下面是我的bash环境。你回复我怎么不引用我回复,而引用你自己的回复呢? $ echo $SHELL /bin/bash $ bash -version GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) Copyright (C) 2009 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
echo_no_bug 2015-10-15
  • 打赏
  • 举报
回复
引用 3 楼 naixiyi 的回复:
没有人么 up
你是哪个shell呢?? 我的是bash $ echo $SHELL /bin/bash
vachel66 2015-10-15
  • 打赏
  • 举报
回复
没有OS X 环境,在Linux 上man sed 没有你提到的那段,看来不同环境上是有区别
echo_no_bug 2015-10-15
  • 打赏
  • 举报
回复
没有人么 up
echo_no_bug 2015-10-15
  • 打赏
  • 举报
回复
引用 楼主 naixiyi 的回复:
$sed '/1333/a\' second.txt 1 third $sed '/1/a\' second.txt 1 third $ 并没有插入 也没有提示插入的地方 [1addr]a\ text Write text to standard output immediately before each attempt to read a line of input, whether by executing the ``N'' function or by beginning a new cycle. [2addr]b[label] Branch to the ``:'' function with the specified label. If the label is not specified, branch to the end of the script. [2addr]c\ text Delete the pattern space. With 0 or 1 address or at the end of a 2-address range, text is written to the standard output. [2addr]d Delete the pattern space and start the next cycle. d能看懂 a\ c\是什么鬼 The argument text consists of one or more lines. To embed a newline in the text, precede it with a backslash. Other backslashes in text are deleted and the following character taken literally.
上次回复了 居然没有提交上来 我的是os bash 是脚本的差异吧 sed: 1: "a It works.": command a expects \ followed by text ~/Public/pingan/tmp $sed 'c\ It works.' testls_sort.txt sed: 1: "c\ It works.": extra characters after \ at the end of c command ~/Public/pingan/tmp $sed 'c It works.' testls_sort.txt sed: 1: "c It works.": command c expects \ followed by text -----------我是分割线------------------- 我可能会问一个傻逼的问题。以下是的man sed 结果截取的一段 SYNOPSIS sed [-Ealn] command [file ...] sed [-Ealn] [-e command] [-f command_file] [-i extension] [file ...] -E Interpret regular expressions as extended (modern) regular expressions rather than basic regular expressions (BRE's). The re_format(7) manual page fully describes both formats. 傻逼问题来了,所以大致的格式是sed -e command [file] 这个command 指的是什么?在man sed结果的哪个部分呢,我怎么找到呢
vachel66 2015-10-11
  • 打赏
  • 举报
回复
你的格式错了,所以没有效果,看我下面测试的情况。

首先测试文件test.txt 包含如下两行内容

$ less test.txt
This is a test file.
I am testing sed command.
$


然后看sed a 的效果,在每行后面追加了一行

$ sed 'a It works.' test.txt
This is a test file.
It works.
I am testing sed command.
It works.
$


可以指定只在第二行后面追加内容,如果要追加的内容需要换行,就先输入反斜线,再敲回车

$ sed '2a It works.\
Very great!' test.txt
This is a test file.
I am testing sed command.
It works.
Very great!
$


c的用法与a类似,例如,将第一行替换为指定内容

$ sed '1c The first line is replaced.' test.txt
The first line is replaced.
I am testing sed command.
$


像sed, awk这些高级命令我也记不住具体格式,每次都需要查下测试下才能写出来
今天值班ing

19,612

社区成员

发帖
与我相关
我的任务
社区描述
系统使用、管理、维护问题。可以是Ubuntu, Fedora, Unix等等
社区管理员
  • 系统维护与使用区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧