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

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/.*://'

Related

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

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

Splitting a character in Linux by numbers [duplicate]

This question already has answers here:
How to extract date from filename with extenstion using shell script
(2 answers)
Closed 9 months ago.
Here I print the file,
cat testfile.txt
demo_test_file_2022-06-06
i need a output like this
demo_test_file_ 2022-06-06
please help me for splitting the line when numbers present in Linux
thanks in advance
You need to read that file line-by-line into an std::string variable, then use find_first_of to find the first digit.
Then use substr

How to calculate the length of a xterm control sequence [duplicate]

This question already has answers here:
How can I remove the ANSI escape sequences from a string in python
(8 answers)
Closed 3 years ago.
I am looking to format text before passing it to stdout/console. To do this properly I need to know how long a given section of text is after being printed to the console, which requires knowing how much characters will not be printed due to them being escape sequences.
On the web I have found multiple documentations for these sequence commands, but there seems to be no quick and easy way to find out where a given escape sequence command ends unless I was to parse them. Is there a trick/solution to getting the length of any escape sequence command without parsing?
How many characters is this \x1b[38;2;20;60;122m string \x1b[0m in console?
You can use ansifilter:
$ printf '\x1b[38;2;20;60;122mabc\x1b[0m' | ./ansifilter --no-trailing-nl | wc -c
3

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/}"

Resources