for eg. input :
// copyright
package com.base
import com.base
import com.base
...
i want to replace the pattern "// copyright\n\n^package.*" with another string
i'm trying to do using
sed -e 's/.,^package/$(anotherString)/g' $text
Here is a sed solution:
$ sed '\|// copyright|,\|^package|{s/^package/Something\nElse/p;d}' file
Something
Else com.base
import com.base
import com.base
...
Did you want to remove all of the original package line? If so, just a minor change is needed:
$ sed '\|// copyright|,\|^package|{s/^package.*/Something\nElse/p;d}' file
Something
Else
import com.base
import com.base
...
$ awk '/^\/\/ copyright/,/^package/{if (/^package/) print "Something\nElse"; next} 1' file
Something
Else
import com.base
import com.base
...
"\|// copyright|,\|^pack ...": extra characters at the end of d command error using the first sed command (bash on macOS) - any ideas what's wrong?
sed '\|// copyright|,\|^package|{s/^package/Something\nElse/p;d;}' file
// copyrightandpackage ...? One or two ? Your example has one empty line in between but you're saying you want to replace// copyright\n\npackage.*which means two empty lines in between...