Add text between two patterns in File using sed command
Since /r
stands for reading in a file, use:
sed '/First/r file1.txt' infile.txt
You can find some info here: Reading in a file with the 'r' command.
Add -i
(that is, sed -i '/First/r file1.txt' infile.txt
) for in-place edition.
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 '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 -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
SED: insert a word/string between two patterns in the SAME LINE
Use capture groups.
sed -e 's/(pattern1)(pattern2)/\1new_word\2/'
\1
is replaced with whatever matched the first pattern, \2
gets whatever matched the second pattern.
Output text between two patterns on the same line using sed command
For your current input you may use this sed
:
sed 's/.*begin \(.*\) end.*/\1/' file
not sure what is wrong
Difference is use of .*
after end
that matches text after last end
and discards in substitution.
However for your 2nd part if there are two end
words, sed
command won't work correctly as it will find last end
due to greedy matching of .*
.
e.g if your input is:
this is begin not sure what is wrong end and why not end
Then following awk
would work better:
awk -F 'begin | end' '{print $2}' file
not sure what is wrong
Sed - Insert line with text after match pattern between two strings
Data set:
$ cat test.txt
text text
text text
[textabc pattern 1]
text text
text text
xyz = 123
text text
[textdef pattern 2]
text text
text text
A couple small changes to OPs current sed
command:
# current
sed '/^\[textabc pattern 1\]$/,/^\[textdef pattern 2\]/ ^xyz .*/a NEW STRING/' test.txt
# new/proposed (2 lines); the 'a'ppend option requires a new line before the end '}'
sed -e '/^\[textabc pattern 1\]$/,/^\[textdef pattern 2\]/{/^xyz .*/aNEW STRING
}' test.txt
# new/proposed (1 line); break into 2 segments via a 2nd '-e' flag to eliminate need for embedded newline character
sed -e '/^\[textabc pattern 1\]$/,/^\[textdef pattern 2\]/{/^xyz .*/a'"NEW STRING" -e '}' test.txt
Both of the above new/proposed sed
commands generate the following:
text text
text text
[textabc pattern 1]
text text
text text
xyz = 123
NEW STRING
text text
[textdef pattern 2]
text text
text text
NOTE: Once OP is satisfied with the results the -i
flag can be reintroduced to allow sed
to make in-place changes to data file.
write each block of text between two markers to a file with sed
This might work for you (GNU sed):
sed '/START/,/END/!d' file | csplit -z - '/END/1' '{*}'
Use sed to gather up all lines between start and end separators, then apply csplit to create separate files for each group of lines.
how to print Lines Between Two Patterns in file using SED or AWK?
You may use this sed
:
sed -n '/MULTIPLE-RESOURCES/,/^###$/ { /###$/!p; }' file
### MULTIPLE-RESOURCES
#### Viewing Resource Information
> kubectl get svc, po
> kubectl get deploy, no
> kubectl get all
> kubectl get all --all-namespaces
## KUBECTL
How can I use sed to append lines between two patterns?
Could you please try following.
awk '/^state/{val=$NF;next} val{print $0","val}' Input_file
Explanation: Adding explanation too now.
awk '
/^state/{ ##Check condition if a line starts from string state then do following.
val=$NF; ##Creating variable val whose value is last field of current line.
next} ##Using next keyword to skip all further statements.
val{ ##Checking condition if variable val is NOT NULL then do following.
print $0","val ##Printing current line with command and variable val here.
}' Input_file ##Mentioning Input_file name here.
How to use sed/grep to extract text between two words?
sed -e 's/Here\(.*\)String/\1/'
Replace text between two lines with contents of a file stored in a variable in sed
This might work for you (GNU sed):
sed '/blue/,/gray/!b;//!d;/blue/r file2' file1
For the range of lines between blue
and gray
, delete lines that do not match either the first or the last lines in the range and read the lines to insert before the last line of the range.
EDIT:
The first sed command /blue/,/grey/!b
matches a range of lines i.e. the lines that range between a line containing blue
upto and including a line containing gray
. The !b
part means if the lines are not in that range then break out of the sed commands i.e. do not do any further sed processing for these lines, just print as normal.
The sed commands following will only affect those lines that are in the range between blue
and gray
.
The second command //!d
means: delete those lines that do not match either the start/end of the range i.e. blue
or gray
. The //
uses the regexp from a previous /.../
command. N.B. a delete command terminates any further sed processing for that line.
The sed commands following will only affect lines that containing either blue
or gray
.
The third command matches a line containing blue
and reads in lines from file2.
N.B. the lines containing blue
and grey
are processed by sed naturally and printed before the next line is read into the pattern space as are the lines not between blue
and gray
.
An alternative:
sed '/blue/,/gray/!b;//!d;/gray/e cat file2' file1
And another:
sed -ne '/blue/{p;r file2' -e ':a;n;/gray/!ba};p' file1
Related Topics
Using Grep to Search for a String That Has a Dot in It
Control Mouse by Writing to /Dev/Input/Mice
Redirect All Output to File Using Bash on Linux
Convert All File Extensions to Lower-Case
Colors with Unix Command "Watch"
How to Find Out What Group a Given User Has
Postgresql Database Default Location on Linux
How to Extract Characters Between the Delimiters Using Sed
How to Create a File in Linux from Terminal Window
Attach to a Processes Output for Viewing
Fastest Way to Tell If Two Files Have the Same Contents in Unix/Linux
Linux's Thread Local Storage Implementation
Which Signal Does Ctrl-X Send When Used in a Terminal
Download Images from Google with Command Line
How to Add Date String to Each Line of a Continuously Written Log File