Replace integer string in a variable based on month?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Replace integer string in a variable based on month?
# 1  
Old 11-12-2018
Replace integer string in a variable based on month?

Hi Folks -


Linux Version = Linux 2.6.39-400.128.17.el5uek x86_64

I have a process that determines the start and end load periods for an Oracle data load process.

The variables used are as follows follows:

Quote:
_STARTPOV
_ENDPOV
They are populated like such:
Quote:
_STARTPOV=9-18

_ENDPOV=10-18
However, the load requires the month to be the 3 digit format rather than number format. Therefore, to get around it, I am using a cut methodology but I w wondering if I'm over complicating things and there is an easier way?

Code:
GetLoadPOVValues () {
    
    #::-- Usage --::#
        # Param 1 = Required Year digits [i.e. 2 or 4]
        # Param 2 = Number of periods to load [i.e. YTD or 1 or 2 etc]
        
        # Examples are based on a November 2018 run-time
        # Example = GetLoadPOVValues "2" "1" says use 2 digit year [18] and _STARTPOV & _ENDPOV are the same [Oct-2018] since loading only 1 month
        # Example = GetLoadPOVValues "4" "YTD" says use 4 digit year [2018] and _STARTPOV is Jan-2018 and _ENDPOV is Oct-2018
        
    _REGEXP='^[0-9]+$'
    _DIGIT="$1"; [ "${_DIGIT}" -eq "2" ] && _YR="y" || _YR="Y"
    _NUMPD="$2"
    if ! [[ "${_NUMPD}" =~ "${_REGEXP}" ]]
    then
        _STARTPOV="1-$(date +%${_YR})"
    else
        _STARTPOV=`date -d "$(date +%Y-%m-1) -${_NUMPD} month" +%-m-%${_YR}`
    fi
    _ENDPOV=`date -d "$(date +%Y-%m-1) -1 month" +%-m-%${_YR}`
    
    _M="$( cut -d '-' -f 1 <<< "$_ENDPOV" )"; GetMonth "${_M}"; _YR="$( cut -d '-' -f 2 <<< "$_ENDPOV" )"; _ENDPOV="${_M}-${_YR}"
    _M="$( cut -d '-' -f 1 <<< "$_STARTPOV" )"; GetMonth "${_M}"; _YR="$( cut -d '-' -f 2 <<< "$_STARTPOV" )"; _STARTPOV="${_M}-${_YR}"

}

GetMonth () {
    _NUM="$1"
    [[ "${_NUM}" == "01" ]] && _M="Jan" || [[ "${_NUM}" == "1" ]]  && _M="Jan"
    [[ "${_NUM}" == "02" ]] && _M="Feb" || [[ "${_NUM}" == "2" ]]  && _M="Feb"
    [[ "${_NUM}" == "03" ]] && _M="Mar" || [[ "${_NUM}" == "3" ]]  && _M="Mar"
    [[ "${_NUM}" == "04" ]] && _M="Apr" || [[ "${_NUM}" == "4" ]]  && _M="Apr"
    [[ "${_NUM}" == "05" ]] && _M="May" || [[ "${_NUM}" == "5" ]]  && _M="May"
    [[ "${_NUM}" == "06" ]] && _M="Jun" || [[ "${_NUM}" == "6" ]]  && _M="Jun"
    [[ "${_NUM}" == "07" ]] && _M="Jul" || [[ "${_NUM}" == "7" ]]  && _M="Jul"
    [[ "${_NUM}" == "08" ]] && _M="Aug" || [[ "${_NUM}" == "8" ]]  && _M="Aug"
    [[ "${_NUM}" == "09" ]] && _M="Sep" || [[ "${_NUM}" == "9" ]]  && _M="Sep"
    [[ "${_NUM}" == "10" ]] && _M="Oct"
    [[ "${_NUM}" == "11" ]] && _M="Nov"
    [[ "${_NUM}" == "12" ]] && _M="Dec"
}

GetLoadPOVValues "2" "2"

Thank you!!

