我一直试图插入一个文件作为第一行与以下SED命令,但没有多少成功。每次在第1行之后插入文件时,是否会在第1行之前插入一个开关?
sed -i '1r file1.txt' file2.txt提前谢谢。
发布于 2017-01-14 21:24:12
N命令如果file2.txt有多行(其他请参见另一节):
sed -i -e '1 { r file1.txt' -e 'N; }' file2.txt诀窍是使用N命令推迟第一行的打印。
从手册中:
N将下一行输入附加到模式空间中。
e命令如果file2.txt不是空的(否则只需做一个副本):
sed -i -e '1 e cat file1.txt' file2.txtGNU sed提供了一个e命令,用于执行参数中的命令。其结果是立即输出。
从手册中:
E 命令此命令允许将输入从shell命令输送到模式空间。没有参数,
e' command executes the command that is found in pattern space and replaces the pattern space with the output; a trailing newline is suppressed. If a parameter is specified, instead, thee‘命令将其解释为命令,并将其输出发送到输出流。请注意,与'r‘命令不同,命令的输出将立即打印;而'r’命令则将输出延迟到当前周期的结束。
发布于 2017-01-14 21:22:51
如果您决心避免将cat转到显式临时文件(或通过缓冲区(如sponge) ),那么看起来ed至少会接受它的r命令的0地址:
ed -s file2.txt << EOF
0r file1.txt
wq
EOF或等量
printf '0r file1.txt\nwq\n' | ed -s file2.txthttps://unix.stackexchange.com/questions/337435
复制相似问题