LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-28-2014, 07:17 PM   #1
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Rep: Reputation: 46
Date conversion issues


I am having issues getting dates converted. Note that I am not a programmer. Please don't laugh at what I have. It is the best I can do with copy/paste. A friend of mine wrote what I copied off for me for the start of the project. But I hate to keep bugging him with my small issues.

The first part of the script that is failing.

It looks like this
Code:
# Tempoary Variables
BDay=17
BMonth=09
BYear=1956

#step 1 convert civil birthday to hebrew birthday

HBDate=`hdate -q ${BDay} ${BMonth} ${BYear}`
# gives 
#  "a blank line"
#Friday, 17 September 1965, 20 Elul 5725
echo " #1 $HBDate"


#step 2 seperate output from above into
# HBDay = Hebrew Birth Day
# HMonth = Hebrew Birth Month
# HBYear = Hebrew Birth Year

HBlocData=`echo ${HBDate} | awk -F"," '{printf("%s",$3)}'`
HBDay=`echo ${HBlocData} | awk -F"," '{printf("%s",$1)}' | sed s/\ //g`
  if [ `echo ${HBlocData} | awk NF = 4` ]; then
    HMonth=`echo ${HBlocData} | awk -F"," '{printf("%s",$2 $3)}' | sed s/\ //g`;
    HBYear=`echo ${HBlocData} | awk -F"," '{printf("%s",$4)}' | sed s/\ //g`;
  elif [ `echo ${HBlocData} | awk NF = 3` ]; then
    HMonth=`echo ${HBlocData} | awk -F"," '{printf("%s",$2)}' | sed s/\ //g`;
    HBYear=`echo ${HBlocData} | awk -F"," '{printf("%s",$3)}' | sed s/\ //g`;
  fi    
echo "#2 $HBDay"
echo "$HMonth"
echo "$HByear"
My guess is that the awk/sed part is not working the way I want. I tried to change it to what I needed but clearly it is not right.

It fails with
Code:
awk: fatal: cannot open file `=' for reading (No such file or directory)
I tried adding a = with no joy

Thanks
 
Old 09-28-2014, 07:25 PM   #2
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Original Poster
Rep: Reputation: 46
Thinking about it I thought I saw the problem and changed the line to

if [ ` ${NF} = 4` ]; then

but still no joy
 
Old 09-28-2014, 09:06 PM   #3
Smokey_justme
Member
 
Registered: Oct 2009
Distribution: Slackware
Posts: 534

Rep: Reputation: 203Reputation: 203Reputation: 203
replace this:
Code:
if [ `echo ${HBlocData} | awk NF = 4` ]; then
with this:
Code:
if [ `echo ${HBlocData} | awk 'NF = 4'` ]; then

Then do the same for elif line

P.S. I did not actually ran your code, just saw this according to the error provided.. It may very well be very wrong..
 
Old 09-28-2014, 10:49 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
One more change:

Code:
if [ `echo ${HBlocData} | awk 'NF == 4'` ]; then
For awk, = is assignemnt, == is a test for equality.
 
Old 09-29-2014, 05:23 AM   #5
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Original Poster
Rep: Reputation: 46
Thanks Smokey_justme and ntubski

That got me past that problem. I had tried several things very similar to that but none just right.

Now the next line(s) that are suppose to populate some other variables are not doing what they are suppose to. Big surprise, Not.

Code:
HBlocData=`echo ${HBDate} | awk -F"," '{printf("%s",$3)}'`
#echo $HBlocData | awk '{printf (NF)}';
#echo "$NF"
echo "$HBlocData"
  if [ `echo $HBlocData} | awk 'NF  ==  4'` ]; then
    HBDay=`echo ${HBlocData} | awk '{printf("%s",$1)}' | sed s/\"//g`;
    HMonth=`echo ${HBlocData} | awk -F"," '{printf("%s",$2 $3)}' | sed s/\"//g` ;
    HBYear=`echo ${HBlocData} | awk -F"," '{printf("%s",$4)}' | sed s/\"//g` ;
  elif [ `echo $HBlocData} | awk 'NF  ==  3'` ]; then
    HBDay=`echo ${HBlocData} | awk -F"," '{printf("%s",$1)}' | sed s/\"//g` ;
    HMonth=`echo ${HBlocData} | awk -F"," '{printf("%s",$2)}' | sed s/\"//g` ;
    HBYear=`echo ${HBlocData} | awk -F"," '{printf("%s",$3)}' | sed s/\"//g` ;
  fi    
echo "#2 $HBDay"
echo "$HMonth"
echo "$HByear"
The elif line is 30 when I run it I get

Code:
 12 Tishrei 5717
++ echo 12 Tishrei '5717}'
++ awk 'NF  ==  4'
+ '[' ']'
++ echo 12 Tishrei '5717}'
++ awk 'NF  ==  3'
+ '[' 12 Tishrei '5717}' ']'
./blessingStart.sh: line 30: [: Tishrei: binary operator expected
+ echo '#2 '
#2 
+ echo ''

+ echo ''

+ set +x
Invalid Month
I gather that my code is expecting a number not a word and it does not know how to deal with that. The reason for the test is that on a leap year we have Adar I and Adar II.

I have tried removing the "sed" part with no joy. Even the day variable is not being populated. I have tried adding and removing the " in the sed line. no Joy.
 
Old 09-29-2014, 05:35 AM   #6
Smokey_justme
Member
 
Registered: Oct 2009
Distribution: Slackware
Posts: 534

Rep: Reputation: 203Reputation: 203Reputation: 203
First of all, fix the lines .. you hava $HBlocData} when you should have ${HBlocData}... Pay close attention to symbols on your output... It's normal to have typos, but the output will usually point them out..

Second, please provide the full script at any point you have problems since, for example, right now the output indicates some weird quotes.. And if we decide to take a closer look at your script we would like and probably be required to run it locally to figure out what the problem is..

Last edited by Smokey_justme; 09-29-2014 at 05:38 AM.
 
Old 09-29-2014, 05:49 AM   #7
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Original Poster
Rep: Reputation: 46
Thanks


The code with the typo's fixed.

Code:
#!/bin/bash

# Tempoary Variables
BDay=17
BMonth=09
BYear=1956

#step 1 convert civil birthday to hebrew birthday

HBDate=`hdate -q ${BDay} ${BMonth} ${BYear}`
# gives 
#  "a blank line"
#Friday, 17 September 1965, 20 Elul 5725
echo " #1 $HBDate"


#step 2 seperate output from above into
# HBDay = Hebrew Birth Day
# HMonth = Hebrew Birth Month
# HBYear = Hebrew Birth Year
set -x
HBlocData=`echo ${HBDate} | awk -F"," '{printf("%s",$3)}'`
#echo $HBlocData | awk '{printf (NF)}';
#echo "$NF"
echo "$HBlocData"
  if [ `echo ${HBlocData} | awk 'NF  ==  4'` ]; then
    HBDay=`echo ${HBlocData} | awk '{printf("%s",$1)}' | sed s/\"//g`;
    HMonth=`echo ${HBlocData} | awk -F"," '{printf("%s",$2 $3)}' | sed s/\"//g` ;
    HBYear=`echo ${HBlocData} | awk -F"," '{printf("%s",$4)}' | sed s/\"//g` ;
  elif [ `echo ${HBlocData} | awk 'NF  ==  3'` ]; then
    HBDay=`echo ${HBlocData} | awk -F"," '{printf("%s",$1)}' | sed s/\"//g` ;
    HMonth=`echo ${HBlocData} | awk -F"," '{printf("%s",$2)}' | sed s/\"//g` ;
    HBYear=`echo ${HBlocData} | awk -F"," '{printf("%s",$3)}' | sed s/\"//g` ;
  fi    
echo "#2 $HBDay"
echo "$HMonth"
echo "$HByear"
set +x

#step 3 populate HBMonth
case ${HMonth} in
         Nisan) 
           HBMonth="7"
           ;;
         Iyar)
           HBMonth="8"
           ;;
         Sivan)
           HBMonth="9"
           ;;
         Tammuz)
           HBMonth="10"
           ;;
         Av)
           HBMonth="11"
           ;;
         Elul)
           HBMonth="12"
           ;;
         Tishrei)
           HBMonth="1"
           ;;
         Cheshvan)
	   HBMonth="2"
           ;;
         Kislev)
           HBMonth="3"
           ;;
         Tevet)
           HBMonth="4"
           ;;
         "Sh'vat")
           HBMonth="5"
           ;;
         Adar)
           HBMonth="6"
           ;;
         "Adar I")
           HBMonth="13"
           ;;
         "Adar II")
           HBMonth="14"
           ;;
         *)
           echo "Invalid Month"
           exit
           ;;
        esac

