How remove first match in gsub? [duplicate] - ruby

This question already has answers here:
Ruby - replace the first occurrence of a substring with another string
(3 answers)
Closed 6 years ago.
I have this
2016-05-20T13:36:29.835, CTF3D57C
and I want this
2016-05-2013:36:29.835, CTF3D57C
I just want to remove the first T character. How do I do this?

This will substitute the first 'T' in string with anything you want:
str = str.sub('T', '')
If you wish to substitute all occurrence of any substring or regex, use gsub

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

Split a string by two delimiters [duplicate]

This question already has answers here:
Split string by multiple delimiters
(6 answers)
Closed 6 years ago.
I want to split a string by whitespaces and # using a single ruby command.
word.split(" ") will split by whitespaces ;
word.split("#") will split by '.
How to do all three at once?
Use regular expressions' character class to do that:
word.split(/[ #]/)
To match any whitespace character use \s : word.split(/[\s#]/)
A character class is delimited with square brackets ([, ]) and lists
characters that may appear at that point in the match. /[ab]/ means a
or b, as opposed to /ab/ which means a followed by b.
/\s/ - A whitespace character: /[ \t\r\n\f]/

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

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