1

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 
  • How many empty lines between // copyright and package ... ? 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... – don_crissti Apr 16 '15 at 17:18
1

Using sed

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

Using awk

$ awk '/^\/\/ copyright/,/^package/{if (/^package/) print "Something\nElse"; next} 1' file
Something
Else

import com.base
import com.base
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.