echo "#3 $HBDay $HBMonth $HByear"        
        
#step 4 Get this hebrew year
# CHYear = Current Hebrew Year


HBCDate=`hdate -q`
HBDateData=`echo ${HBCDate} | awk -F"," '{printf("%s",$3)}'`

  if [ `echo ${HBDateData} | awk 'NF == 4'` ] ; then
    CHYear=`echo ${HBDateData} | awk -F"," '{printf("%s",$4)}' | sed s/\ //g`;
  elif [ `echo ${HBDateData} | awk 'NF == 3'` ] ; then
    CHYear=`echo ${HBDateData} | awk -F"," '{printf("%s",$3)}' | sed s/\ //g`;
fi
    
echo "#4 $CHYear"    
    
#step 5 Get this years Hebrew Birthaday Gegorian calander day
#SNDate=`hdate -qT ${HBDay} ${HBMonth} ${CHYear}`
        
CSNDate=`hdate -q ${HBDay} ${HBMonth} ${CHYear}`

CSNData=`echo ${CSNDate} | awk -F"," '{printf("%s",$2)}'`

    SNDay=`echo ${CSNData} | awk -F"," '{printf("%s",$1)}' | sed s/\ //g`
    SNMonth=`echo ${CSNData} | awk -F"," '{printf("%s",$2)}' | sed s/\ //g`;
    SNYear=`echo ${CSNData} | awk -F"," '{printf("%s",$3)}' | sed s/\ //g`;


echo "#5 ${SNDay} ${SNMonth} ${SNYear}"

exit 0
 
Old 09-29-2014, 12:31 PM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by rbees View Post
./blessingStart.sh: line 30: [: Tishrei: binary operator expected
You were trying to use the "[" command to check if awk's output was non-empty, but the output was split into multiple arguments which "[" can't handle. Use quotes to avoid word splitting:

Code:
if [ -n "`echo ${HBlocData} | awk 'NF  ==  4'`" ]; then
The -n option tells "[" to test for non-empty string (that's what it does when given a single string, but it's usually better to be explicit).
 
Old 09-29-2014, 10:03 PM   #9
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Original Poster
Rep: Reputation: 46
I did finally figure out the awk not sed issues I was having.

Thanks

I am afraid that there will still be issues with the case esac and converting the month name to a number. Will have to do some error checking.

Code:
#!/bin/bash

# Tempoary Variables
BDay=17
BMonth=09
BYear=1956

#step 1 convert civil birthday to hebrew birthday

HBDate=`hdate -q ${BDay} ${BMonth} ${BYear}`
# gives 
#  "a blank line"
#Friday, 17 September 1965, 20 Elul 5725
echo " #1 $HBDate"


#step 2 seperate output from above into
# HBDay = Hebrew Birth Day
# HMonth = Hebrew Birth Month
# HBYear = Hebrew Birth Year

HBlocData=`echo ${HBDate} | awk -F"," '{printf("%s",$3)}'`
#echo $HBlocData | awk '{printf (NF)}';
#echo "$NF"

echo "$HBlocData"
  if [ -n "`echo ${HBlocData} | awk 'NF  ==  4'`" ]; then
    HBDay=`echo ${HBlocData} | awk -F" "  '{printf("%s",$1)}'`;
    HMonth=`echo ${HBlocData} | awk -F" " '{printf("%s",$2 $3)}'` ;
    HBYear=`echo ${HBlocData} | awk -F" " '{printf("%s",$4)}'` ;
  elif [ -n "`echo ${HBlocData} | awk 'NF  ==  3'`" ]; then
    HBDay=`echo ${HBlocData} | awk -F" " '{printf("%s",$1)}'` ;
    HMonth=`echo ${HBlocData} | awk -F" " '{printf("%s",$2)}'` ;
    HBYear=`echo ${HBlocData} | awk -F" " '{printf("%s",$3)}'` ;
  fi    
echo "#2 $HBDay"
echo "$HMonth"
echo "$HBYear"

#step 3 populate HBMonth
case ${HMonth} in
         Nisan) 
           HBMonth="7"
           ;;
         Iyar)
           HBMonth="8"
           ;;
         Sivan)
           HBMonth="9"
           ;;
         Tammuz)
           HBMonth="10"
           ;;
         Av)
           HBMonth="11"
           ;;
         Elul)
           HBMonth="12"
           ;;
         Tishrei)
           HBMonth="1"
           ;;
         Cheshvan)
	   HBMonth="2"
           ;;
         Kislev)
           HBMonth="3"
           ;;
         Tevet)
           HBMonth="4"
           ;;
         "Sh'vat")
           HBMonth="5"
           ;;
         Adar)
           HBMonth="6"
           ;;
         "Adar I")
           HBMonth="13"
           ;;
         "Adar II")
           HBMonth="14"
           ;;
         *)
           echo "Invalid Month"
           exit
           ;;
        esac

