Multi line replacement with Sed - sed

I have a file like this:
function a() {
doSomething();
doSomethingElse();
}
Now I need to replace all text between function a() { and } with some other text
I tried several ways found here, but they all failed. I hope I could get the explanation along with an answer.
P.S. the trick need to be compatible with both OS X and GNU Sed.

You can do a standard substitution :
sed -i 's/\(\<\)doSomething()/\1somethingElse()/' your_file
We use the word boundary to delimit your string :
\(\<\)doSomething()
And then we replace that string with your new string (including the captured word boundary) :
\1somethingElse()
The -i flag stands for : inline replacement
I used this sample file :
function a() {
doSomething();
doSomething();
foo();doSomething();
}
Output :
function a() {
somethingElse();
somethingElse();
foo();somethingElse();
}

sed '/^function a() {/,/}/{
/^[[:blank:]]*doSomething();/ c\
Replace with\
whaterver you want \
where new line are backslash \
at the end for multiline.
}' YourFile
replace the line of doSomething inside function a() paragraph by some other line(s)
Highly depend on content of function a() (assuming there is no ending } line inside the function)

Related

Eclipse formatter: How to move comment between method name and open brace to always have open brace on same line as method declaration

I have code that looks like this, that I'm trying to format
Original code:
public int doThing(int a) // -incredibly useful comment here
{
int ab = a+1;
return ab;
}
I want it to look like this
public int doThing() { // -incredibly useful comment here
int ab = a+1;
return ab;
}
If I try to turn on the Brace position -> Method Declaration -> Same line option and run the formatter, any code with a comment in the position "breaks" the formatter, and I get an output for my example that looks the same as the original code, but methods without a comment have the correct formatting (meaning the results are inconsistent).
Is it possible with the eclipse formatter to get the style I want? I'm trying to run it against a large amount of code, and would prefer not to have to fix these all manually to get a consistent brace position.
The problem here is that is not formatting but rewriting. Using File Search + regular expression + Replace could do that in bulk.
Try this regex
^(\s*(?:public|private|protected)\s+[^(]+\([^)]*\))(\s*\/\/[^/]+)\R\s*\{
On File Search ( Ctrl + H)
Hit Replace and use $1 { $2\n as replacement
Code should compile after the refactoring.
UPDATE:
Fixed regex part that represents function arguments
\([^)]*\)
Full Regex matches these cases
public int doSmthg() // coment here
{
return 1;
}
private String doSmthgElse(String arg) // coment here
{
return arg;
}

Multiple line powershell command in JobDSL

I'm tinkering with JobDSL and can't seem to find a way to run several powershell commands in one go. Example:
job('whatever'){
steps{
powershell("""$var = $env:mybuildvar
cmdlet2 $var""")
}
}
How do I achieve this? Thanks!
it seems that """ """ works for batch, but not for powershell.
Also, if I try to use $var with escaping or without JobDSL fails with
ERROR: (sandbox_CI_Dev, line 15) No signature of method:
javaposse.jobdsl.dsl.helpers.step.StepContext.powershell() is
applicable for argument types: (java.lang.String) values: [$var =
$ENV:mybuildvar]
The error is reproducable on jobdsl playground (http://job-dsl.herokuapp.com/), use following code (or anything similar to code above):
job('whatever') {
steps{
powershell("write-output $")
}
}
also powershell('write-output test; write-output test') doesn't work
The method name is powerShell, not powershell. See https://jenkinsci.github.io/job-dsl-plugin/#path/job-steps-powerShell.
And Groovy interpolates double quoted strings, see String interpolation. You need to use single quotes to avoid the interpolation if you want to use the dollar sign ($), e.g. '$var'. Use triple single quotes for multiline strings.
job('whatever'){
steps{
powerShell('''$var = $env:mybuildvar
cmdlet2 $var''')
}
}

sed pattern replace with new line

I'm trying to replace with insert in all files in the directory with the following command:
find . -type f -exec sed -i.bak ':begin;$!N;s/\(#Autowired\)\n\(public .*\)\((ServletRequest\)/\2() \{\}\n&/;tbegin' {} \;
Here is what I'm trying to do:
Match:
#Autowired
public something(ServletRequest
Replace With:
public something() {}
#Autowired
public something(ServletRequest
I am basically trying to add a default constructor to all my java classes in a certain directory/package. I can't seem to match the newline
This seems to work:
sed '/#Autowired/{:l
N;s/\(.*public[ ]*\)\([^(]*\)\((ServletRequest\)/public \2() {}\n\1\3/;
/ServletRequest/!bl}' input
and if you have the ServletRequest bit always following the #autowired:
sed '/#Autowired/{
N;s/\(.*public[ ]*\)\([^(]*\)\((ServletRequest\)/public \2() { }\n\1\3/}'
input

Matching $END$ in perl

When i use the line :
if (m/^$END$/g) {
# ...
}
in my code, the compiler thinks that I am searching for a Static'END$' in my code,
whereas I want to search the string "$END$". How shall I go about it?
To match a literal $, just escape it with a backslash:
if (m/^\$END\$/) { ... }
Removed the /g that shouldn't be there.
if (/\A\Q $END$ /x) { ... }
perldoc perlreref:
\Q Disable pattern metacharacters until \E
Removed the /g that shouldn't be there.

Is it possible with Eclipse to only find results in string literals?

Lets assume I have the following Java code:
public String foo()
{
// returns foo()
String log = "foo() : Logging something!"
return log;
}
Can I search in Eclipse for foo() occurring only in a String literal, but not anywhere else in the code? So in the example here Eclipse should only find the third occurrance of foo(), not the first one, which is a function name and not the second one, which is a comment.
Edit: Simple Regular Expressions won't work, because they will find foo() in a line like
String temp = "literal" + foo() + "another literal"
But here foo() is a function name and not a String literal.
You can try it like this:
"[^"\n]*foo\\(\\)[^"\n]*"
You have to escape brackets, plus this regex do not match new lines or additional quotes, which prevent wrong matches.
Maybe you should use regex to find any occurence of foo() between two " ?