mles
mles

Reputation: 3476

Remove a specific function from a file with sed or awk

Due to an automatic code scan finding I have to remove the function for self signed certificates from https://github.com/apache/cordova-plugin-file-transfer/blob/master/src/ios/CDVFileTransfer.m, lines 838 through 850. So far I have come up to remove these lines by line number

sed -i '' -e '838,850d' CDVFileTransfer.m

which of course is dangerous if any of the code before changes.

I'm looking for a sed expression that removes everything from the line(Line 838) beginning with

// for self signed certificates

to the first line(Line 850) that only has a closing bracket

}

So something like

sed -i '' -e '^"// for self signed certificates",^"}"d' CDVFileTransfer.m

Is that possible?

I've tried my luck with regular expressions:

sed -i '' -e '#"// for self signed certificates"*^{#' CDVFileTransfer.m

but it doesn't match anything.

Upvotes: 0

Views: 187

Answers (1)

William Pursell
William Pursell

Reputation: 212414

sed -e '/^\/\/ for self signed certificates/,/^}$/d'

or

sed -e '\@^// for self signed certificates@,/^}$/d' input

I'm omitting the -i for for clarity, and because it is not available with all versions of sed. Also, sed is a stream editor. If you want to edit a file, you should use ed which is the editor for files upon which sed is based. Personal opinion, -i is a terrible idea: if you want to overwrite a file, use shell redirections or write a wrapper function.

Upvotes: 1

Related Questions