0

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}} 
3
  • 1
    Your command works for me under GNU sed. If you are using BSD (OSX) sed, consider adding some semicolons before the closing braces: } --> ;}. Commented Jun 13, 2015 at 21:05
  • yes command looks nice (GNU sed), only file+2, that command will delete all lines in range excluding first and last Commented Jun 13, 2015 at 21:13
  • in the future, please learn to search S.O. before posting. Your headline 'remove lines between two pattern' (with an 's' on pattern) returns 76 items. Good luck. Commented Jun 14, 2015 at 14:19

2 Answers 2

2

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 
Sign up to request clarification or add additional context in comments.

Comments

1
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.

2 Comments

I think I am using some other sed not GUN sed as you mentioned .. getting the below error "sed: command garbled: /end_ABC/a ##here" how do I check which sed is am using ?
sed --version +link

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.