Adding extra lines to document using awk - bash

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
}

Related

Is there a way to figure out the output format in a pandoc lua filter

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

How to use a function as a string while using matching pattern

I want to make use of functions to get the full path and directory name of a script.
For this I made two functions :
function _jb-get-script-path ()
{
#returns full path to current working directory
# + path to the script + name of the script file
return $PWD/${0#./*}
}
function _jb-get-script-dirname ()
{
return ${(_jb-get-script-path)##*/}
}
as $(_jb-get-script-path) should be replaced by the result of the function called.
However, I get an error: ${(_jb-get-script-path)##*/}: bad substitution
therefore i tried another way :
function _jb-get-script-path ()
{
return $PWD/${0#./*}
}
function _jb-get-script-dirname ()
{
local temp=$(_jb-get-script-path);
return ${temp##*/}
}
but in this case, the first functions causes an error : numeric argument required. I tried to run local temp=$(_jb-get-script-path $0) in case the $0 wasn't provided through function call (or i don't really know why) but it didn't change anything
I don't want to copy the content of the second fonction as i don't want to replicate code for no good reason.
If you know why those errors happen, I really would like to know why, and of course, if you have a better solution, i'd gladely hear it. But I'm really interessed in the resolution of this problem.
You need to use echo instead of return which is used for returning a numeric status:
_jb-get-script-path() {
#returns full path to current working directory
# + path to the script + name of the script file
echo "$PWD/${0#./*}"
}
_jb-get-script-dirname() {
local p="$(_jb-get-script-path)"
echo "${p##*/}"
}
_jb-get-script-dirname

change braces position with sed

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)

C like preprocessor macros in Bash

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.

Bash alias name from a variable

Is there a way to make a bash alias (or function) with its name coming from a variable?
For instance, is it possible to do something along these lines:
create_alias_with_name() {
alias $1="echo a custom alias"
}
Or something along these lines:
create_func_with_name() {
$1() {
"echo inside a function with a variable name"
}
}
In other words, I would prefer to have some kind of function "factory" that can register functions for me. Is this possible or beyond the capabilities of Bash?
Did you even try it? Your first example works fine.
You can make the second work by adding an eval:
create_func_with_name() {
eval "$1() {
echo inside a function with a variable name
}"
}
just in case, one may use a variable both as a part of the alias name and as a part of the alias command:
alias foo${var1}="bar${var2}"

Resources