bash 从特定行开始在文件中插入行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13316437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert lines in a file starting from a specific line
提问by Sadiel
I would like to insert lines into a file in bash starting from a specific line.
我想在 bash 中从特定行开始将行插入到文件中。
Each line is a string which is an element of an array
每一行都是一个字符串,它是数组的一个元素
line[0]="foo"
line[1]="bar"
...
and the specific line is 'fields'
并且特定行是“字段”
file="$(cat $myfile)"
for p in $file; do
if [ "$p" = 'fields' ]
then insertlines() #<- here
fi
done
回答by Chris Seymour
This can be done with sed: sed 's/fields/fields\nNew Inserted Line/'
这可以通过 sed 完成: sed 's/fields/fields\nNew Inserted Line/'
$ cat file.txt
line 1
line 2
fields
line 3
another line
fields
dkhs
$ sed 's/fields/fields\nNew Inserted Line/' file.txt
line 1
line 2
fields
New Inserted Line
line 3
another line
fields
New Inserted Line
dkhs
Use -i
to save in-place instead of printing to stdout
用于-i
就地保存而不是打印到stdout
sed -i 's/fields/fields\nNew Inserted Line/'
sed -i 's/fields/fields\nNew Inserted Line/'
As a bash script:
作为 bash 脚本:
#!/bin/bash
match='fields'
insert='New Inserted Line'
file='file.txt'
sed -i "s/$match/$match\n$insert/" $file
回答by Mark Reed
This is definitely a case where you want to use something like sed
(or awk
or perl
) rather than readling one line at a time in a shell loop. This is not the sort of thing the shell does well or efficiently.
在这种情况下,您绝对想使用sed
(or awk
or perl
) 之类的东西,而不是在 shell 循环中一次读取一行。这不是 shell 做得好的或有效的事情。
You might find it handy to write a reusable function. Here's a simple one, though it won't work on fully-arbitrary text (slashes or regular expression metacharacters will confuse things):
您可能会发现编写可重用的函数很方便。这是一个简单的,尽管它不适用于完全任意的文本(斜线或正则表达式元字符会混淆):
function insertAfter # file line newText
{
local file="" line="" newText=""
sed -i -e "/^$line$/a"$'\\n'"$newText"$'\n' "$file"
}
Example:
例子:
$ cat foo.txt
Now is the time for all good men to come to the aid of their party.
The quick brown fox jumps over a lazy dog.
$ insertAfter foo.txt \
"Now is the time for all good men to come to the aid of their party." \
"The previous line is missing 'bjkquvxz.'"
$ cat foo.txt
Now is the time for all good men to come to the aid of their party.
The previous line is missing 'bjkquvxz.'
The quick brown fox jumps over a lazy dog.
$
回答by s3n0
Or anoter one example with the sed
:
或者另一个例子sed
:
echo -e "line 1\nline 2\nline 3\nline 4" > /tmp/test.py
cat /tmp/test.py
line 1
line 2
line 3
line 4
sed -i '2 a line 2.5' /tmp/test.py # sed for inside of the file 'LINE_NUMBER append TEXT'
cat /tmp/test.py
line 1
line 2
line 2.5
line 3
line 4
回答by Oz123
sed is your friend:
sed 是你的朋友:
:~$ cat text.txt
foo
bar
baz
~$
~$ sed '/^bar/\na this is the new line/' text.txt > new_text.txt
~$ cat new_text.txt
foo
bar
this is the new line
baz
~$