sed追加从stdout获得的文本(sed append text obtained from stdout)
我知道sed可以通过几种不同的方式添加文本:
追加字符串NEW_TEXT :sed -i '/PATTERN/ a NEW_TEXT' file
附加文件file_with_new_text内容:sed -i '/PATTERN/ r file_with_new_text' file
然而,有无论如何追加通过标准输出到sed文本? 我希望获得相当于:
# XXX,YYY,ZZZ are line numbers
# The following appends lines XXX-YYY in file1
# after line ZZZ in file2.
sed 'XXX,YYY p' file1 > /tmp/file
sed -i 'ZZZ r /tmp/file' file2
而不必使用临时文件。 这可能吗?
I know sed can append text in a few different ways:
to append the string NEW_TEXT: sed -i '/PATTERN/ a NEW_TEXT' file
to append contents of file file_with_new_text: sed -i '/PATTERN/ r file_with_new_text' file
However, is there anyway to append text piped to sed via stdout? I am hoping to obtain the equivalent of:
# XXX,YYY,ZZZ are line numbers
# The following appends lines XXX-YYY in file1
# after line ZZZ in file2.
sed 'XXX,YYY p' file1 > /tmp/file
sed -i 'ZZZ r /tmp/file' file2
without having to use a temporary file. Is this possible?
原文:https://stackoverflow.com/questions/21637811
更新时间:2020-01-22 16:28
最满意答案
您可以从/dev/stdin读取。 显然,这只会在每个过程中运行一次。
例:
$ cat file1
this is text from file1
etc
$ cat file2
Hello world from file2
$ sed -n '1p' file1 | sed -i '$ r /dev/stdin' file2
$ cat file2
Hello world from file2
this is text from file1
You can read from /dev/stdin. Clearly, this will only work once per sed process.
Example:
$ cat file1
this is text from file1
etc
$ cat file2
Hello world from file2
$ sed -n '1p' file1 | sed -i '$ r /dev/stdin' file2
$ cat file2
Hello world from file2
this is text from file1
2014-02-07
相关问答
你可以使用awk : awk 'BEGIN{FS=OFS=", "} {for (i=1; i<=NF; i++) $i = "cast(" $i " as string)"} 1' <<< "$test"
cast(abc as string), cast(def as string)
You can use awk: awk 'BEGIN{FS=OFS=", "} {for (i=1; i<=NF; i++) $i = "cast(" $i " as string)"} 1' <<< "$
...
我不明白为什么它也不起作用,但你也可以使用替换选项而不是附加: ${SED} -i "s%-i lo.*%&\n\n#Trusted Traffic\n-A INPUT -s 10.153.156.0/25,10.153.174.160/27 -d ${MGTIP} -m state --state NEW -j ACCEPT\n\n#Remote Access\n-A INPUT -s 10.120.80.0/21,10.152.80.0/21,10.153.193.0/24,172.18.1.
...
没有可以方便地执行此操作的本机批处理工具或实用程序。 可以编写纯批处理文件,但它会很难看,而且速度很慢。 我相信你可以找到一个可以完成这项工作的unix工具的Windows端口。 或者您可以编写一个简单的自定义JScript或VBS脚本。 我还想象使用PowerShell很容易。 我会使用我的JREPL.BAT实用程序 - 一个混合的JScript /批处理脚本,它在stdin上执行正则表达式查找和替换。 JREPL.BAT是纯脚本,可以在XP以后的任何Windows机器上本机运行。 假设JREP
...
尝试以下(看起来STDERR上的sox输出): play File.wav >> output.txt 2>&1
它是否易于解析是一个不同的故事...... :) 即 [00:01:30.31] Out:532k [ -===|===- ] Clip:0
通过重定向将各个行分解。 Try the following (it would appear that sox outputs on STDERR): play File.wav >> output.txt 2>&1
Whether it
...
这种类型的事情通常最好用awk这样的语言完成,例如: awk 'NR==1{n=NF}{$n=$n}1' FS=, OFS=, file
This type of thing is usually best done with a language like awk, for example: awk 'NR==1{n=NF}{$n=$n}1' FS=, OFS=, file
您可以从/dev/stdin读取。 显然,这只会在每个过程中运行一次。 例: $ cat file1
this is text from file1
etc
$ cat file2
Hello world from file2
$ sed -n '1p' file1 | sed -i '$ r /dev/stdin' file2
$ cat file2
Hello world from file2
this is text from file1
You can read from /dev
...
在第一次出现字符串之前添加东西比较容易: sed '//i\ADD SOME TEXT\nADD SOME MORE TEXT' file
结果: <?xml version="1.0" encoding="utf-8"?>
1
...
这是因为除了你用tee做的事情之外,你还用> bin/newfile.conf重定向屏幕输出。 只需删除>及其后的所有内容即可。 如果除了屏幕之外还要一次输出这两个文件,可以使用tee两次,例如: printf ... | tee -a bin/logfile.log | tee bin/newfile.conf
这将附加到logfile.log并覆盖newfile.conf ,并写入屏幕。 根据需要使用或省略-a选项。 正如John1024指出的那样你也可以使用tee一次,因为它接受多个文件名
...
您可以提供以下功能: function(CleanMessage)
execute_process(COMMAND ${CMAKE_COMMAND} -E echo "${ARGN}")
endfunction()
并像这样使用它: CleanMessage("Clean text")
如果你想推出船,你甚至可以扩展内置的message选项,包括一个CLEAN : function(message MessageType)
if(${ARGV0} STREQUAL "CLEAN")
...