LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash String Handling (https://www.linuxquestions.org/questions/linux-newbie-8/bash-string-handling-4175721687/)

GPGAgent 02-04-2023 05:30 PM

Bash String Handling
 
Hi All, yet again I cannot get string handling in bash into my head, I don't do enough of itso here goes:

Quote:

720x480 | crop=656:480:22:32 : MyMovie.mp4
I want to get the last two crop values, 22 and 32 into variables to check.

If more than 30 then I'll report it via echo

MadeInGermany 02-04-2023 07:54 PM

You can split it via a read command.
Code:

IFS=":" read a b c d e <<< "720x480 | crop=656:480:22:32 : MyMovie.mp4"
echo "$c" "$d"

Or like this
Code:

[[ "720x480 | crop=656:480:22:32 : MyMovie.mp4" =~ ^[^:]*:[^:]*:([^:]*):([^:\ ]*) ]]
echo "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}"


syg00 02-04-2023 09:02 PM

regex. Allows you to also easily check you are testing the correct record.

Lot easier with bash these days than it used to be.

pan64 02-05-2023 04:15 AM

or you can use arrays, if you wish:
Code:

var="720x480 | crop=656:480:22:32 : MyMovie.mp4"
var2=( ${var//:/ } )
echo ${var2[3]}  # 480
echo ${var2[4]}  # 22


GPGAgent 02-05-2023 09:06 AM

Thank you everybody, that's brilliant - I was thinking awk was a possible solution, actually it probably is.

I could easily do this in SQL but that's becasue I've been using it for 30+ years!

GPGAgent 02-05-2023 09:43 AM

Quote:

Originally Posted by MadeInGermany (Post 6409180)
You can split it via a read command.
Code:

[[ "720x480 | crop=656:480:22:32 : MyMovie.mp4" =~ ^[^:]*:[^:]*:([^:]*):([^:\ ]*) ]]
echo "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}"


This worked just fine - thanks

But could you explain the mask and why its 1 and 2 in the BASH_REMATCH array

MadeInGermany 02-05-2023 10:40 AM

man bash
Quote:

...
Substrings matched by parenthesized subexpressions within the regular expression are saved in the array variable BASH_REMATCH. The element of BASH_REMATCH with index 0 is the portion of the string matching the entire regular expression. The element of BASH_REMATCH with index n is the portion of the string matching the nth parenthesized subexpression.
...
The RE must match, then the 1st (capture group) lands in ${BASH_REMATCH[1]} the 2nd in ${BASH_REMATCH[2]} etc. This is in tune with sed and awk where capture groups are referred as \1 \2 etc.
Also ${BASH_REMATCH[0]} is the total matching portion, like grep -o would report it.

GPGAgent 02-05-2023 05:23 PM

Quote:

Originally Posted by MadeInGermany (Post 6409299)
man bash


The RE must match, then the 1st (capture group) lands in ${BASH_REMATCH[1]} the 2nd in ${BASH_REMATCH[2]} etc. This is in tune with sed and awk where capture groups are referred as \1 \2 etc.
Also ${BASH_REMATCH[0]} is the total matching portion, like grep -o would report it.

Thanks, that helps loads

chrism01 02-05-2023 10:45 PM

Just for fun
Code:

v1=echo ' 720x480 | crop=656:480:22:32 : MyMovie.mp4 '|cut -d':' -f3
v2=echo ' 720x480 | crop=656:480:22:32 : MyMovie.mp4 '|cut -d':' -f4



All times are GMT -5. The time now is 08:29 PM.