我正在尝试移动一个现有的行,该行以let's say开头:abc::
位于文件夹中各个md文件中以id:
开头的现有行下面。
我们假设文件是这样的
(file 1)
text
id: 123
more text
abc:: cba
(file 2)
text
id: 321
more text
a lot more text
abc:: def
在这两种情况下,运行代码的结果都应该是abc::...
移动到id:...
的正下方
我的假设是,下面的代码会将以abc::
开始的行移动到包含id:
的行的正下方。但是,如果我运行代码,它会运行文件(根据编辑的时间戳),但不会改变任何内容。
MWE:
find . -name '*.md' -type f -exec sed -i '' '/^id:/{
N
s/\n\(abc::.*\)/\1\
/
}' {} +
2条答案
按热度按时间v8wbuo2f1#
这个
sed
命令(通过find
的-exec
执行)应该可以做到这一点:lrpiutwd2#
使用任何awk,包括MacOS上默认的BSD awk:
即使
abc::
行出现在输入中的id:
行之前,如果其中一行没有出现在输入中,它也会按原样打印文件。