LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-04-2023, 05:30 PM   #1
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
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

Last edited by GPGAgent; 02-04-2023 at 05:32 PM.
 
Old 02-04-2023, 07:54 PM   #2
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
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]}"

Last edited by MadeInGermany; 02-04-2023 at 07:59 PM.
 
Old 02-04-2023, 09:02 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,141

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
regex. Allows you to also easily check you are testing the correct record.

Lot easier with bash these days than it used to be.
 
Old 02-05-2023, 04:15 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,949

Rep: Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325Reputation: 7325
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
 
Old 02-05-2023, 09:06 AM   #5
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
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!
 
Old 02-05-2023, 09:43 AM   #6
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by MadeInGermany View Post
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
 
Old 02-05-2023, 10:40 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
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.
 
1 members found this post helpful.
Old 02-05-2023, 05:23 PM   #8
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Thumbs up

Quote:
Originally Posted by MadeInGermany View Post
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
 
Old 02-05-2023, 10:45 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,363

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to capture 1000 lines before a string match and 1000 line a string match including line of string match ? sysmicuser Linux - Newbie 12 11-14-2017 05:21 AM
re:bash - how would I test for a string from a file and do something based on $string slacker_ Programming 2 06-09-2014 03:35 AM
replace string by another string in bash shell chinhth2010 Linux - General 2 08-02-2011 10:40 PM
[SOLVED] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 07:22 PM
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration