Removing everything after last hyphen in a string in Bash script? [duplicate] - string

This question already has answers here:
How can I remove all text after a character in bash?
(7 answers)
Closed last month.
Working on a script where I need to take a string, and remove everything after the last occurence of a certain character. In this case a hyphen.
For example, This-is-a-filename-0001.jpg should result in This-is-a-filename

You can cut strings in bash:
line="This-is-a-filename-0001.jpg"
echo "${line%-*}" # prints: This-is-a-filename
The %-*operator removes all beginning with the last hyphen.

You're looking for a sed within your script, something close to what's below.
sed 's!/[^/]*$!/!'
Generally, I would say, please do research before posting a question like yours since it's relatively easy to find the answers

Related

How to escape backslashes and forward slashes in VIM find/search? [duplicate]

This question already has answers here:
How does one escape backslashes and forward slashes in VIM find/search?
(6 answers)
Closed 2 years ago.
for example, I want to find and replace, say my original string is "//hello" to "hello".
Actually I'm trying to uncomment lines which I've commented before.
substitution
if your pattern contains slashes /, e.g. //hello you can of course escape them in s/pat/repl/ command. However, better to pick some another delimiter for your s command:
:s#//hello#whatever#g
In this way, the command is easier to read and understand.
Search
Say If you want to search //hello, you can try a backward search with ?, then you don't have to escape the slashes.
Did you try this:
%s/\/\/hello/hello

How to get last value before a colon in bash [duplicate]

This question already has answers here:
How to get a substring after the last underscore (_) in unix shell script
(3 answers)
Closed 3 years ago.
I have a string like:
arn:aws:ecs:us-east-1:123456789:task-definition/myservice:10
Is there anyway I can get the last value 10? I tried to get last character but forgot that this int value can increase and eventually becomes 2 characters.
Well, many ways, this one works, though is not elegant :)
echo "arn:aws:ecs:us-east-1:123456789:task-definition/myservice:10" | sed 's/.*://'

Unable To Concatenate 2 Linux Variables Into 1 [duplicate]

This question already has answers here:
How to concatenate string variables in Bash
(30 answers)
Filename not printing correctly with underscore "_" in Bash [duplicate]
(2 answers)
Closed 4 years ago.
I'm trying to concatenate two variables in linux tectia SSH into one variable, separated by "_". For some reason only one of the two variables is printed out.
I've tried to concatenate via " " e.g.:
sample1="$var1_$var2"
or
sample1="$var1 _ $var2"
and I've tried to concatenate directly e.g.:
sample1=$var1_$var2
Would appreciate any help given!
cnt_abr1=ab
cnt_abr2=cd
cnt_abr3=ef
env_abr1=a
env_abr2=b
sample1="$env_abr1_$cnt_abr1"
sample2=$env_abr2_$cnt_abr3
echo $sample1
echo $sample2
Output:
_ ab
ef
Since underscores are effectively letters, bash has no way of knowing when your variable name ends and your literal underscore begins. The proper way to reference the variables is with ${...} in this case, which unambiguously delimits the name from the rest of the command line:
sample1="${env_abr1}_${cnt_abr1}"
sample2=${env_abr2}_${cnt_abr3}
In both cases, the second name does not require special treatment. Any other (semantically valid) non-letter character would do as well, as you pointed out in your comment:
sample1="$env_abr1"_"$cnt_abr1"
sample2="$env_abr2"_"$cnt_abr3"

Cut characters from position from string in bash [duplicate]

This question already has answers here:
Remove the middle n characters from lines in Bash
(6 answers)
Closed 4 years ago.
I have the following string, which it will always be 35 characters long:
S202SCTRXBAVCWPJAC001181204120000.N
I would like to cut 3 characters (position 17-19), JAC in this case, to remain only
S202SCTRXBAVCWP001181204120000.N
Is there a way to achieve this in bash?
strIn=S202SCTRXBAVCWPJAC001181204120000.N
strOut=${strIn:0:15}${strIn:18}
echo "$strOut"
...uses only bash-built-in functionality to emit:
S202SCTRXBAVCWP001181204120000.N
...as it emits the first 15 characters starting at position 0, then everything after position 18.
Agree with the answer by Charles Duffy. If you know you want specifically "JAC" instead of what indices you want removed, you could do:
str="S202SCTRXBAVCWPJAC001181204120000.N"
echo "${str/JAC/}"

What's the meaning of this sed command? sed 's%^.*/%%' [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 1 year ago.
I saw a bash command sed 's%^.*/%%'
Usually the common syntax for sed is sed 's/pattern/str/g', but in this one it used s%^.* for the s part in the 's/pattern/str/g'.
My questions:
What does s%^.* mean?
What's meaning of %% in the second part of sed 's%^.*/%%'?
The % is an alternative delimiter so that you don't need to escape the forward slash contained in the matching portion.
So if you were to write the same expression with / as a delimiter, it would look like:
sed 's/^.*\///'
which is also kind of difficult to read.
Either way, the expression will look for a forward slash in a line; if there is a forward slash, remove everything up to and including that slash.
the usually used delimiter is / and the usage is sed 's/pattern/str/'.
At times you find that the delimiter is present in the pattern. In such a case you have a choice to either escape the delimiter found in the pattern or to use a different delimiter. In your case a different delimiter % has been used.
The later way is recommended as it keeps your command short and clean.

Resources