Computing.Net > Forums > Unix > File Pattern Matching

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

File Pattern Matching

Reply to Message Icon

Name: navik_pathak
Date: April 20, 2004 at 23:50:18 Pacific
OS: HP-UX
CPU/Ram: 1024
Comment:

Hi All,

I need help regarding file name pattern.

AMK_MFG_RFP_210404.TAB
TJ_MFG_RFP_210404.TAB
JAGUARIUNA_BR_MFG_RFP_210404.TAB
FORT_WORTH_TX_CFC_RFP_210404.TAB
FL_MFG_RFP_210404.TAB


Above is the file name will be in respective directory.
Now my first requirement is to find RFP in the above file name and if RFP is found in the file name then i have to remove date time part from it.

AMK_MFG_RFP.TAB
TJ_MFG_RFP.TAB
JAGUARIUNA_BR_MFG_RFP.TAB
FORT_WORTH_TX_CFC_RFP.TAB
FL_MFG_RFP.TAB


Thanks in advance

Prashant Pathak



Sponsored Link
Ads by Google

Response Number 1
Name: FishMonger
Date: April 21, 2004 at 00:25:53 Pacific
Reply:

Do you want to do it from the command line or script? Which language? When you say, "Now my first requirement is..", does that mean this is a homework assignment?

Here's a portion of one method.
s!(?<=RFP)_\d+!!


0

Response Number 2
Name: Wolfbone
Date: April 21, 2004 at 00:53:52 Pacific
Reply:

Bash version:

[[ ${str/RFP/} != $str ]] && str=${str//[[:digit:]]/}

Bourne version:

[ "`echo $str | tr -d 'RFP'`" = "$str" ] ||
str=`echo "$str" | tr -d '[:digit:]'`

both examples will strip out ANY digits from a string $str containing "RFP" which is OK for the examples you gave but not if there are other digits in the filename string.


0

Response Number 3
Name: Wolfbone
Date: April 21, 2004 at 01:05:09 Pacific
Reply:

Bah! - remove the extra underscore yourself ;)


0

Response Number 4
Name: Wolfbone
Date: April 21, 2004 at 01:32:18 Pacific
Reply:

Double Bah! - replace tr -d 'RFP' in the 2nd version with sed 's/RFP//'


0

Response Number 5
Name: navik_pathak
Date: April 21, 2004 at 03:29:18 Pacific
Reply:

Hi Wolford,

First Of all i am using K Shell.

I am doing this thing in Shell script not at command line

Prashant Pathak


0

Related Posts

See More



Response Number 6
Name: Wolfbone
Date: April 21, 2004 at 04:48:52 Pacific
Reply:

Well Hi Prashant :) - It won't make a great deal of difference if it's in a script or at the command line.

I'm not very familiar with the Korn but a quick look at the manual seems to indicate that the (amended) 2nd version may work in that environment too.


0

Response Number 7
Name: Wolfbone
Date: April 21, 2004 at 05:23:40 Pacific
Reply:

In other words, if in your shellscript you've got the file name into the variable filename, this line should transform it according to your rules:

[ "`echo $filename | tr -d 'RFP'`" = "$filename" ] || filename=`echo "$filename" | sed 's/_[0-9]\{6\}//'`


0

Response Number 8
Name: Wolfbone
Date: April 21, 2004 at 05:27:04 Pacific
Reply:

Doh! - I've been awake to long! - replace the tr -d 'RFP' bit with sed 's/RFP//'


0

Sponsored Link
Ads by Google
Reply to Message Icon

Unix Script running awt program on un...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: File Pattern Matching

Script to delete lines from a file www.computing.net/answers/unix/script-to-delete-lines-from-a-file/7408.html

deleting rows in a file www.computing.net/answers/unix/deleting-rows-in-a-file/3676.html

deleting files older than 2 days www.computing.net/answers/unix/deleting-files-older-than-2-days/4394.html