I have multiple source codes in which braces are like this
function()
{
if(...)
{
...
}
else
{
...
}
}
I would like to make it look like this:
function() {
if(...) {
...
}
else {
...
}
}
I've tried some tricks with the sed command, but I can't figure out how to get it working properly. Here is my latest try:
sed ":a; N; $!ba; s/\n{/ {/g" myfile
EDIT -
I managed to have this working with that command:
sed "N;/\n *{/s// {/;P;D"
As usual with sed, I quite don't understand why it works, but it does.
Since you're performing code formatting, can I suggest you use a tool more geared to this, and investigate something like AStyle. Here's the info for your specific issue (brace positioning)
Related
Lets say I have a code like this:
return {
{
Str = function (elem)
if elem.text == "any string" then
return pandoc.RawInline ("latex", "anything")
end
end,
}
}
Can i include a if statement that says:
If the output file is latex then use this code and if the output file is html (or not latex) do that...
So I essentially wonder if there is a way of figuring out the output file format within the lua-filter.
Yes, this is possible. Check out this pandoc lua filters example: https://pandoc.org/lua-filters.html#center-images-in-latex-and-html-output specifically these lines:
if FORMAT:match 'latex' then
...
if FORMAT:match 'html' then
I have a jsp page and a fucntion in it.
function foo() {
if(confirm("bar") {
// do something
}
I want to edit the exsiting or add a new if condtion to the jsp page with a shell script to make it like below
function foo() {
if(confirm("new text"+bar) {
//do something
}
I'm trying to use awk like this to a new if condition when one doesnt exist already.
awk '/foo/{print;print "if(confirm(Hey) {";next}1' myjsp.jsp
awk '/foo2/{print "}";}1' myjsp.jsp //foo2 is fucntion after foo. using this add closing }
The problem is I'm seeing a duplicate lines printing I guess because option 1 print out everything after current line. How do I stop this.
Try this simple method
sed -r '/foo()/{n;/if\(confirm/s/([^"]+)"(.*)".*/\1"new text"+\2)\{/}' fileName
OutPut:
function foo() {
if(confirm("new text"+bar){
// do something
}
I'm not really sure a good name for this question so please rename it if you can think of a better one.
In Bash I have a function that I am using to store certain functions. Consider:
menv_funcs=()
function menv_function {
menv_funcs+=($1)
}
I am then using it in this manner:
menv_function fetch
function fetch {
...
}
I would like to use it like this though:
menv_function fetch {
...
}
Essentially I'm looking for something like the preprocessor macros in C would do but I have been unable to find a way. Any ideas?
As far as I'm aware, you can't directly achieve this. However, I can think of two solutions that may be of interest to you.
First of all, you could just declare the functions as usual, and then obtain the list of declared functions through declare -F. This could be done like:
function fetch {
:
}
menv_funcs=()
while IFS=$"\n" read l; do
menv_funcs+=${l#declare -f }
done < <(declare -F)
Which will cause menv_funcs[#] to list all the functions declared at the point of calling the snippet. Of course, this may catch unwanted functions as well.
To avoid this, you may add some prefix to function names and filter the list:
function menv_fetch {
:
}
menv_funcs=()
while IFS=$"\n" read l; do
if [[ ${l} == 'declare -f menv_'* ]]; then
menv_funcs+=${l#declare -f menv_}
fi
done < <(declare -F)
And if you really want to achieve something like macros, you may try to play with eval:
menv_funcs=()
function menv_function {
local name=${1}
local body=${2}
menv_funcs+=( ${name} )
eval "function ${name} ${body}"
}
menv_function fetch "{
:
}"
But note that you will actually need to quote the whole function body and escape everything appropriately.
I would like to copy some files in Gradle and the resulting files should not contain any blank lines, i.e., the blank lines are not copied. I assume that can be done with filter(...) and maybe with the TokenFilter from ant. However, I am not sure how to the syntax would look like.
Thanks.
Gradle uses Ant for filtering, because of its powerful implementation. For example, you can use the LineContainsRegExp Ant filter to filter out any line that is only empty or whitespaces.
The appropriate regexp can be [^ \n\t\r]+
You can use Ant directly from Gradle like this:
task copyTheAntWay {
ant.copy(file:'input.txt', tofile:'output.txt', overwrite:true) {
filterchain {
filterreader(classname:'org.apache.tools.ant.filters.LineContainsRegExp') {
param(type:'regexp', value:'[^ \n\t\r]+')
}
}
}
}
or by using the Gradle CopySpec's filter method:
task copyGradlefied(type:Copy) {
def regexp = new org.apache.tools.ant.types.RegularExpression()
regexp.pattern = '[^ \n\t\r]+'
from(projectDir) {
include 'input.txt'
filter(org.apache.tools.ant.filters.LineContainsRegExp, regexps:[regexp])
}
into "outputDir"
}
Let's say I've got some text with a couple tags like this:
[twitter:jpunt]
I want to replace those into something like this:
#Jpunt
How could I do this in Ruby? I've been researching regular expressions for a couple of hours, just with a lot of frustration as a result. Anyone?
This should do the job:
initial = "[twitter:jpunt]"
link = initial.gsub(/\[twitter:(\w+)\]/i, '#\1')
It is one line code (click here to test this code) >>
output = input.gsub(/\[([^:]+):([^\]]+)\]/) {
'#' + $2.capitalize + '' }
The above code works with any tag name. If you want just twitter to be allowed, then go with modification:
output = input.gsub(/\[twitter:([^\]]+)\]/) {
'#' + $1.capitalize + '' }