ksh Multiple Pattern Matching Operators


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh Multiple Pattern Matching Operators
# 1  
Old 04-30-2011
ksh Multiple Pattern Matching Operators

I figured this would be simple, but I am stuck.

Variable longpath="/dir1/dir2/dir3/filename.stuff.morestuff.garbage"
I want to end up with just "filename.extra.moreextra". So, I want to get rid of the path and .garbage
I want to do this with just ksh internals. So, no sed,grep,awk,expr, etc...
I can do it with two operations.
Code:
longpath="/dir1/dir2/dir3/filename.stuff.morestuff.garbage"
a=${longpath##*/}; b=${a%.*} ; echo $b

How can I combine the two operations into one?
# 2  
Old 04-30-2011
In fact, your command is good enough.

Code:
echo "/dir1/dir2/dir3/filename.stuff.morestuff.garbage" |awk -F "[/.]" '{print $(NF-3),$(NF-2),$(NF-1)}' OFS="."

This User Gave Thanks to rdcwayx For This Post:
# 3  
Old 04-30-2011
Hi.

The shell ksh does not allow nesting -- like this:
Code:
#!/usr/bin/env ksh

# @(#) s1	Demonstrate nested parameter expansion in shell.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for i;do printf "%s" "$i";done; printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && . $C

FILE=${1-data1}

pl " Original code:"
longpath="/dir1/dir2/dir3/filename.stuff.morestuff.garbage"
a=${longpath##*/}; b=${a%.*} ; echo $b

pl " Shortened code:"
longpath="/dir1/dir2/dir3/filename.stuff.morestuff.garbage"
echo ${${longpath##*/}%.*}

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.7 (lenny) 
ksh 93s+

-----
 Original code:
filename.stuff.morestuff

-----
 Shortened code:
./s1: line 19: syntax error at line 20: `!' unexpected

and dash, bash shells will not allow nesting, however, zsh will:
Code:
% zsh s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.7 (lenny) 
zsh 4.3.6

-----
 Original code:
filename.stuff.morestuff

-----
 Shortened code:
filename.stuff.morestuff

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 4  
Old 04-30-2011
TYVM rdcwayx and drl.
I guess if I was going to use it enough and I am really 'lana' about using ksh internals, I could just write a function like this.
Code:
strip () {
    read -p "Input: " istring
    nopath=${istring##*/}
    noend=${nopath%.*}
    export ostring=$noend
}
strip $istring
echo $ostring

# 5  
Old 05-01-2011
I think you're doing the right thing by gravitating towards ksh internal commands. This can really speed up your script by avoiding external processes. But you should not worry about using extra lines of code to accomplish this. It is still much faster than the overhead of forking another process. And rememeber that ksh compiles as it runs just like perl. If those two statements are in a loop, they will be read once... not once per iteration.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Group Multiple Lines on SINGLE line matching pattern

Hi Guys, I am trying to format my csv file. When I spool the file using sqlplus the single row output is wrapped on three lines. Somehow I managed to format that file and finally i am trying to make the multiple line on single line. The below command is working fine but I need to pass the... (3 Replies)
Discussion started by: RJSKR28
3 Replies

2. Shell Programming and Scripting

Creating single pattern for matching multiple files.

Hi friends, I have a some files in a directory. for example 856-abc 856-def 851-abc 945-def 956-abc 852-abc i want to display only those files whose name starts with 856* 945* and 851* using a single pattern. i.e 856-abc 856-def 851-abc 945-def the rest of the two files... (2 Replies)
Discussion started by: Little
2 Replies

3. Shell Programming and Scripting

Problems with Multiple Pattern String Matching

I am facing a problem and I would be grateful if you can help me :wall: I have a list of words like And I have a datafile like the box of the box of tissues out of of tissues out of the book, the the book, the pen and the the pen and the I want to find Patterns of “x.*x” where... (2 Replies)
Discussion started by: A-V
2 Replies

4. Shell Programming and Scripting

Help need with PERL multiple search pattern matching!

My example file is as given below: conn=1 uid=oracle conn=2 uid=db2 conn=3 uid=oracle conn=4 uid=hash conn=5 uid=skher conn=6 uid=oracle conn=7 uid=mpalkar conn=8 uid=anarke conn=9 uid=oracle conn=1 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.10.5.6 to 10.18.6.5 conn=2... (3 Replies)
Discussion started by: sags007_99
3 Replies

5. Shell Programming and Scripting

Multiple pattern matching using awk and getting count of lines

Hi , I have a file which has multiple rows of data, i want to match the pattern for two columns and if both conditions satisfied i have to add the counter by 1 and finally print the count value. How to proceed... I tried in this way... awk -F, 'BEGIN {cnt = 0} {if $6 == "VLY278" &&... (6 Replies)
Discussion started by: aemunathan
6 Replies

6. Shell Programming and Scripting

SED multiple pattern matching

Hello sorry for the probably simple question - searching about the forums and Internet, I have not found the answer. Could you tell me please how to do a multiple pattern match with SED So it would be SED searching for "PATTERN1" 'or' "PATTERN2" not 'and' if they happen to fall on the same... (4 Replies)
Discussion started by: lostincashe
4 Replies

7. Shell Programming and Scripting

perl basic multiple pattern matching

Hi everyone, and thank you for your help with this. I am VERY new with perl so all of your help is appreciated. I have tried google but as I don't know the proper terms to search for and could be daunting for a newbie scripter... I know this is very easy for most of you! Thanks! I have a... (4 Replies)
Discussion started by: sinusoid
4 Replies

8. Shell Programming and Scripting

pattern matching over multiple lines and deleting the first

I've got a longish log file with content such as Uplink traffic: Downlink traffic: I want to parse the log file and remove any line that contains the string "Uplink traffic:" at the beginning of the line, but only if the line following it beginnings with the string "Downlink traffic:" (in... (7 Replies)
Discussion started by: Yorkie99
7 Replies

9. Shell Programming and Scripting

Pattern matching in ksh

In the Korn shell is it possible to truncate a pattern like this: - ) ]] So that it is not necessary to enter 8 times? (2 Replies)
Discussion started by: steadyonabix
2 Replies

10. Shell Programming and Scripting

ksh pattern matching

I try to use the pattern substitution operators as documented in the O'Reilley "Learning the Korn Shell" but it doesn't seem to work as advertised. This works all right: var='Regular expressions rules!' $ echo ${var//e/#} R#gular #xpr#ssions rul#s! The docs says that using !(expr)... (5 Replies)
Discussion started by: ripat
5 Replies
Login or Register to Ask a Question