Korn Shell regular pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Korn Shell regular pattern
# 1  
Old 12-23-2011
Korn Shell regular pattern

Hello,

I can't seem to understand korn shell regular expression. I am trying to extract the tagfrom its own filename string.

var="LNX_1.2.0.0.af329a3da.tar"

whereby af329a3da is the tagI am trying to extract out from.

I am trying to avoid using IFS because future modifications would require me to identify the file by

(PLATFORM)_(VERSION).(TAG).tar


Any insights?

${var##/*/}
$(var%*}

doesn't really help.
# 2  
Old 12-23-2011
Some possibilities:
Code:
tag=$(echo $var | sed 's/.*\.\(.*\)\..*/\1/')

tag=$(echo $var | awk -F"." '{print $(NF-1)}')

# 3  
Old 12-23-2011
Code:
var="LNX_1.2.0.0.af329a3da.tar"
t=${var%.*}
tag=${t##*.}

# 4  
Old 12-23-2011
With expr

Code:
expr $var : '.*\.\(.*\)\..*'

This User Gave Thanks to clx For This Post:
# 5  
Old 12-23-2011
Perl.
Code:
echo $var | perl -e 'chomp($x=<>);$t=(split /\./,$x)[-2];print $t'

# 6  
Old 01-31-2012
Thank you

---------- Post updated at 01:06 AM ---------- Previous update was at 01:05 AM ----------

Code:
expr $var : '.*\.\(.*\)\..*'

works very well for my case. Smilie

Last edited by Franklin52; 01-31-2012 at 03:17 AM.. Reason: Please use code tags for code and data samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

On korn shell, how to share history between regular user and root?

I'm exploring OpenBSD and want to stick to its default shell, which is ksh. My goal is for my regular user ("bruno") and root user to have a shared history file. However, it seems that when running as root, ksh refuses to write to a HISTFILE that is owned by non-root user. This illustrates the... (3 Replies)
Discussion started by: DevuanFan
3 Replies

2. Shell Programming and Scripting

Regular Expression repeat pattern

Hi, I'm struggling with very very simple task but dont know where I'm going wrong. Have the following file numbers.txt 1 12 123 1234 12345 123456 1234567 12345678 123456789 1234567890 9876543210 987654321 98765432 9876543 987654 98765 (1 Reply)
Discussion started by: bobbygsk
1 Replies

3. Shell Programming and Scripting

Pattern search (regular expression in UNIX)

Hello , Could anyone help me to define the string in regular expression way . Below is my string \rtf1\ansi\deff0{\fonttbl{\f0\fswiss Helv;}{\f1\fnil MS Sans Serif;}} {\colortbl ;\red0\green0\blue0;} \viewkind4\uc1\pard\cf1\lang1033\f0\fs16 The string will always start as \rtf1 and... (6 Replies)
Discussion started by: Pratik4891
6 Replies

4. Shell Programming and Scripting

Korn Shell for pattern matching and extracting

Guys, i'm new to shell scripting. Here's what i need. I need a shell script which would read a file containing only 1 line which never changes. File containts - SQL_Mgd_Svc_ELONMCL54496 |EMEA\brookkev, EMEA\fieldgra, EMEA\tidmamar, EMEA\attfiste, EMEA\baldogar, EMEA\clarkia2, EMEA\conwasha,... (9 Replies)
Discussion started by: butterfly20
9 Replies

5. Shell Programming and Scripting

Regular Expression for Random pattern

What would be the regular expression that can search for a Pattern, having 8 characters out of which atleast 1 digit, 1 lower case, 1 upper case letter and 1 special character must be there. But these can occur at any place randomly. Please help me out. I'm using find $dir -name "*.txt" -exec... (0 Replies)
Discussion started by: Pradeep Kr.
0 Replies

6. Shell Programming and Scripting

Validate time pattern using regular expression

Hi, I am new to scripting. please help me in validating the user entered time Pattern Here is the program #!/bin/bash validateTimeFormat() { checkTime=$1 timePattern="::" if ] then echo "Valid time pattern" return 1 else echo "InValid time pattern" return -1 fi } echo "Please... (2 Replies)
Discussion started by: vvenu88
2 Replies

7. Shell Programming and Scripting

validate date pattern using Regular Expression

Hi, i am java guy and new to unix. I want to validate date pattern using Regex expression here is the sample program i have written. #!/bin/sh checkDate="2010-04-09" regex="\\d{4}-\\d{2}-\\d{2}\$" echo $regex if ] then echo "OK" else echo "not OK" fi But the ouput is... (2 Replies)
Discussion started by: vvenu88
2 Replies

8. Shell Programming and Scripting

Regular Expression to exclude pattern

Hi All I am using regular expressions to determine how to group certain data. I've included an example of the data below. USD_SPTR_2Y_725.5_PUT_EUROPEAN_09Q1|USD||European| CAD_NDX_10Yx1Y_5.5_PUT_EUROPEAN_09Q1|CAD||European| The regular expressions I am using is as follows and this is... (5 Replies)
Discussion started by: kingpin2502
5 Replies

9. Shell Programming and Scripting

Pattern manipulation in korn shell script using sed.

Hi, Could any one let me know, how can I cut the last field in below mentioned line. net,-hopcount,0,-netmask,255.255.255.0,,,,,192.168.37.0,10.253.0.1 net,-hopcount,0,-netmask,255.255.255.0,,,,,192.168.1.0,10.253.0.1 net,-hopcount,0,-netmask,255.255.255.0,,,,,192.168.38.0,10.253.0.1... (3 Replies)
Discussion started by: ajilesh
3 Replies

10. Shell Programming and Scripting

Korn pattern-list with a variable

I am trying to use a pattern-list match in korn shell using a variable and it always seems to regard the pattern-list as a literal: Using the directory names explicitly in the pattern-list works fine: ls @(test|test1)/test.txt and returns: test/test.txt Trying to use a variable for this... (2 Replies)
Discussion started by: partchimp
2 Replies
Login or Register to Ask a Question