I have an input file (PowerCenter xml file) and I need to replace a shortcut for a target environment. So I need to find a line with SHORTCUT and then change a parameter in REPOSITORYNAME
I am trying to find the substring from REPOSITORYNAME to eol and replace the first occurrence of "whatever" between quotes. I have found a regex ".*?" which works in regexr.com, but not in bash
input=SHORTCUT COMMENTS="" REFERENCETYPE="LOCAL" REFOBJECTNAME="mplt_EBXSOAPExport" REPOSITORYNAME="ZRH_PCE_P01" VERSIONNUMBER="1"/>"
x=$(echo $input | grep -o 'REPOSITORYNAME.*>' | sed -r '{0,/\".\+?\"/s/\".\+?\"/\"TARGET\"/}')
echo $x;
input="SHORTCUT COMMENTS="" REFERENCETYPE="LOCAL" REFOBJECTNAME="mplt_EBXSOAPExport" REPOSITORYNAME="any_word" VERSIONNUMBER="1"/>"
another possible input="SHORTCUT COMMENTS="" REFERENCETYPE="LOCAL" REFOBJECTNAME="mplt_EBXSOAPExport" REPOSITORYNAME = "any_word" VERSIONNUMBER="1"/>"
output="SHORTCUT COMMENTS="" REFERENCETYPE="LOCAL" REFOBJECTNAME="mplt_EBXSOAPExport" REPOSITORYNAME="updated" VERSIONNUMBER="1"/>"
Thanks
You can use the following :
sed '/SHORTCUT/s/REPOSITORYNAME\s*=\s*"[^"]*"/REPOSITORYNAME="WhatYouWant"/'
It tests that a line contains SHORTCUT and when it does it performs a search/replace matching the REPOSITORYNAME and its value and replacing the latter by a new one.
You can try it here !
Related
I'm trying to add a line in a file afile.xyz using my script. This is what I've done so far using sed:
n="$(grep ".method" "$m" | grep "onCreate(Landroid/os/Bundle;)V")"
sed -i '' -e '/$n/ a\
"test", /Users/username/Documents/afile.xyz
I'm getting the error:
"onCreate\(\Landroid\/ ...": bad flag in substitute command: 'g'
How do I solve this? Please do help. Thanks.
Edit: Content of n
method protected onCreate(Landroid/os/Bundle;)V
2 problems:
because the sed body is in single quotes, the variable $n will not be expanded,
the regular expression in $n contains the / dilimiters.
Try this:
n=$(...)
nn=${n//\//\\/} # escape all slashes
sed -i '' '/'"${nn}"'/ a ...
The single-quoted sed body is interrupted to append the double quoted shell variable.
You can also use a different delimiter for the RE:
sed -i '' -e "\#$n# a\\
\"test\"" /Users/username/Documents/afile.xyz
I have a sed expression in a function that I pass parameters to.
insert_after_new_line() {
if ! grep -q "${2}" "${3}"; then
sed -i "/${1}/{N;N;a ${2}}" "${3}"
fi
}
insert_after_new_line search insert textfile.txt
I am trying to have a blank line inserted below the search string and the insert string inserted after.
so
text
text
search
text
becomes
text
text
search
insert
text
but I keep getting the error
sed: -e expression #1, char 0: unmatched `{'
I tested this. works in command line
sed -i '/search/a \\ninsert' file
sed really delimiters commands by a newline. There is a ; but it does not work for all commands, mostly these which take file name as an argument. ; does not work for r R or for example a. Sed will read everything after a command, so sed interprets is like a ${2}} as a single command, in result it does not find enclosing }, cause it's been eaten by a command. You need a newline:
sed -i "/${1}/{N;N;a ${2}
}" "${3}"
or
sed -i "/${1}/{N;N;a ${2}"$'\n'"}" "${3}"
this should work:
sed -i '/search/{G;ainsert
}' file
You can replace the text by shell variable, but replace the single quotes by double quotes too.
what I want to achieve is to find an string in file and go on finding first occurrence of another match and replace that with some value.
Ex: string = {Name: name;Address:someadd;var1:var1;var2:var2},{Name: differntName;Address:someadd;var1:var1;var2:var2}
Now what i need to do is to find Name: name and then find first occurrence of "var2:var2" after "Name: name" and replace it with "var2:newvarvalue"
Please note that i need to complete this task with sed in bash scripting.
Thanks in advance.
Edit : i am trying to modify .yaml docker compose file
Here is a terrible solution using sed :
split on ,, one part per line :
sed 's/,/\n/g'
replace var2:var2 by var2:newvarvalue if on the same line as Name: name :
sed '/Name: name/s/var2:var2/var2:newvarvalue/'
or
sed -E 's/(Name: name.*)var2:var2/\1var2:newvarvalue/'
It's terrible because any extra comma or linefeed might break the whole thing.
var='{Name: name;Address:somea,dd;var1:var1;var2:var2},{Name: differntName;Address:someadd;var1:var1;var2:var2}'
NEW_VAL='new_value'
IFS=$'\n'
OBJECTS=("$(echo "${var}" | sed -nE 's/[^{]*(\{[^}]+\})/\1\n/gp')")
for obj in "${OBJECTS[#]}"; do
echo "${obj}" | sed -E 's/(.*var2:)(var2)(.*)/\1'"${NEW_VAL}"'\3/'
done
Output:
{Name: name;Address:somea,dd;var1:var1;var2:new_value}
{Name: differntName;Address:someadd;var1:var1;var2:new_value}
This solution accounts for a comma in the object (as exemplified in the first object) by extracting each and setting the delimiter to a newline.
I have a variable with a bunch of data.
text = "ABCDEFGHIJK"
file = garbage.txt //iiuhdsfiuhdsihf]sdiuhdfoidsoijsf
What I would like to do is replace the ] charachter in file with text. I've tried using sed but I keep getting odd errors.
output should be:
//iiuhdsfiuhdsihfABCDEFGHIJKsdiuhdfoidsoijsf
Just need to escape the ] character with a \ in regex:
text="ABCDEFGHIJK"
sed "s/\(.*\)\]\(.*\)/\1$text\2/" file > file.changed
or, for in-place editing:
sed -i "s/\(.*\)\]\(.*\)/\1$text\2/" file
Test:
sed "s/\(.*\)\]\(.*\)/\1$text\2/" <<< "iiuhdsfiuhdsihf]sdiuhdfoidsoijsf"
# output => iiuhdsfiuhdsihfABCDEFGHIJKsdiuhdfoidsoijsf
There is always the bash way that should work in your osx:
filevar=$(cat file)
echo "${filevar/]/$text}" #to replace first occurence
OR
echo "${filevar//]/$text}" #to replace all occurences
In my bash i don't even have to escape ].
By the way, the simple sed does not work?
$ a="AA"
$ echo "garbage.txt //iiuhdsfiuhdsihf]sdiuhdfoidsoijsf" |sed "s/]/$a/g"
garbage.txt //iiuhdsfiuhdsihfAAsdiuhdfoidsoijsf
I am trying to convert a Capital case word to camel case using shell script. e.g. ProjectAssignment should be converted to projectAssignment.
echo "project assignment" | sed 's/.*/\L&/; s/[a-z]*/\u&/g'
This produces output as : Project Assignment
On the similar lines, I want to convert ProjectAssignment to projectAssignment.
You can do this with gnu-sed:
s='ProjectAssignment'
echo "$s" | sed 's/^[A-Z]/\L&/'
projectAssignment
^[A-Z] will match only the first letter if it is uppercase.
Here is a quick and dirty awk command to convert any string to camelCase (no spaces):
camelCaseString=$( echo "$originalString" | awk 'BEGIN{OFS=""};{for(j=1;j<=NF;j++){ if(j==1){$j=tolower($j)} else {$j=toupper(substr($j,1,1)) tolower(substr($j,2)) }}}1')
Example:
originalString="This is my normal string"
echo $camelCaseString
thisIsMyNormalString