Jupyter shell assignment passing variables to !sed [duplicate] - shell

This question already has answers here:
How to insert strings containing slashes with sed? [duplicate]
(11 answers)
Closed 4 years ago.
This example works properly to find and replace from a Jupyter cell:
all_labels = ['cat', 'dog']
!sed -i 's/num_classes: 90/num_classes: {len(all_labels)}/g' {FILE_PATH}
However this example with the same syntax produces an error:
record_path = '/path/to/data.record'
!sed -i 's/PATH_TO_BE_CONFIGURED\/mscoco_train\.record/{record_path}/g' {FILE_PATH}
I added two forward slashes to escape the backslash and period so my regex tester would recognize the sentence. The error I get is:
sed: -e expression #1, char 50: unknown option to `s'
Anyone know why I can pass a variable in the first example, but not in the second?

I ended up needing to escape the variable too
import re
record_path = re.escape(record_path)

Related

native bash way to add trailing whitespaces to variable [duplicate]

This question already has answers here:
Create string with trailing spaces in Bash
(2 answers)
Bash LeftPad String with spaces inside variable
(1 answer)
Closed 2 years ago.
Is there a pure bash way to add trailing whitespaces with something like parameter substition in the example above I am using printf in conjunction with command substition witch is not that performant
declare -ir _CONST_VARIABLE_LENGTH='30' _CONST_SUBTRACTOR='3'
declare some_var='here is a string'
declare new_var
new_var="$(printf "%-$((_CONST_VARIABLE_LENGTH-_CONST_SUBTRACTOR))s" "$some_var")"
# what i want, but doesn't work
# ${var:0:LENGTH} only goes till actually length and won't add something if LENGTH is greater than actual var lenght
new_var="${some_var:0:$((_CONST_VARIABLE_LENGTH-_CONST_SUBTRACTOR))}"

What does expanding a variable as "${var%%r*}" mean in bash? [duplicate]

This question already has an answer here:
Bash: manipulating with strings (percent sign)
(1 answer)
Closed 6 years ago.
I've got the following variable set in bash:
ver=$(/usr/lib/virtualbox/VBoxManage -v | tail -1)
then I have the following variable which I do not quite understand:
pkg_ver="${ver%%r*}"
Could anyone elaborate on what this does, and how pkg_ver is related to the original ver value?
It is a bash parameter expansion syntax to extract text from end of string upto first occurrence of r
name="Ivory"
printf "%s\n" "${name%%r*}"
Ivo
${PARAMETER%%PATTERN}
This form is to remove the described pattern trying to match it from the end of the string. The operator "%" will try to remove the shortest text matching the pattern, while "%%" tries to do it with the longest text matching.
You will get everything from variable ver until first "r" character and it will be stored inside pkg_ver.
export ver=aaarrr
echo "${ver%%r*}"
aaa

Replacing a particular string in a unix script with sed [duplicate]

This question already has answers here:
How to insert strings containing slashes with sed? [duplicate]
(11 answers)
Closed 6 years ago.
I have a string like below
/home/adcde
to be replaced with efgh
so the same adcde is used some where else in the script which I don't want to replace.
so If I am replacing with the below command
sed 's/adcde/efgh/g' - it replaces the all of it
so instead I want to replace the adcde that starts with a /
sed 's/"/adcde"/efgh/g'
but it does not seem to work
any help would be appreciate?
sed 's:/adcde:/efgh:g'- solved the issue
Thanks to Benjamin for pointing it out

Concatenating asterisks in bash [duplicate]

This question already has answers here:
Printing asterisk ("*") in bash shell
(2 answers)
Closed 7 years ago.
I am trying to concatenate asterisks (*) to a string variable. However, I keep getting the files in the current directory instead. I have tried
row+='*'
row+=$row #where row is *
row='*'"$row"
row="\*$row"
row="${row}*"
etc.
row='*'."$row" #produces *.*.*.
I thought \ would escape the *, but it didn't work.
Try using quotes around the variable before you "print".
For example:
cronSen="*/$a * * * * bash /etc/init.d/ckDskCheck.sh"
echo "$cronSen"
This could work. I got the idea from here: Printing asterisk (*) in bash shell

why we use ##*/ expression with bash variable [duplicate]

This question already has answers here:
explain the linux regex for getting filename
(2 answers)
Closed 8 years ago.
I am tring to understand the bash script.
I am seeing ##* / expression with bash variable.
i.e ${foo##*/}
Can someone please tell me why we use that expression?
It's called "Parameter expansion". The variable $foo is searched for a substring matching the pattern */ (i.e. anything up to a slash) from the beginning (#), and what remains in the variable is returned. Doubling the #-sign makes the matching greedy, i.e. it tries to find the longest possible match.

Resources