substring a date


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers substring a date
# 1  
Old 09-26-2012
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 12:48 PM..
# 2  
Old 09-26-2012
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  
Old 09-26-2012
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 01:31 PM..
# 4  
Old 09-26-2012
Possibly you are using KSH88 or PDKSH, which do not have substrings.
# 5  
Old 09-26-2012
Try replacing ksh with ksh93, in your script. That aside, the substring should start with 4 and not 5.
# 6  
Old 09-26-2012
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  
Old 09-26-2012
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

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. Shell Programming and Scripting

Get the substring

Hi All, I have a ouput string likes 'u8wos' or 'u10acsd' or somthing else 'u{number}{any characters}'and I want to get the number behind the letter 'u' by bash shell. Thanks Damon (11 Replies)
Discussion started by: Damon_Qu
11 Replies

7. UNIX for Dummies Questions & Answers

Substring

Hi I use the below cmd to get the list of files that are modified than <temp> file in the <path> diretory cmd:find <path> -name '*.zip' -type f -newer <temp> -print i am getting all the list of files that are new or modified, with abs path, i want to copy all of these files to a... (3 Replies)
Discussion started by: Naveen_5960
3 Replies

8. 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

9. 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

10. UNIX for Dummies Questions & Answers

substring

Hi, I've got a UNIX-script in which a variable 'name' is used. This variable is filled with a filename (e.g. file.tst). Now I want to search for files which start with the same name, but without the extension, e.g. file_test. Is there a way of doing this, using something like substring or... (4 Replies)
Discussion started by: Anika
4 Replies
Login or Register to Ask a Question