Sponsored Content
Top Forums Shell Programming and Scripting BASH Regex - get filename tags, labels and descriptions Post 303024645 by Bishop on Saturday 13th of October 2018 10:07:02 PM
Old 10-13-2018
BASH Regex - get filename tags, labels and descriptions

Hi, I am trying to switch from windows to linux.
I have been using Autohotkey scripts for some little things.
I started writing some bash scripts for NEMO browser in linux mint and I am trying to convert some of AHK scripts but as I am not a programmer. I have a hard time with regex stuff.

What I did is split file path and I got it working ok. -- Directory, FileName, Filename-Without-Extension, Extension

Now every filename may or may not contain [tags], {labels} and (description) in brackets at the end of the filename.
Where I am stuck is regex that works in Autohotkey is not in BASH. I tried sed, awk, but I have no idea what I am doing.

example filename:
multiple.{1}(1)[1] dots.and spaces {labels}(description)[tag1 tag2].txt
filename with only tags [tag1 tag2 tag3].txt

Note:
Tags, labels or description can be any words or numbers, not just what I have in example.
Not every filename will have all 3. Some could have only tags, others only descriptions or labels.

need to split and get this:
shortName = "multiple.{1}(1)[1] dots.and spaces" -- filename without any tags,labels and descriptions
tags = "tag1 tag2" -- tags without brackets
labels = "label" -- labels without brackets
description = "description" -- description without brackets


Original regex from Autohotkey that works in all of my scripts

Code:
; NameNoExt is filename without extension
; get filename only without tags[], labels{} or description() 
ShortName:=RegExReplace(NameNoExt, "\s*(\([^()]+\)|{[}]+|\[[^][]+\])+$")
; Get tags
Tags:=RegExReplace(NameNoExt, ".*\[(.*)?\](?!\s).*$", "$1")
; Get labels
Label:= RegExReplace(NameNoExt, ".*\{(.*)?\}(?!\s).*$", "$1")
; Get description
Description := RegExReplace(NameNoExt, ".*\((.*)?\)(?!\s).*$", "$1")



Here is what I have in BASH
.. and it is going nowere..
So to mention again, this is where I need help with regex splitting these tags, labels and descriptions.

Code:
str="multiple.{1}(1)[1] dots.and spaces {lab}(desc)[tag1 tag2].txt"

IFS='
'

NameNoExt=`echo "${str%.*}"`        # for filename with multiple dots

shortName="???"        # get "multiple.{1}(1)[1] dots"

#regexp=".*\[(.*)?\](?!\s).*$"    # from autohotkey
regexp="\[([^)]+)\]+$"
if [[ $NameNoExt =~ $regexp ]]
then 
  TAG="${BASH_REMATCH[1]}"
else 
  TAG="Nothing"
fi
#regexp=".*\{(.*)?\}(?!\s).*$"    # from autohotkey
regexp="\{([^}]+)\}+$"
if [[ $NameNoExt =~ $regexp ]]
then 
  LAB="${BASH_REMATCH[1]}"
else 
  LAB="Nothing"
fi
#regexp=".*\((.*)?\)(?!\s).*$"    # from autohotkey
regexp="\(([^)]+)\)+$"
if [[ $NameNoExt =~ $regexp ]]
then 
  DESC="${BASH_REMATCH[1]}"
else 
  DESC="Nothing"
fi
zenity --info --text "`printf "Input = \"$str\"\n\nNameNoExt = \"$NameNoExt\"\nShort Name  = \"$shortName\"\nTags  = \"$TAG\"\nLabels  = \"$LAB\"\nDescription  = \"$DESC\""`"

I highly appreciate any help with this.
Thank you all.
 

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
HTML::TagCloud(3pm)					User Contributed Perl Documentation				       HTML::TagCloud(3pm)

NAME
HTML::TagCloud - Generate An HTML Tag Cloud SYNOPSIS
# A cloud with tags that link to other web pages. my $cloud = HTML::TagCloud->new; $cloud->add($tag1, $url1, $count1); $cloud->add($tag2, $url2, $count2); $cloud->add($tag3, $url3, $count3); my $html = $cloud->html_and_css(50); # A cloud with tags that do not link to other web pages. my $cloud = HTML::TagCloud->new; $cloud->add_static($tag1, $count1); $cloud->add_static($tag2, $count2); $cloud->add_static($tag3, $count3); my $html = $cloud->html_and_css(50); # A cloud that is comprised of tags in multiple categories. my $cloud = HTML::TagCloud->new; $cloud->add($tag1, $url1, $count1, $category1); $cloud->add($tag2, $url2, $count2, $category2); $cloud->add($tag3, $url3, $count3, $category3); my $html = $cloud->html_and_css(50); # The same cloud without tags that link to other web pages. my $cloud = HTML::TagCloud->new; $cloud->add_static($tag1, $count1, $category1); $cloud->add_static($tag2, $count2, $category2); $cloud->add_static($tag3, $count3, $category3); my $html = $cloud->html_and_css(50); # Obtaining uncategorized HTML for a categorized tag cloud. my $html = $cloud->html_without_categories(); # Explicitly requesting categorized HTML. my $html = $cloud->html_with_categories(); DESCRIPTION
The HTML::TagCloud module enables you to generate "tag clouds" in HTML. Tag clouds serve as a textual way to visualize terms and topics that are used most frequently. The tags are sorted alphabetically and a larger font is used to indicate more frequent term usage. Example sites with tag clouds: <http://www.43things.com/>, <http://www.astray.com/recipes/> and <http://www.flickr.com/photos/tags/>. This module provides a simple interface to generating a CSS-based HTML tag cloud. You simply pass in a set of tags, their URL and their count. This module outputs stylesheet-based HTML. You may use the included CSS or use your own. CONSTRUCTOR
new The constructor takes a few optional arguments: my $cloud = HTML::TagCloud->new(levels=>10); if not provided, levels defaults to 24 my $cloud = HTML::TagCloud->new(distinguish_adjacent_tags=>1); If distinguish_adjacent_tags is true HTML::TagCloud will use different CSS classes for adjacent tags in order to be able to make it easier to distinguish adjacent multi-word tags. If not specified, this parameter defaults to a false value. my $cloud = HTML::TagCloud->new(categories=>@categories); If categories are provided then tags are grouped in separate divisions by category when the HTML fragment is generated. METHODS
add This module adds a tag into the cloud. You pass in the tag name, its URL and its count: $cloud->add($tag1, $url1, $count1); $cloud->add($tag2, $url2, $count2); $cloud->add($tag3, $url3, $count3); add_static This module adds a tag that does not link to another web page into the cloud. You pass in the tag name and its count: $cloud->add_static($tag1, $count1); $cloud->add_static($tag2, $count2); tags($limit) Returns a list of hashrefs representing each tag in the cloud, sorted by alphabet. Each tag has the following keys: name, count, url and level. css This returns the CSS that will format the HTML returned by the html() method with tags which have a high count as larger: my $css = $cloud->css; html($limit) This returns the tag cloud as HTML without the embedded CSS (you should use both css() and html() or simply the html_and_css() method). If any categories were specified when items were being placed in the cloud then the tags will be organized into divisions by category name. If a limit is provided, only the top $limit tags are in the cloud, otherwise all the tags are in the cloud: my $html = $cloud->html(200); html_with_categories($limit) This returns the tag cloud as HTML without the embedded CSS. The tags will be arranged into divisions by category. If a limit is provided, only the top $limit tags are in the cloud. Otherwise, all tags are in the cloud. html_without_categories($limit) This returns the tag cloud as HTML without the embedded CSS. The tags will not be grouped by category if this method is used to generate the HTML. html_and_css($limit) This returns the tag cloud as HTML with embedded CSS. If a limit is provided, only the top $limit tags are in the cloud, otherwise all the tags are in the cloud: my $html_and_css = $cloud->html_and_css(50); AUTHOR
Leon Brocard, "<acme@astray.com>". COPYRIGHT
Copyright (C) 2005-6, Leon Brocard This module is free software; you can redistribute it or modify it under the same terms as Perl itself. perl v5.12.3 2011-06-18 HTML::TagCloud(3pm)
All times are GMT -4. The time now is 04:53 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy