Sponsored Content
Top Forums Shell Programming and Scripting BASH Regex - get filename tags, labels and descriptions Post 303024953 by Bishop on Sunday 21st of October 2018 12:59:27 AM
Old 10-21-2018
Ok, I think I got this thing working now.
I did not know how to extract content A,B,C from "only" end brackets so I had to create new VAR from the tail brackets (if existed) and then extract contents of A,B and C from that. Otherwise regex would be getting 1,2,3 from brackets in the filename.

Anyway... it is ok as it is now but if there is a better solution I would love to learn it.
note: this is for string testing only, once it is in a real script I would have checking to see if file or directory before getting shortName and extension.

Code:
#!/bin/bash
test_str=(
    'test1.{1}(2)[3] dots.spaces {A1}(B1)[C1].ext'
    'test2.[3]{1}(2) dots.spaces [C1](B1){A1}.ext'
    'test3 [3].com [C1].ext'
    'test4 {1}(2)[3]dots.ext'
    'test4 {1} (2) [3] dots.ext'
    'test6 (B1)[C1].ext'
    'test7[C1](B1).ext'
)
IFS='
'
regexp='\s*(\([^)]+\)|\{[^}]+\}|\[[^][]+\])+$'
subst=""
regexTag='.*\[([^]]+)\]'
regexLab='.*\{([^}]+)\}'
regexDes='.*\(([^)]+)\)'


for p in "${test_str[@]}"
do
    NameNoExt=`echo "${p%.*}"`
    if [[ $NameNoExt =~ $regexp ]]; then
        tailBrackets="${BASH_REMATCH[0]}";
        shortName=$(echo "$NameNoExt" | sed -r 's/'$regexp'/'$subst'/g');
    else
        shortName="$NameNoExt"
        tailBrackets=""
    fi
    # if tail bracket exist then extract A,B,C
    if [ $tailBrackets ]; then
        if [[ $tailBrackets =~ $regexTag ]]
        then 
          TAG="${BASH_REMATCH[1]}"
        else 
          TAG=""
        fi
        if [[ $tailBrackets =~ $regexLab ]]
        then 
          LAB="${BASH_REMATCH[1]}"
        else 
          LAB=""
        fi
        if [[ $tailBrackets =~ $regexDes ]]
        then 
          DES="${BASH_REMATCH[1]}"
        else 
          DES=""
        fi
    else
        TAG=""
        LAB=""
        DES=""
    fi
    zenity --info --text "`printf "Filename = \"$p\"\n\nNameNoExt = \"$NameNoExt\"\nShort Name  = \"$shortName\"\nTail Brackets = \"$tailBrackets\"\nA  = \"$LAB\"\nB  = \"$DES\"\nC  = \"$TAG\""`"
done

 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

/etc/vfstab Field Descriptions

While I was reading a Sun SysAdmin Guide, I came across this point... /etc/vfstab Field Descriptions mount at boot - The root (/), /usr and /var file systems are not mounted from the vfstab file initially. This field should always be set to no for these file systems and for virtual file... (2 Replies)
Discussion started by: minazk
2 Replies

2. Shell Programming and Scripting

regex test in bash

Hi I want to do a regex test and branch based on the test result, but this doesn't seems to work :confused: if \) ]] then echo success else echo failed fi (1 Reply)
Discussion started by: subin_bala
1 Replies

3. Shell Programming and Scripting

Bash regex

Hello everybody, I'm clearly not an expert in bash scripting as I've written maybe less than 10 scripts in my life. I'm trying to strip an xml string removing every tag in it. I'm using bash substitution to do so, but apparently I missed something about what is a regex for bash ... As an... (4 Replies)
Discussion started by: kerloi
4 Replies

4. Shell Programming and Scripting

[BASH] Allow name with spaces (regex)

Hey all, I have a very simple regular expression that I use when I want to allow only letters with spaces. (I know this regex has a lot of shortcomings, but I'm still trying to learn them) isAlpha='^*$'However, when I bring this over to BASH it doesn't allow me to enter spaces. I use the... (3 Replies)
Discussion started by: whyte_rhyno
3 Replies

5. Shell Programming and Scripting

Bash regex help

I've been using the following regex below in a bash script on RHEL 5.5 using version GNU bash, version 3.2.25(1)-release I've tried using the script on RHEL 6.3 which uses GNU bash, version 4.1.2(1)-release I assume there's been alot of changes to bash since that's quite a jump in revisions.... (12 Replies)
Discussion started by: woodson2
12 Replies

6. UNIX for Dummies Questions & Answers

Need help with Regex for bash

Hi, I am trying to match this word: hexagon-bx.mydomain.com with regex. I have tried this: "\.*]*$" So far I have not been successful. I also need to make sure that the regex will match words that just have lowercase letters and numbers in them, such as camera01. How can I create such an... (5 Replies)
Discussion started by: newbie2010
5 Replies

7. Shell Programming and Scripting

Regex for filename in grep

I want to print the filename keyword="XXTNL_AVSKRIV2ING" ftype="sql' I wan to search the keyword in all the sql files and the output shoul dbe filename:count grep -iwc "$keyword" *.$ftype | grep -v ":0$" But the output does not dispaly the filename which contains space as... (4 Replies)
Discussion started by: millan
4 Replies

8. AIX

AIX Command LED Descriptions

Dear master Unix.com, I ask about command, what is command the meaning this? and what the function? /usr/lpp/diagnostics/bin/usysfault -s normal regards, -Ruhul (3 Replies)
Discussion started by: williamen
3 Replies

9. UNIX for Dummies Questions & Answers

Regex for (a|b) in bash

I am trying to find files using the following by using simple bash script: if -2014 ]]; then echo "yes";fi What I need to find are any files with date 08-**-2014 so August 2014 any files. I can use if -2014 ]]; then echo "yes";fi That works fine. How do I get files beginning with 08... (1 Reply)
Discussion started by: newbie2010
1 Replies

10. Shell Programming and Scripting

Using RegEx with variable within bash if [[ ]]

I stumbled upon a problem, which I simplified to this: There is a list of numbers, stored in variable $LIST, lets use `seq 5 25` for demonstration. There is a number that should be compared against this list. For demonstration I use user input - read VALUE I am trying to compare RegEx... (2 Replies)
Discussion started by: Zorbeg
2 Replies
bindtags(n)						       Tk Built-In Commands						       bindtags(n)

__________________________________________________________________________________________________________________________________________________

NAME
bindtags - Determine which bindings apply to a window, and order of evaluation SYNOPSIS
bindtags window ?tagList? _________________________________________________________________ DESCRIPTION
When a binding is created with the bind command, it is associated either with a particular window such as .a.b.c, a class name such as But- ton, the keyword all, or any other string. All of these forms are called binding tags. Each window contains a list of binding tags that determine how events are processed for the window. When an event occurs in a window, it is applied to each of the window's tags in order: for each tag, the most specific binding that matches the given tag and event is executed. See the bind command for more information on the matching process. By default, each window has four binding tags consisting of the name of the window, the window's class name, the name of the window's near- est toplevel ancestor, and all, in that order. Toplevel windows have only three tags by default, since the toplevel name is the same as that of the window. The bindtags command allows the binding tags for a window to be read and modified. If bindtags is invoked with only one argument, then the current set of binding tags for window is returned as a list. If the tagList argu- ment is specified to bindtags, then it must be a proper list; the tags for window are changed to the elements of the list. The elements of tagList may be arbitrary strings; however, any tag starting with a dot is treated as the name of a window; if no window by that name exists at the time an event is processed, then the tag is ignored for that event. The order of the elements in tagList determines the order in which binding scripts are executed in response to events. For example, the command bindtags .b {all . Button .b} reverses the order in which binding scripts will be evaluated for a button named .b so that all bindings are invoked first, following by bindings for .b's toplevel ("."), followed by class bindings, followed by bindings for .b. If tagList is an empty list then the binding tags for window are returned to the default state described above. The bindtags command may be used to introduce arbitrary additional binding tags for a window, or to remove standard tags. For example, the command bindtags .b {.b TrickyButton . all} replaces the Button tag for .b with TrickyButton. This means that the default widget bindings for buttons, which are associated with the Button tag, will no longer apply to .b, but any bindings associated with TrickyButton (perhaps some new button behavior) will apply. EXAMPLE
If you have a set of nested frame widgets and you want events sent to a button widget to also be delivered to all the widgets up to the current toplevel (in contrast to Tk's default behavior, where events are not delivered to those intermediate windows) to make it easier to have accelerators that are only active for part of a window, you could use a helper procedure like this to help set things up: proc setupBindtagsForTreeDelivery {widget} { set tags [list $widget [winfo class $widget]] set w $widget set t [winfo toplevel $w] while {$w ne $t} { set w [winfo parent $w] lappend tags $w } lappend tags all bindtags $widget $tags } SEE ALSO
bind(n) KEYWORDS
binding, event, tag Tk 4.0 bindtags(n)
All times are GMT -4. The time now is 01:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy