Date substring from a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date substring from a string
# 15  
Old 02-02-2015
Quote:

You seem to be mixing the awk match() and split() functions. But, I get a syntax error when I try this code. The match() function takes two arguments (string to be matched and an ERE to match) and sets various awk variables to indicate whether or not a match was found and, if there was a match, the starting position of the match and the length of the match. The awk script I suggested in post #8 in this thread uses match() this way.

The awk split() function creates an array of values corresponding to values separated by field separators specify by an ERE. With the ERE you use, the desired date would not appear in the array at all since the ERE selects the date as the field separator.
Hi Don,

I`m getting the the following result when I run the code in cygwin

Code:
$ cat tmp
 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
Single digit hour Fri Jan  9  5:50:59 PM CST 2015
Double digit hour Sat Jan 10 11:22:33 AM CST 2015
No space single Fri Jan  9  5:50:59PM CST 2015
No space double Sat Jan 10 11:22:33AM CST 2015

$ awk 'match($0, /([0-9]+):([0-5]?[0-9]):([0-5]?[0-9])/, ary) {print ary[1]":"ary[2]":"ary[3]}' tmp
17:17:33
17:47:33
5:50:59
11:22:33
5:50:59
11:22:33

This User Gave Thanks to senhia83 For This Post:
# 16  
Old 02-02-2015
Hi Don,

From "From the above" i meant Thu Jan 29 15:01:17 CST 2015.
i am using ksh.
timezones are always US timezones(CST or EST).Range of years should be 2010 to 2030.
Yes I am looking for 24 hrs time, no AM or PM.
Well i might have confused with the parse string, the file will contain strating like below:
Code:
a.ksh started at Thu Jan 29 15:01:17 CST 2015
a.ksh ended at Thu Jan 29 15:31:17 CST 2015


Last edited by usrrenny; 02-02-2015 at 05:41 PM.. Reason: missed CODE tag
# 17  
Old 02-03-2015
Quote:
Originally Posted by senhia83
Hi Don,

I`m getting the the following result when I run the code in cygwin

Code:
$ cat tmp
 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
Single digit hour Fri Jan  9  5:50:59 PM CST 2015
Double digit hour Sat Jan 10 11:22:33 AM CST 2015
No space single Fri Jan  9  5:50:59PM CST 2015
No space double Sat Jan 10 11:22:33AM CST 2015

$ awk 'match($0, /([0-9]+):([0-5]?[0-9]):([0-5]?[0-9])/, ary) {print ary[1]":"ary[2]":"ary[3]}' tmp
17:17:33
17:47:33
5:50:59
11:22:33
5:50:59
11:22:33

Thanks for the information. I wasn't aware of this non-standard extension to the match() function in cygwin's awk utility.
# 18  
Old 02-03-2015
Quote:
Originally Posted by Don Cragun
Thanks for the information. I wasn't aware of this non-standard extension to the match() function in cygwin's awk utility.
Isn't Cygwin's awk just GNU awk ?
This User Gave Thanks to Scrutinizer For This Post:
# 19  
Old 02-03-2015
Quote:
Originally Posted by Scrutinizer
Isn't Cygwin's awk just GNU awk ?
It appears that it is. I wasn't aware that gawk had this non-standard extension to POSIX requirements either; but now that I'm looking for it, it is in the gawk(1) man page.
# 20  
Old 02-03-2015
Quote:
Originally Posted by usrrenny
Hi Don,

From "From the above" i meant Thu Jan 29 15:01:17 CST 2015.
i am using ksh.
timezones are always US timezones(CST or EST).Range of years should be 2010 to 2030.
Yes I am looking for 24 hrs time, no AM or PM.
Well i might have confused with the parse string, the file will contain strating like below:
Code:
a.ksh started at Thu Jan 29 15:01:17 CST 2015
a.ksh ended at Thu Jan 29 15:31:17 CST 2015

If you have a file that starts with two lines like those shown above and you just want to extract the start and end times, a ksh function just using ksh built-ins (as suggested by sea) seems to do what you want. If we save the Korn shell script:
Code:
#!/bin/ksh
# SYNOPSIS:
#    gettimes [pathname]
# DESCRIPTION:
#    Get the start and end timestamps from the first two lines in the file named
#    by the pathname operand (default to standard input if no operand is 
#    specified) and write them to standard output separated by a space.
gettimes() {
	# If called with an operand, redirect input from the file named by it...
	if [ $# -gt 0 ]
	then	exec < "$1"
	fi

	# Get start time ($sTIME) from 1st line in input file...
	read _prog _se _time _at _dow _mon _md sTime _TZ _yr

	# Get end time ($eTIME) from 2nd line in input file...
	read _prog _se _time _at _dow _mon _md eTime _TZ _yr

	# Print the found start and end times...
	printf '%s %s\n' "$sTime" "$eTime"
}

# Test function reading standard input passed into this script
times=$(gettimes)
printf 'gettimes function returned "%s"\n' "$times"
stime="${times% *}"
etime="${times#* }"
printf 'Read start time "%s" & end time "%s" from 1st 2 lines from stdin.\n' \
	"$stime" "$etime"

# Test function reading data from a file named "file".
times=$(gettimes file)
printf '\n\ngettimes function returned "%s"\n' "$times"
stime="${times% *}"
etime="${times#* }"
printf 'Read start time "%s" & end time "%s" from 1st 2 lines from "file":\n'  \
	"$stime" "$etime"
head -n 2 file

in a file named tester and make it executable:
Code:
$ chmod +x tester

and invoke it with the command:
Code:
$ printf '%s\n' 'a.sh start time is Tue Feb  2 01:23:45 CST 2015' 'a.sh end time is Tue Feb  2 12:34:56 CST 2015' | ./tester

then we get the output:
Code:
gettimes function returned "01:23:45 12:34:56"
Read start time "01:23:45" & end time "12:34:56" from 1st 2 lines from stdin.


gettimes function returned "17:17:33 17:47:33"
Read start time "17:17:33" & end time "17:47:33" from 1st 2 lines from "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

Which seems to be what you're trying to do.

Although written and tested using the Korn shell, this script will work with any shell that performs standard POSIX shell command substitution and variable expansions (such as ash, bash, dash, and ksh).
# 21  
Old 02-03-2015
Hi All, Thanks a ton for all your messages. I am able to get the start time & end time out of the strings. Now I need to subtract
Code:
total_time=end_time-start_time

. But seems both start_time & end_time are strings, and I am getting result as
Code:
11:31:12-11:01:45

i.e.end_time-start_time

Last edited by Scrutinizer; 02-03-2015 at 03:29 PM.. Reason: Spelling
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting substring within string between 2 token within the string

Hello. First best wishes for everybody. here is the input file ("$INPUT1") contents : BASH_FUNC_message_begin_script%%=() { local -a L_ARRAY; BASH_FUNC_message_debug%%=() { local -a L_ARRAY; BASH_FUNC_message_end_script%%=() { local -a L_ARRAY; BASH_FUNC_message_error%%=() { local... (3 Replies)
Discussion started by: jcdole
3 Replies

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

3. Shell Programming and Scripting

Remove a substring from string

Good morning friends, how can i remove a string with linux scripting from a file? In specific i want to remove from a file all the tweet names and links eg @aerta and links such as http://dst.co/pIiu3i9c Thanx!!! (4 Replies)
Discussion started by: paladinaeon
4 Replies

4. UNIX for Dummies Questions & Answers

substring a date

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. Please use code tags next time for your code and data. (9 Replies)
Discussion started by: lillyt2006
9 Replies

5. Shell Programming and Scripting

How to extract a substring from a string

Hi, I have an input string say for example: ABC,DEF,IJK,LMN,...,XYZ The above string is comma delimited. Now I have to extract the last part after the comma i.e. XYZ. :b: (3 Replies)
Discussion started by: bghosh
3 Replies

6. Shell Programming and Scripting

Help with string and substring also I/O

#!/bin/sh PRINTF=/usr/bin/printf PASSWD=/etc/passwd $PRINTF "Enter a UserID\n" read USERID if ; then $PRINTF "$USERID does not exist, please contact IT service\n" exit 1 fi USERHOME=`grep "^$USERID:" $PASSWD | awk -F : '{print $6}'` USERSHELL=`grep "^$USERID:"... (1 Reply)
Discussion started by: ikeQ
1 Replies

7. Shell Programming and Scripting

get substring from string

Hi All, Problem Description: XML_REP_REQUEST=`CONCSUB "$LOGIN" "SQLAP" "$RESP_NAME" "$USRNM" WAIT="Y" "CONCURRENT" "APPLICATION_SHORT_NAME" "CP_SHORT_NAME"` echo Report Request: $XML_REP_REQUEST --to print value in log file While execution the value of 'XML_REP_REQUEST' is 'Prozess... (5 Replies)
Discussion started by: suman.g
5 Replies

8. UNIX for Dummies Questions & Answers

How to get the substring from the string

Hi All, Can anybody help me to get the substring from the given string. (3 Replies)
Discussion started by: Anshu
3 Replies

9. Shell Programming and Scripting

getting a substring from a string

hi all, I am trying to extract SUBSTRINGS out of a string using ksh. The string is "SAPR3K.FD0.FA.TJ.B0010.T050302" I tried using a= `expr substr $stringZ 1 2` which is giving me a syntax error, donno why?? any ideas why its not working?? I also tried echo "welcome" | awk '{... (3 Replies)
Discussion started by: maradona
3 Replies

10. Programming

can i get a substring from a string?

for example, the string a is "abcdefg", can i get a substring "bcd" (from ato a) from string a? thank you (4 Replies)
Discussion started by: dell9
4 Replies
Login or Register to Ask a Question