The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Searching all files that contain pattern ravi.sadani19 Shell Programming and Scripting 4 11-29-2008 06:52 PM
searching files inside directory kylethesir UNIX for Dummies Questions & Answers 4 06-06-2008 01:44 AM
Searching for files with certain string pattern intrigue UNIX for Dummies Questions & Answers 1 02-28-2008 05:28 PM
Pattern searching pattern in c files murthybptl Shell Programming and Scripting 6 11-17-2007 09:15 AM
Regarding Searching Pattern sbasetty Shell Programming and Scripting 2 09-29-2006 05:49 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-06-2005
Rediranch Rediranch is offline
Registered User
  
 

Join Date: Apr 2005
Posts: 3
Pattern searching inside Variable - not looking at files

Hi,

I've searched this site and not found this already, so if I missed on my search, sorry.

I need to pass in a variable to a script, where the first three characters of that variable represent a calendar quarter, and the last 2 characters are the year. I.E. Q0105 for Q1, Q0205 for Q2, and so on. How to do this is not the question.

Inside the script, I need to pattern match on the first three characters to determine what quarter it is (technically I could match on the 3rd char only but it's easier to read and understand if I do all three - but not stuck on that).

The reason is I need to populate three other variables based on the quarter. The other variables represent the months, so for Q0105, I need to make M0105, M0205, M0305. For Q0405 I need to make M1005, M1105, M1205.

So, how do I read just the first three chars that are inside the input variable, to match it up to a quarter?

Thanks!
  #2 (permalink)  
Old 06-06-2005
Rediranch Rediranch is offline
Registered User
  
 

Join Date: Apr 2005
Posts: 3
Of course, as soon as I post this, the guy here at work that I asked, who had no idea, came up with something that worked.....

INPUT_VAL=$1



if [[ ${INPUT_VAL%${INPUT_VAL##Q01}} = "Q01" ]]; then
print "Quarter is 1"
MONTH1="M0105"
MONTH2="M0205"
MONTH3="M0305"
else
if [[ ${INPUT_VAL%${INPUT_VAL##Q02}} = "Q02" ]]; then
print "Quarter is 2"
MONTH1="M0405"
MONTH2="M0505"
MONTH3="M0605"
else
if [[ ${INPUT_VAL%${INPUT_VAL##Q03}} = "Q03" ]]; then
print "Quarter is 3"
MONTH1="M0705"
MONTH2="M0805"
MONTH3="M0905"
else
if [[ ${INPUT_VAL%${INPUT_VAL##Q04}} = "Q04" ]]; then
print "Quarter is 4"
MONTH1="M1005"
MONTH2="M1105"
MONTH3="M1205"
else
print "Input was not in quarter format"
fi
fi
fi
fi

Last edited by Rediranch; 06-06-2005 at 05:00 PM..
  #3 (permalink)  
Old 06-06-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
no input validation - taking the set assumptions

red.sh Q0105
red.sh Q0305

red.sh:
Code:
#!/bin/ksh

typeset -Z2 year=$(echo $1 | sed 's/^.*\(..\)$/\1/')
typeset -i quat=$(echo $1 | sed 's/^Q\(..\).*$/\1/')

typeset -Z2 mon

for i in 1 2 3
do
   mon="$(( i + (quat - 1) * 3 ))"
   eval m${i}="M${mon}${year}"
done

echo "m1->[${m1}]"
echo "m2->[${m2}]"
echo "m3->[${m3}]"
which can be simplified a bit further given ksh-nature of the script - left as an exercise.
  #4 (permalink)  
Old 06-07-2005
Rediranch Rediranch is offline
Registered User
  
 

Join Date: Apr 2005
Posts: 3
Thanks, vgersh99.

Not sure what all your statements do, but it does work.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:04 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0