Reputation: 99
My output file is as below:
judi#cat file
---ABC---
word1
word2
word3
word4
word5
word6
---end_ABC---
---DEF---
line1
line2
line3
line4
---end_DEF---
judi#
I need to remove the lines in between the pattern ABC
and end_ABC
(inclusive the pattern, then replace with new content; the new content is in a file).
The content of the file varies, so I need to use only the pattern.
judi#file1
---ABC---
wordA1
wordA2
wordA3
---end_ABC---
judi#
Desired result has to be
judi#
---ABC---
wordA1
wordA2
wordA3
---end_ABC---
---DEF---
line1
line2
line3
line4
---end_DEF---
judi#
I tried this command:
sed '/ABC/,/end_ABC/{/ABC/!{/end_ABC/!d}}' file > file 2
But I get this error:
sed: command garbled: /ABC/,/end_ABC/{/ABC/!{/end_ABC/!d}}
Upvotes: 0
Views: 1920
Reputation: 1726
sed '/end_ABC/a ##here' file | sed '/ABC/,/end_ABC/d' | sed '/##here/r file1' | sed '/##here/d' >file2
output
judi#cat file
judi#file1
---ABC---
wordA1
wordA2
wordA3
---end_ABC---
judi#
---DEF---
line1
line2
line3
line4
---end_DEF---
judi#
a ##here
is appending ##here
after matching end_ABC
.
r file1
is inserting text from file1 after finding pattern ##here
.
Upvotes: 1
Reputation: 204498
Never use range expressions as they make trivial tasks very slightly briefer but even slightly more complicated tasks need a complete rewrite or duplicate conditions. Just use a flag:
awk '
NR==FNR { rep = rep $0 OFS; next }
/---ABC---/ { printf "%s", rep; inBlock=1 }
!inBlock
/---end_ABC---/ { inBlock=0 }
' file1 file
Upvotes: 2