Find and replace in shell script (special characters) - shell

I am new to shell scripting:
I have following:
old=/dev/sda
new=/dev/sda5
Given these variables, I need to extract 5 from "new" string.
How should I go about it? sed? awk?
Tried using:
partitionno=$(echo $new | sed 's/$old//g')

To get the correct result with the least change to your command, try:
partitionno=$(echo "$new" | sed "s|$old||g")
There are two key points here:
Shell variables are not expanded inside single quotes. So '$old' remains as the original four characters: $, o, l, and d. For the shell variables to be expanded, use double quotes.
sed "s/$old//g" still won't work because there are too many slashes. A substitute command uses three slashes. After the shell expands $old, there are five slashes. The solution is to use a different delimiter for the substitute command. I chose | above because | is not likely to be in a file name.

Using Shell Parameter Expansion:
$ old=/dev/sda
$ new=/dev/sda5
$ echo "${new#$old}"
5
${parameter#word}
${parameter##word}
The word is expanded to produce a pattern just as in filename expansion (see Filename Expansion). If the pattern matches the beginning of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ‘#’ case) or the longest matching pattern (the ‘##’ case) deleted. If parameter is ‘#’ or ‘’, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with ‘#’ or ‘’, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

Related

what does "// /_" mean in bash scripting [duplicate]

This question already has an answer here:
What is the meaning of `//` in Bash parameter expansions?
(1 answer)
Closed last year.
I am currently moving our shell/bash scripts from a jenkinsfile to groovy scripts that are stored in methods but still execute as sh scripts.
The issue i have is with variables containing // /_
exmaple:
${VARIABLE_NAME// /_}
I cannot find what // /_ exactly does when supplied like this in a variable.
I need to find another way to do this because when moved to Groovy methods, it causes formating issues where escaping doesnt work properly.
It will replace all spaces with underscores.
Consider the following example:
$ var='hello world john doe'
$ echo "${var// /_}"
hello_world_john_doe
$
${parameter/pattern/string}
The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string. The match is performed according to the rules described below (see Pattern Matching). If pattern begins with ‘/’, all matches of pattern are replaced with string. Normally only the first match is replaced. If pattern begins with ‘#’, it must match at the beginning of the expanded value of parameter. If pattern begins with ‘%’, it must match at the end of the expanded value of parameter. If string is null, matches of pattern are deleted and the / following pattern may be omitted. If the nocasematch shell option (see the description of shopt in The Shopt Builtin) is enabled, the match is performed without regard to the case of alphabetic characters. If parameter is ‘#’ or ‘’, the substitution operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with ‘#’ or ‘’, the substitution operation is applied to each member of the array in turn, and the expansion is the resultant list.
Copied from: https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html with my emphasize on how / and // differences.

bash shell reworking variable replace dots by underscore

I can't see to get it working :
echo $VERSIONNUMBER
i get : v0.9.3-beta
VERSIONNUMBERNAME=${VERSIONNUMBER:1}
echo $VERSIONNUMBERNAME
I get : 0.9.3-beta
VERSION=${VERSIONNUMBERNAME/./_}
echo $VERSION
I get : 0_9.3-beta
I want to have : 0_9_3-beta
I've been googling my brains out I can't make heads or tails of it.
Ideally I'd like to remove the v and replace the periods with underscores in one line.
Let's create your variables:
$ VERSIONNUMBER=v0.9.3-beta
$ VERSIONNUMBERNAME=${VERSIONNUMBER:1}
This form only replaces the first occurrence of .:
$ echo "${VERSIONNUMBERNAME/./_}"
0_9.3-beta
To replace all occurrences of ., use:
$ echo "${VERSIONNUMBERNAME//./_}"
0_9_3-beta
Because this approach avoids the creation of pipelines and subshells and the use of external executables, this approach is efficient. This approach is also unicode-safe.
Documentation
From man bash:
${parameter/pattern/string}
Pattern substitution. The pattern is expanded to produce a pattern
just as in pathname expansion. Parameter is expanded and the longest
match of pattern against its value is replaced with
string. If pattern begins with /, all matches of pattern are replaced
with string. Normally only the first match is replaced. If
pattern begins with #, it must match at the beginning of the expanded
value of parameter. If pattern begins with %, it must match at the
end of the expanded value of parameter. If string
is null, matches of pattern are deleted and the / following pattern
may be omitted. If the nocasematch shell option is enabled, the
match is performed without regard to the case of alphabetic
characters. If parameter is # or *, the substitution operation is
applied to each positional parameter in turn, and the
expansion is the resultant list. If parameter is an array variable
subscripted with # or *, the substitution operation is
applied to each member of the array in turn, and the expansion is the
resultant list.
(Emphasis added.)
You can combine pattern substitution with tr:
VERSION=$( echo ${VERSIONNUMBER:1} | tr '.' '_' )

Stripping characters from a Bash variable

I am attempting to write a bash script (poorly) and need assistance in stripping characters from a variable.
variable is defined as $managementipmask= 111.111.111.111/24
I need to strip the /24 from the end of the variable.
Thanks in advance.
Use parameter expansion to remove everything from the first /:
$ k="111.111.111.111/24"
$ echo "${k%%/*}"
111.111.111.111
See this resource on parameter expansion for additional details:
http://mywiki.wooledge.org/BashGuide/Parameters#Parameter_Expansion
${parameter%pattern}
The 'pattern' is matched against the end of 'parameter'. The result is
the expanded value of 'parameter' with the shortest match deleted.
${parameter%%pattern}
As above, but the longest match is deleted.
So you can delete from the last / using a single %:
$ k="111.111.111.111/24/23"
$ echo "${k%/*}"
111.111.111.111/24
Another way:
k="111.111.111.111/24"
echo "${k/%\/24/}"
It replaces last /24 with empty string.
From Bash Manual:
${parameter/pattern/string}
The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the longest match of pattern
against its value is replaced with string. If pattern begins with ‘/’,
all matches of pattern are replaced with string. Normally only the
first match is replaced. If pattern begins with ‘#’, it must match at
the beginning of the expanded value of parameter. If pattern begins
with ‘%’, it must match at the end of the expanded value of parameter.
If string is null, matches of pattern are deleted and the / following
pattern may be omitted. If parameter is ‘#’ or ‘’, the substitution
operation is applied to each positional parameter in turn, and the
expansion is the resultant list. If parameter is an array variable
subscripted with ‘#’ or ‘’, the substitution operation is applied to
each member of the array in turn, and the expansion is the resultant
list.

SED script called from BASH: How can I shorten variable path in output?

I am wrinting a sed invocation using a shell variable. The variable contains a path name with file name:
sed "file name is '$variable'"
...
variable=/path/path/file.txt
The problem is that I don't need the /path/path/ part. I need just file.txt part in output.
Also my path is dynamic so I am guessing that I need to search (somehow) in a string for a first slash from the ending. How do I do that?
You can use basename to do that:
basename /tmp/a.jpg
a.jpg
You can use the shell's variable substitution feature to remove parts matching a glob pattern:
$ variable=/path/path/file.txt
$ echo ${variable##*/} # Remove longest left part matching "*/"
file.txt
From the bash manual:
${parameter#word}
${parameter##word}
The word is expanded to produce a pattern just as in pathname expansion. If
the pattern matches the beginning of the value of parameter, then the result
of the expansion is the expanded value of parameter with the shortest match-
ing pattern (the ‘‘#’’ case) or the longest matching pattern (the ‘‘##’’
case) deleted. If parameter is # or *, the pattern removal operation is
applied to each positional parameter in turn, and the expansion is the
resultant list. If parameter is an array variable subscripted with # or *,
the pattern removal operation is applied to each member of the array in
turn, and the expansion is the resultant list.
${parameter%word}
${parameter%%word}
The word is expanded to produce a pattern just as in pathname expansion. If
the pattern matches a trailing portion of the expanded value of parameter,
then the result of the expansion is the expanded value of parameter with the
shortest matching pattern (the ‘‘%’’ case) or the longest matching pattern
(the ‘‘%%’’ case) deleted. If parameter is # or *, the pattern removal
operation is applied to each positional parameter in turn, and the expansion
is the resultant list. If parameter is an array variable subscripted with #
or *, the pattern removal operation is applied to each member of the array
in turn, and the expansion is the resultant list.

Get the value before decimal point

I am writing a Bash script. I have a variable in it which is a float value. For example:
x=2099.2
I need the value before decimal point, i.e, only 2099.
You can use the following shell parameter expansion:
${x%%.*}
This removes everything from the first dot.
See it live:
$ v=203.4
$ echo ${v%%.*}
203
$ v=2.3.4
$ echo ${v%%.*}
2
From Bash Reference Manual → 3.5.3 Shell Parameter Expansion:
${parameter%%word}
The word is expanded to produce a pattern just as in filename expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the value of parameter with the longest matching pattern deleted. If parameter is ‘#’ or ‘’, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with ‘#’ or ‘’, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

Resources