New to UNIX ... Date parsing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting New to UNIX ... Date parsing
# 15  
Old 01-22-2013
What code works in native unix but not your script?

What is 'native unix' anyway?

Show your code. We cannot even guess what is happening when we can't see.
# 16  
Old 01-22-2013
Sorry ... when I say "native unix" I mean typing in the commands directly without using a script and executing the script. By the way ... I got this working with the following:

linedate="12/14/2013"
linefmtdate=${linedate:6:4}${linedate:0:2}${linedate:3:2}

My problem now is how do I pad fields. For example if the date inputed is 4/23/2012, how do I turn this to 20120423?
# 17  
Old 01-22-2013
Another way to do it would be:

Code:
VAR="12/14/2013"
IFS="/" read DD MM YYYY <<EOF
$VAR
EOF

NEWVAR=$(sprintf "%04d%02d%02d" $YYYY $MM $DD)

# 18  
Old 01-22-2013
I'm still not getting this ... I'm not sure what IFS is .. The last set of instructions works, but what I really want is:

Read in data
field1 is everything up to first /
field2 is everything up to second /
field3 is everything up to third /
new data is field3field1field2 where field 1 and 2 are padded with 0 to make 2 digits

ie 12/24/2013 would be 20131224
2/10/2012 would be 20120210
1/1/2000 would be 20000101

Is there a simple way to do this?

---------- Post updated at 03:23 PM ---------- Previous update was at 03:22 PM ----------

also .. can any of you recommend a basic unix script programming book?
# 19  
Old 01-22-2013
Quote:
Originally Posted by MJKeeble
I'm still not getting this ... I'm not sure what IFS is .. The last set of instructions works, but what I really want is:

Read in data
field1 is everything up to first /
field2 is everything up to second /
field3 is everything up to third /
new data is field3field1field2 where field 1 and 2 are padded with 0 to make 2 digits
I gave you exactly what you asked for, in pretty much the exact way you wanted it.

IFS is a special variable which controls what the shell splits upon. It's usually space, but you can tell it to split on any character or characters.

So we tell read to split upon / while reading into three different variables -- YYYY, MM, and DD. We feed it that text using a here-document, which acts like file redirection but is right inside the shell. If you wanted to read directly from the keyboard, you can chop of everything from <<EOF to EOF and it will read direct instead of from a variable.

Then we use another shell builtin, printf, to zero-pad the numbers the way you want. It takes a format string, then one or more values after that to fill out the format string.

%d means "integer".
"%04d" means "integer, padded with zeroes up to four digits".

Try typing this into your shell and you'll see what it does:

Code:
printf "%08d\n" 5

So %04d is for the year, and %02d are for the months and days.

And we capture its output in $( ) brackets to turn it into a string variable.

Quote:
also .. can any of you recommend a basic unix script programming book?
The advanced bash scripting guide is a good reference, particularly the reference cards.

Last edited by Corona688; 01-22-2013 at 04:48 PM..
# 20  
Old 01-22-2013
Think I finally got it Smilie Smilie Smilie

Code in shell script

Code:
linedate="2/14/2013"
IFS="/" read mm dd yyyy <<EOF
$linedate
EOF
newvar=$(printf "%04d%02d%02d" $yyyy $mm $dd)
echo $newvar $linedate

Output from echo

Code:
20130214 2/14/2013

Pretty sure it was a case of me not understanding what I was doing ... but it works now. Thank you again for all your help.

Last edited by Scrutinizer; 01-22-2013 at 05:32 PM.. Reason: code tags
This User Gave Thanks to MJKeeble For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

awk command in hp UNIX subtract 30 days automatically from current date without date illegal option

current date command runs well awk -v t="$(date +%Y-%m-%d)" -F "'" '$1 < t' myname.dat subtract 30 days fails awk -v t="$(date --date="-30days" +%Y-%m-%d)" -F "'" '$1 < t' myname.dat awk command in hp unix subtract 30 days automatically from current date without date illegal option error... (20 Replies)
Discussion started by: kmarcus
20 Replies

2. UNIX for Beginners Questions & Answers

Getting the current time from a website and parsing date

I am trying to work on a script to grab the UTC time from a website So far I was able to cobble this together. curl -s --head web-url | grep ^Date: | sed 's/Date: //g' Which gives me the result I need. Wed, 06 Dec 2017 21:43:50 GMT What I need to is extract the 21:43:50 and convert... (4 Replies)
Discussion started by: allisterB
4 Replies

3. UNIX for Dummies Questions & Answers

Really need some help with Parsing files by date

Hi, I am new to Unix and need some help with a problem I have. I have been asked to ftp a file(s) from a directory to another system. I need to be able to allow the current days file to build and only send any other file across that is in the directory. I need a peice of code that will read... (3 Replies)
Discussion started by: darrenwg99
3 Replies

4. Shell Programming and Scripting

Parsing the date output

Hi fellows, I need to define a notification for SSL certificate expiration. My Command output is below: (this is the "Expiration Date") Tue Mar 15 09:30:01 2012 So, at 15th Feb (1 month before the expiration), a notification has to be triggered by a script or sth else. How can i set an... (5 Replies)
Discussion started by: oduth
5 Replies

5. Shell Programming and Scripting

Parsing Log File Based on Date & Error

I'm still up trying to figure this out and it is driving me nuts. I have a log file which has a basic format of this... 2010-10-10 22:25:42 Init block 'UA Deployment Date': Dynamic refresh of repository scope variables has failed. The ODBC function has returned an error. The database... (4 Replies)
Discussion started by: k1ko
4 Replies

6. Shell Programming and Scripting

parsing multi-date text file

Hi all: Trying to parse a log file of rsync activity to get the amount of date being transferred. The log file contains multiple dates and what I am trying to do is get the file sizes for the current date. What I have been trying to do is pipe it through awk but I am having trouble retrieving... (1 Reply)
Discussion started by: raggmopp
1 Replies

7. Shell Programming and Scripting

Compare date from db2 table to yesterday's Unix system date

I am currently running the following Korn shell script which works fine: #!/usr/bin/ksh count=`db2 -x "select count(*) from schema.tablename"` echo "count" I would like to add a "where" clause to the 2nd line that would allow me to get a record count of all the records from schema.tablename... (9 Replies)
Discussion started by: sasaliasim
9 Replies

8. UNIX for Dummies Questions & Answers

Parsing Date to a file name

Hi All There is a file "apple_2008-08-15.log". I have to use grep on this file to collect my test log. There are 45 such files. Is there a command that I can use to dynamically substitute the daily date as part of the file name? As of now Iam renaming the file to the new date and running my... (6 Replies)
Discussion started by: pk_eee
6 Replies

9. Shell Programming and Scripting

Date parsing into string, help!

I have a file that was created yesterday that I want to access... daily.log2008-02-16 I am using... curr_time=`date +"%Y %m %d"` yesterday=`./datecalc.ksh -a $curr_time - 1` the value for yesterday is now "2008 2 16" in ksh, how do I transform the string "2008 2 16" into "2008-02-16" (5 Replies)
Discussion started by: martyb555
5 Replies

10. Shell Programming and Scripting

parsing a system log file via the 'date' command

Hello, I'm trying to update some scripts here that parse our system logs daily. They report information just fine... but they just report too much info. Specifically, if there's been some failed login attempts on several different days (say Monday and Tuesday), when I get the report from... (5 Replies)
Discussion started by: cjones
5 Replies
Login or Register to Ask a Question