echo "#3 $HBDay $HBMonth $HBYear"        
        
#step 4 Get this hebrew year
# CHYear = Current Hebrew Year

HBCDate=`hdate -q --not-sunset-aware`

HBDateData=`echo ${HBCDate} | awk -F"," '{printf("%s",$3)}'`
echo "HBDateData"

  if [ -n "`echo ${HBDateData} | awk 'NF  ==  4'`" ] ; then
    CHYear=`echo ${HBDateData} | awk -F" " '{printf("%s",$4)}'`;
  elif [ -n "`echo ${HBDateData} | awk 'NF  ==  3'`" ] ; then
    CHYear=`echo ${HBDateData} | awk -F" " '{printf("%s",$3)}'`;
  fi
    
echo "#4 $CHYear"    

#step 5 Get this years Hebrew Birthaday Gegorian calander day
#SNDate=`hdate -qT ${HBDay} ${HBMonth} ${CHYear}`
        
CSNDate=`hdate -q ${HBDay} ${HBMonth} ${CHYear}`

CSNData=`echo ${CSNDate} | awk -F"," '{printf("%s",$2)}'`

    SNDay=`echo ${CSNData} | awk -F"," '{printf("%s",$1)}' | sed s/\ //g`
    SNMonth=`echo ${CSNData} | awk -F"," '{printf("%s",$2)}' | sed s/\ //g`;
    SNYear=`echo ${CSNData} | awk -F"," '{printf("%s",$3)}' | sed s/\ //g`;


echo "#5 ${SNDay} ${SNMonth} ${SNYear}"

exit 0
 
Old 09-30-2014, 03:10 PM   #10
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Original Poster
Rep: Reputation: 46
Thanks for the help so far.

I think this is the last problem with this part of the script.

After changing the temporary variables at the beginning of the script to get a leap-year using
Code:
# Tempoary Variables
BDay=17
BMonth=2
BYear=2016
I get a fail with

Code:
v$ ./blessingStart.sh
 #1 
Wednesday, 17 February 2016, 8 Adar I 5776
 8 Adar I 5776
#2 8
AdarI
5776
+ case ${HMonth} in
+ echo 'Invalid Month'
Invalid Month
+ exit
As you can see if falls trough the test for "Adar I" and "Adar II" to *) invalid....

Can anybody point me at how to fix this without using if;then ?
 
Old 09-30-2014, 05:13 PM   #11
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Original Poster
Rep: Reputation: 46
Not having a lot of luck here.

I have tried putting a case inside the case that populates $HBMonth but no joy.

I really think that I need an if-then and not a case. But I have been told not to do that.
 
Old 09-30-2014, 05:20 PM   #12
Smokey_justme
Member
 
Registered: Oct 2009
Distribution: Slackware
Posts: 534

Rep: Reputation: 203Reputation: 203Reputation: 203
Your problem is here:
Code:
  HMonth=`echo ${HBlocData} | awk -F" " '{printf("%s",$2 $3)}'` ;
You should replace this with something like
Code:
  HMonth=`echo ${HBlocData} | awk -F" " '{printf("%s %s",$2,$3)}'` ;
 
Old 10-02-2014, 04:20 PM   #13
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Original Poster
Rep: Reputation: 46
Thanks Smokey_justme

That was just what I needed.

Thanks
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary number to date conversion musings. geelsu General 3 06-23-2011 01:29 PM
[SOLVED] date format conversion iddarth.85 Linux - Newbie 3 04-29-2011 03:36 AM
Date Conversion : Month Name --> Number onesikgypo Programming 1 04-28-2009 11:26 AM
Date command conversion jefn Linux - Newbie 4 04-27-2009 04:43 AM
perl date conversion AndersT Programming 1 03-18-2009 04:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration