I want to add Some large code between two patterns:
我想在两个模式之间添加一些大代码:
File1.txt
This is text to be inserted into the File.
infile.txt
Some Text here
First
Second
Some Text here
I want to add File1.txt content between First and Second :
我想在First和Second之间添加File1.txt内容:
Desired Output:
Some Text here
First
This is text to be inserted into the File.
Second
Some Text here
I can search using two patterns with sed command ,But I don't have idea how do I add content between them.
我可以使用sed命令使用两种模式进行搜索,但我不知道如何在它们之间添加内容。
sed '/First/,/Second/!d' infile
4 个解决方案
#1
7
Since /r
stands for reading in a file, use:
由于/ r代表读取文件,请使用:
sed '/First/r file1.txt' infile.txt
You can find some info here: Reading in a file with the 'r' command.
您可以在这里找到一些信息:使用'r'命令读取文件。
Add -i
(that is, sed -i '/First/r file1.txt' infile.txt
) for in-place edition.
为就地版添加-i(即sed -i'/ First / r file1.txt'infile.txt)。
To perform this action no matter the case of the characters, use the I
mark as suggested in Use sed with ignore case while adding text before some pattern:
要执行此操作,无论字符是什么情况,请在使用sed with ignore case时建议使用I标记,同时在某些模式之前添加文本:
sed 's/first/last/Ig' file
As indicated in comments, the above solution is just printing a given string after a pattern, without taking into consideration the second pattern.
如评论中所示,上述解决方案仅仅是在模式之后打印给定的字符串,而不考虑第二模式。
To do so, I'd go for an awk with a flag:
要这样做,我会去拿一个带旗帜的awk:
awk -v data="$(<patt_file)" '/First/ {f=1} /Second/ && f {print data; f=0}1' file
Given these files:
鉴于这些文件:
$ cat patt_file
This is text to be inserted
$ cat file
Some Text here
First
First
Second
Some Text here
First
Bar
Let's run the command:
让我们运行命令:
$ awk -v data="$(<patt_file)" '/First/ {f=1} /Second/ && f {print data; f=0}1' file
Some Text here
First # <--- no line appended here
First
This is text to be inserted # <--- line appended here
Second
Some Text here
First # <--- no line appended here
Bar
#2
1
i think you can try this
我想你可以尝试一下
$ sed -n 'H;${x;s/Second.*\n/This is text to be inserted into the File\
&/;p;}' infile.txt
#3
1
awk
flavor:
awk '/First/ { print $0; getline < "File1.txt" }1' File2.txt
#4
0
Here's a cut of bash code that I wrote to insert a pattern from patt_file. Essentially had had to delete some repetitious data using uniq then add some stuff back in. I copy the stuff I need to put back in using lineNum values, save it to past_file. Then match patMatch in the file I'm adding the stuff to.
这是我编写的用于从patt_file插入模式的bash代码的一部分。基本上不得不使用uniq删除一些重复数据然后添加一些东西。我复制我需要放回使用lineNum值的东西,将其保存到past_file。然后在我正在添加内容的文件中匹配patMatch。
#This pulls the line number from row k, column 2 of the reduced repitious file
lineNum1=$(awk -v i=$k -v j=2 'FNR == i {print $j}' test.txt)
#This pulls the line number from row k + 1, coulmn 2 of the reduced repitious file
lineNum2=$(awk -v i=$((k+1)) -v j=2 'FNR == i {print $j}' test.txt)
#This pulls fields row 4, 2 and 3 column into with tab spacing (important) from reduced repitious file
awk -v i=$k -v j=2 -v h=3 'FNR == i {print $j" "$h}' test.txt>closeJ.txt
#This substitutes all of the periods (dots) for \. so that sed will match them
patMatch=$(sed 's/\./\\./' closeJ.txt)
#This Selects text in the full data file between lineNum1 and lineNum2 and copies it to a file
awk -v awkVar1=$((lineNum1 +1)) -v awkVar2=$((lineNum2 -1)) 'NR >= awkVar1 && NR <= awkVar2 { print }' nice.txt >patt_file.txt
#This inserts the contents of the pattern matched file into the reduced repitious file
#The reduced repitious file will now grow
sed -i.bak "/$patMatch/ r "patt_file.txt"" test.txt
#1
7
Since /r
stands for reading in a file, use:
由于/ r代表读取文件,请使用:
sed '/First/r file1.txt' infile.txt
You can find some info here: Reading in a file with the 'r' command.
您可以在这里找到一些信息:使用'r'命令读取文件。
Add -i
(that is, sed -i '/First/r file1.txt' infile.txt
) for in-place edition.
为就地版添加-i(即sed -i'/ First / r file1.txt'infile.txt)。
To perform this action no matter the case of the characters, use the I
mark as suggested in Use sed with ignore case while adding text before some pattern:
要执行此操作,无论字符是什么情况,请在使用sed with ignore case时建议使用I标记,同时在某些模式之前添加文本:
sed 's/first/last/Ig' file
As indicated in comments, the above solution is just printing a given string after a pattern, without taking into consideration the second pattern.
如评论中所示,上述解决方案仅仅是在模式之后打印给定的字符串,而不考虑第二模式。
To do so, I'd go for an awk with a flag:
要这样做,我会去拿一个带旗帜的awk:
awk -v data="$(<patt_file)" '/First/ {f=1} /Second/ && f {print data; f=0}1' file
Given these files:
鉴于这些文件:
$ cat patt_file
This is text to be inserted
$ cat file
Some Text here
First
First
Second
Some Text here
First
Bar
Let's run the command:
让我们运行命令:
$ awk -v data="$(<patt_file)" '/First/ {f=1} /Second/ && f {print data; f=0}1' file
Some Text here
First # <--- no line appended here
First
This is text to be inserted # <--- line appended here
Second
Some Text here
First # <--- no line appended here
Bar
#2
1
i think you can try this
我想你可以尝试一下
$ sed -n 'H;${x;s/Second.*\n/This is text to be inserted into the File\
&/;p;}' infile.txt
#3
1
awk
flavor:
awk '/First/ { print $0; getline < "File1.txt" }1' File2.txt
#4
0
Here's a cut of bash code that I wrote to insert a pattern from patt_file. Essentially had had to delete some repetitious data using uniq then add some stuff back in. I copy the stuff I need to put back in using lineNum values, save it to past_file. Then match patMatch in the file I'm adding the stuff to.
这是我编写的用于从patt_file插入模式的bash代码的一部分。基本上不得不使用uniq删除一些重复数据然后添加一些东西。我复制我需要放回使用lineNum值的东西,将其保存到past_file。然后在我正在添加内容的文件中匹配patMatch。
#This pulls the line number from row k, column 2 of the reduced repitious file
lineNum1=$(awk -v i=$k -v j=2 'FNR == i {print $j}' test.txt)
#This pulls the line number from row k + 1, coulmn 2 of the reduced repitious file
lineNum2=$(awk -v i=$((k+1)) -v j=2 'FNR == i {print $j}' test.txt)
#This pulls fields row 4, 2 and 3 column into with tab spacing (important) from reduced repitious file
awk -v i=$k -v j=2 -v h=3 'FNR == i {print $j" "$h}' test.txt>closeJ.txt
#This substitutes all of the periods (dots) for \. so that sed will match them
patMatch=$(sed 's/\./\\./' closeJ.txt)
#This Selects text in the full data file between lineNum1 and lineNum2 and copies it to a file
awk -v awkVar1=$((lineNum1 +1)) -v awkVar2=$((lineNum2 -1)) 'NR >= awkVar1 && NR <= awkVar2 { print }' nice.txt >patt_file.txt
#This inserts the contents of the pattern matched file into the reduced repitious file
#The reduced repitious file will now grow
sed -i.bak "/$patMatch/ r "patt_file.txt"" test.txt