Pattern searching inside Variable - not looking at files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Pattern searching inside Variable - not looking at files
# 1  
Old 06-06-2005
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. Smilie

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! Smilie
# 2  
Old 06-06-2005
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 06:00 PM..
# 3  
Old 06-06-2005
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  
Old 06-07-2005
Thanks, vgersh99.

Not sure what all your statements do, but it does work.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching for pattern in variable using case statements

i would like to search a variable for a pattern, without having make any calls to external tools. i have a code like this: COUNTPRO2="gine is very bad vine is pretty good" case "${COUNTPRO2}" in *vine*) factor=${COUNTPRO2} echo $factor ;; esac If the variable contains... (7 Replies)
Discussion started by: SkySmart
7 Replies

2. UNIX for Dummies Questions & Answers

Searching a pattern in .gz files

in a directory, I have some files(comma seperated) and some .gz files (each .gz file contain one file which again is comma seperated). I want to search and write the names of all those files which contain any particular value (say 1150) at any specified field position(say 10th field). How di I do... (1 Reply)
Discussion started by: Kumar Jivi
1 Replies

3. Shell Programming and Scripting

Searching a particular string pattern in 10000 files

Problem Statement:- I need to search a particular `String Pattern` in around `10000 files` and find the records which contains that `particular pattern`. I can use `grep` here, but it is taking lots of time. Below is the command I am using to search a `particular string pattern` after... (3 Replies)
Discussion started by: raihan26
3 Replies

4. Shell Programming and Scripting

Searching for a string in .PDF files inside .RAR & .ZIP archives.

Hi, I have got a large number of .PDF files that are archived in .RAR & ZIP files in various directories and I would like to search for strings inside the PDF files. I would think you would need something that can recursively read directories, extract the .RAR/.ZIP file in memory, read the... (3 Replies)
Discussion started by: lewk
3 Replies

5. Shell Programming and Scripting

Searching across multiple files if pattern is available in all files searched

I have a list of pattern in a file, I want each of these pattern been searched from 4 files. I was wondering this can be done in SED / AWK. say my 4 files to be searched are > cat f1 abc/x(12) 1 abc/x 3 cde 2 zzz 3 fdf 4 > cat f2 fdf 4 cde 3 abc 2... (6 Replies)
Discussion started by: novice_man
6 Replies

6. Shell Programming and Scripting

Need help in sed command ( Replacing a pattern inside a file with a variable value )

Hello, The following sed command is giving error sed: -e expression #1, char 13: unknown option to `s' The sed command is echo "//-----" | sed "s/\/\/---*/$parChk/g" where parChk="//---ee-" How can i print the variable value from sed command ? And is it possible to replace a... (2 Replies)
Discussion started by: frozensmilz
2 Replies

7. Shell Programming and Scripting

Searching all files that contain pattern

Hello All, i have to search a pattern in all the files in all subfolders that are present in current directory. suppose i am in d1 directory and in that sd1,sd2,sd3 are subdirectories. in sd1 i have files f1,f2 sd2 i have files f3,f4 sd3 i have file f5 i have to list out all those... (4 Replies)
Discussion started by: ravi.sadani19
4 Replies

8. UNIX for Dummies Questions & Answers

searching files inside directory

hey, i need to use grep to search a bunch of header files inside a directory to return which file i can find the function i'm searching for in. how do i use wild cards to search through the files? i can only figure out how to search inside the directory, not inside the files that are in the... (4 Replies)
Discussion started by: kylethesir
4 Replies

9. UNIX for Dummies Questions & Answers

Searching for files with certain string pattern

Hello All I would like to search for files containing certain string pattern under all the directories under /vobs/vobname and print the output to a file in my home directory. How can I do this? Note: /vobs/vobname conatins several directories. Thank You in advance newbetounix (1 Reply)
Discussion started by: intrigue
1 Replies

10. Shell Programming and Scripting

Pattern searching pattern in c files

I have a problem in searching a specific pattern in c files. My requirement: I have to find all the division operator in all cfiles. The problem is, the multi line comments and single line comments will also have forward slash in it. Even after avoiding these comments also, if both... (6 Replies)
Discussion started by: murthybptl
6 Replies
Login or Register to Ask a Question