substring a date


 
Thread Tools Search this Thread
# 1  
substring a date

Hello

I am trying to substring the month in a date string
I am getting an error " bad substitution"

Code:
#!/bin/ksh
INPUT='20121225'
echo ${$INPUT:5:2}
exit 0

Very new in unix.

Thanks.

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by radoulov; 09-26-2012 at 01:48 PM..
# 2  
Quote:
Originally Posted by lillyt2006
Hello

I am trying to substring the month in a date string
I am getting an error " bad substitution"

#!/bin/ksh
INPUT='20121225'
echo ${INPUT:5:2}
exit 0

Very new in unix.

Thanks.
Try the above. You have one extra $ in cmd substitution.
# 3  
I get the same error 'bad substitution'

Code:
#!/bin/ksh
INPUT='20121225'
echo ${INPUT:5:2}
exit 0

Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.

Last edited by Corona688; 09-26-2012 at 02:31 PM..
# 4  
Possibly you are using KSH88 or PDKSH, which do not have substrings.
# 5  
Try replacing ksh with ksh93, in your script. That aside, the substring should start with 4 and not 5.
# 6  
I tried replacing ksh with ksh93 and I get "ksh: test2.sh: not found"
(Thanks for the substring position correction)

I was looking into awk but I'm not sure if it looks by string position.

---------- Post updated at 05:06 PM ---------- Previous update was at 04:04 PM ----------

I finally got it after 1 hour.

INPUT='20121225'
echo $INPUT | awk '{print substr($0,5,2)}'
# 7  
No matter what version of ksh or bash you have, the following should work:
Code:
md=${INPUT#????}
m=${md%??}
echo $m

$md will be the month and day, $m will just be the month.
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #87
Difficulty: Easy
Windows 7 added PowerShell, a more powerful command-line shell and scripting language than the Command Prompt.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date: invalid date trying to set Linux date in specific format

i try to set linux date & time in specific format but it keep giving me error Example : date "+%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" or date +"%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" keep giving me this error : date: invalid date 19-01-2017 00:05:01 Please use CODE tags... (7 Replies)
Discussion started by: umen
7 Replies

2. Shell Programming and Scripting

Date substring from a string

Hi, I have 2 statements in a file a.sh start time is Fri Jan 9 17:17:33 CST 2015 a.sh end time is Fri Jan 9 17:47:33 CST 2015 I am required to get only the time out of it. like 17:17:33 & 17:47:33 PLs suggest (21 Replies)
Discussion started by: usrrenny
21 Replies

3. UNIX for Dummies Questions & Answers

Get date, change format and substring?

I'm somewhat new to shell scripting and I have a file that had a date in it. /somedirectory/datefile.txt I want to take the date in the file: 2013-06-12 and change the formate to 20130612 and have it as a variable/parm I also what to take that date 2013-06-12 and substring it. I know... (3 Replies)
Discussion started by: MJCreations
3 Replies

4. Shell Programming and Scripting

Converting a date to friday date and finding Min/Max date

Dear all, I have 2 questions. I have a file with many rows which has date of the format YYYYMMDD. 1. I need to change the date to that weeks friday date(Ex: 20120716(monday) to 20120720). Satuday/Sunday has to be changed to next week friday date too. 2. After converting the date to... (10 Replies)
Discussion started by: 2001.arun
10 Replies

5. Shell Programming and Scripting

Date One Week Ago From Given Date, Not From Current Date

Hi all, I've used various scripts in the past to work out the date last week from the current date, however I now have a need to work out the date 1 week from a given date. So for example, if I have a date of the 23rd July 2010, I would like a script that can work out that one week back was... (4 Replies)
Discussion started by: Donkey25
4 Replies

6. UNIX for Dummies Questions & Answers

Getting Substring

Hi, I hav a string lets say aa.txt:bb:txt length of the string can vary.. I have to keep the token inside a array and the delimiter is : plz send me the code (2 Replies)
Discussion started by: Deekay.p
2 Replies

7. Shell Programming and Scripting

Again Substring!!!! Help

Hi, To remove the date and time from the below data which is in a file abc.txt 29 Jul 2009 04:36:53,956 ERROR 1 Error with Java 29 Jul 2009 04:36:58,335 ERROR 2 29 Jul 2009 05:37:24,746 ERROR 3 I want the output as ERROR 1 Error with Java ERROR 2 ERROR 3 As, In the above... (2 Replies)
Discussion started by: Pank10
2 Replies

8. Shell Programming and Scripting

How do I Substring ??

Hello everyone. I'm writing a script in UNIX. The purpose is to get the second character from a variable that stores the system year. This is the code: unix_year_yy=`date "+%g"` This will return "07" in variable unix_year_yy. How can I get the second character (7)?? (6 Replies)
Discussion started by: Rigger
6 Replies

9. Shell Programming and Scripting

how to get substring

i have a strings abc-def.csv ghi-jkl.csv i want to make it as abc-*-def.xyz ghi-*-jkl.xyz How to do it?. (5 Replies)
Discussion started by: senthilk615
5 Replies

10. Shell Programming and Scripting

Getting a substring

This is probably pretty simple butI'm not sure how to best go about it. If I have FILE="myBigLongFileName_1.xls" FILE_PREFIX=`echo $FILE| cut -d"." -f1` # that gives "myBigLongFileName_1" All i want to do now is chop the "_1" from the end of $FILE_PREFIX Any ideas anyone? (3 Replies)
Discussion started by: djkane
3 Replies

Featured Tech Videos