[英]How to use Bash (sed or any other) command to insert at EOF-nth line
I did search in Stack Overflow, but, I couldn't find a solution. 我确实在Stack Overflow中搜索,但是找不到解决方案。 Did try enough.
尝试够了。
I have a file containing code like this- 我有一个包含这样的代码的文件-
...
{
...
void abc()
{
if(condition)
{
x=y;
z=0;
}
}
} //EOF
I want to insert some lines of code in between x=y;
我想在
x=y;
之间插入一些代码行x=y;
and z=0;
并且
z=0;
. 。
This can be done by 2 methods- 这可以通过2种方法完成-
x=y;
x=y;
and inserting later Finding no. 没有发现。 of lines in file -
文件中的行数-
NUMOFLINES = $(wc -l some.txt | awk '{print $1}') TOINSERTLINE=NUMOFLINES-5 NUMOFLINES = $(wc -l some.txt | awk'{print $ 1}')TOINSERTLINE = NUMOFLINES-5
So, inserting at TOINSERTLINE's value. 因此,以TOINSERTLINE的值插入。
More optimal methods are welcome. 欢迎使用更多最佳方法。 Help me fix this.
帮我解决这个问题。
Concluded Answer from the discussion- 从讨论中得出的结论-
sed -i.bak -e '^/x=y;/r file-to-insert' *.xaml
sed -i.bak -e'^ / x = y; / r文件插入'* .xaml
This worked for me!! 这对我有用!
Thanks to Jonathan Leffler and anubhava . 感谢Jonathan Leffler和anubhava 。 These were the closest to what I wanted.
这些是我想要的最接近的东西。
If you want to insert a line after 5th line use this sed
: 如果要在第五行之后插入一行,请使用以下
sed
:
sed -i.bak $TOINSERTLINE'r data-file' file
Here data to insert comes from data-file
这里要插入的
data-file
来自data-file
This is one way: 这是一种方法:
(
sed '/x=y;/q'
echo "NUMOFLINES - 5"
cat
) < file
awk '1; /x=y/{print "foo"}' file
awk -v nl=$(wc -l < file) '1; NR==(nl-5){ print "foo" }' file
If those don't do what you want, edit your question to clarify your requirements. 如果这些人没有做您想要的,请编辑您的问题以阐明您的要求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.