Last edited by SIMMS7400; 11-12-2018 at 05:18 AM..
# 2  
Old 11-12-2018
Code:
_MONTHS=(nul Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
_M=${_MONTHS[${_STARTPOV%-*}]}
_STARTPOV=${_M}-${_STARTPOV#*-}

Same for _ENDPOV

The 'nul' is there as arrays index from zero, otherwise you will have to subtract one from the index of months.

I don't think you need to worry about a zero prefix as your code seems to be ensuring the month numbers are not padded by zero or space, but if I'm wrong, use this instead:
Code:
_MONTHS=(nul Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
_M=${_MONTHS[10#${_STARTPOV%-*}]}
_STARTPOV=${_M}-${_STARTPOV#*-}

If you want to change your month names depending on your locale, consider using this for your month array:
Code:
_MONTHS=( nul $(locale abmon|tr ';' ' '))

Andrew
# 3  
Old 11-12-2018
Your function could be boiled down to
Code:
GLP()   { IFS=";" T=($(locale abmon))
          IFS=- read EM EY <<< $(date -d "$(date +%Y-%m-1) -1 month" +%-m-%Y)
          IFS=- read SM SY <<< $(date -d "$(date +%Y-%m-1) -$(($2+0?$2:EM)) month" +%-m-%Y)
          _ENDPOV=${T[EM-1]}-${EY:4-$1}
          _STARTPOV=${T[SM-1]}-${SY:4-$1}
        }


$ GLP 4 2
$ echo $_STARTPOV, $_ENDPOV;
Sep-2018, Okt-2018
$ GLP 4 YTD
$ echo $_STARTPOV, $_ENDPOV;
 Jan-2018, Okt-2018
$ GLP 2 YTD
$ echo $_STARTPOV, $_ENDPOV;
Jan-18, Okt-18

You might want to add some error checking, though.
# 4  
Old 11-12-2018
Thank you, Rudi!!

I'm however getting the following errors:


Quote:
./test.sh: line 81: 10 2018: syntax error in expression (error token is "2018")
./test.sh: line 82: 10 2018: syntax error in expression (error token is "2018")
# 5  
Old 11-12-2018
Unfortunately, there is no line 81 nor line 82 in my proposal.
# 6  
Old 11-12-2018
Hi Rudi -

Sorry, these are the line.

Quote:
IFS=- read SM SY <<< $(date -d "$(date +%Y-%m-1) -$(($2+0?$2:EM)) month" +%-m-%Y)
_ENDPOV=${T[EM-1]}-${EY:4-$1}
# 7  
Old 11-12-2018
Hmmm - how do you expect people to debug any code without giving any context? What's your code? How is it called? What's the contents of the local / temporary variables? What's your shell version?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace a string based on input

I am trying to read a value from a mapping file and would need to replace the value based on country parameter source_table_@ctry_final Expected final_var=source_table_aus_final If the country is in nz,usa,uk then final_var=diff_table_nz_final final_var=diff_table_usa_final like that... (10 Replies)
Discussion started by: Master_Mind
10 Replies

2. Shell Programming and Scripting

Replace variable value in first file based on records in second

Hello , I have below files a) File A <?xml version="1.0" encoding="UTF-8" standalone="no"?> <root xmlns="http://aaa/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema" version="2.0"> <project name="source"> <mapping name="m_Source"> <parameter... (3 Replies)
Discussion started by: Pratik4891
3 Replies

3. Shell Programming and Scripting

Find and replace string based on entries on another file

I have a file1 with different with multiple fields and records File2 has 2 fields. I want to find and replace strings in file1 based on file2 values (I Want an exact match i.e. for example: when searching for DT:3, Substr of DT:34 should not be matched) File2: DT:3 foo_err DT:34 bar_frr... (8 Replies)
Discussion started by: aydj
8 Replies

4. Shell Programming and Scripting

Moving files based on size (string to integer)

I have a log file that I want to archive out as it reaches 100MB. I am using the following to get the file size into a variable but get the error "line 5: filesize=$(wc -c < logfile.log) if then echo "is greater than 100M" else echo "is less than 100M" fi I'm sure there's something... (2 Replies)
Discussion started by: Flakman
2 Replies

5. Shell Programming and Scripting

Converting Month into Integer

okay so i run an openssl command to get the date of an expired script. Doing so gives me this: enddate=Jun 26 23:59:59 2012 GMT Then i cut everything out and just leave the month which is "Jun" Now the next part of my script is to tell the user if the the certificate is expired or not... (6 Replies)
Discussion started by: shade917
6 Replies

6. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

7. Shell Programming and Scripting

how to compare string integer with an integer?

hi, how to I do this? i="4.000" if ; then echo "smaller" fi how do I convert the "4.000" to 4? Thanks! (4 Replies)
Discussion started by: h0ujun
4 Replies

8. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

9. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

10. UNIX for Dummies Questions & Answers

Converting a String variable into Integer

Hi, I am passing a variable to a unix function. However when I try to assign the value to another variable like typeset -i I_CACHE_VAL=$2 Is this because of String to Integer conversion? I get an error. Please help me with thsi. Thanks (2 Replies)
Discussion started by: neeto
2 Replies
Login or Register to Ask a Question