【每日一题0617】 Linux 不定行前插入字符串

新建一个vi test.txt文件,内容如下:

1 this is a test1
2 this is a test2
3 this is a test3
4 this is a test4

假设 test.txt文件有很多行,无法知道3 this is a test3 所在行,
请在3 this is a test3 之前插入hello world 
输出结果如下:

1 this is a test1
2 this is a test2
hello world
3 this is a test3
4 this is a test4


cat/less/more/tail/head test.txt | sed ‘2a/3i hello world’

sed ‘2a/3i hello world’ test.txt

cat test.txt | sed '3i hello world'
sed -e '3i hello word' test.txt
cat test.txt | sed  -e '/test3/i hello world'

因为题目中写了有很多行无法知道目标字符串在哪一行,所以我理解的是,应该匹配字符串再插入更合适些吧

> sed  -i '/3 this is a test3/i hello world' test.txt
> cat test.txt

1 this is a test1
2 this is a test2
hello world
3 this is a test3
4 this is a test4