date-extraction from a file in KSH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting date-extraction from a file in KSH
# 1  
Old 04-19-2006
date-extraction from a file in KSH

Hi, everyone,
Now I have a ".csv" file and I want to extract a string-like date value from the file, say, it is in the 2nd row of the file and it is in the fixed context like:
"
This is the file title, //Row 1
The lastest update happened on: 10-Mar-2006 //Row2
....//other irrelavent rows
"
I want to use KSH script to extract this value "10-Mar-2006", and also, I will need to write this value to a certain fixed place in another file,say,"updateRecord.txt", and it is in the context like:
"
This file is to record the lastest update time, //row 1
The latest update time is: _________ (I want to put the value extracted from source file to be here) //row2
//.....other rows
"
Anyone can provide any tips for me? Thanks a lot for ur help

Rgds
HN
# 2  
Old 04-19-2006
Data Nobody gives any tips?

come on, anyone who knows, pls help
Any kind of share of ur knowledge is deeply appreciated.
Thanks a lot!
# 3  
Old 04-19-2006
well show what u have done..so far.......may be we can help , once u get started with it
# 4  
Old 04-19-2006
Bumping up questions is against the rules.
# 5  
Old 04-19-2006
em...

Ok.....thanks for ur remind
so far, I have thought of procedures to do this as below:
1. Use
head -2 sourcefile.csv > /tmp/heading2rows.txt
tail -1 /tmp/heading2rows.txt > /tmp/heading2ndrow.txt
to get the second row from the source .csv file and save it into a tmp file
2. I want to use awk 's substr(,,) function to get the substring where the value I want, but for this step, I am not quite sure how to compose the actual script...
3. After I can succeed in extracting the value string, eg(10-Mar-2004), I want to write it into another file and the specific postion in tat file, say, from the "a"th char of the second row, and overwrite the old value there.For this part, I am also not v sure how to do it...
so, can anyone give any tips on this, I dun want u to write the actual script for me, but just guid me on any function or command tat can be used to solve such kind of problems...

Thanks n regds
HN
# 6  
Old 04-19-2006
How about something like this:

Code:
Code

typeset method=d                            ;# d=find date by delimiter, otherwise use char position

# Variables relating to the input file

typeset sourceline=2                        ;# The line containing the date
typeset sourcedelim=':'                     ;# The delimiter character on the date line
typeset sourceposn=33                       ;# The position on the date line
typeset infile=sourceRecord.txt             ;# Input file

# Variables relating to the output file

typeset outfile=updateRecord.txt            ;# Output file
typeset outtitle="This file is to record the lastest update time" ;# Output header line
typeset outtext="The latest update time is: "                     ;# Label for date line

typeset -i count=0

while read line
do
  count=$(($count + 1))
  if [ $count -eq $sourceline ]; then
    print $outtitle
    if [ "$method" = "d" ]; then
      print $outtext $(print $line|cut -d$sourcedelim -f2)
    else
      print $outtext $(print $line|cut -c$sourceposn-)
    fi
    break
  fi
done < $infile >$outfile

# Copy the remainder of the file using tail (faster than doing it in the loop if
# the file is large

tail +$(($sourceline + 1)) $infile >> $outfile

You might want to find the date by specifying that it follows the first semi-colon (if so then set the method variable to 'd'); or at a specific character position (set method to any other value, or blank).

cheers
# 7  
Old 04-21-2006
Hi, thestevew, thanks for ur idea and tips above.
------------------------------------------------------------------------
So far, I've got partly done of the script based on my original idea shown below:
#The script to extract "10-Mar-2006" from the source file and keep it in a tmpFile

head -2 sourcefile | tail -1l |awk -F"," '{print $1}' |cut -c52-62 >tmpFile

However, here the new problem arises, I need to convert this value to the format of MM/DD/YYYY, at 1st, I tried to use "date" to convert format, however, I find later tat it can only play with locale time, and I dun wanna change TZ. so I do it manually like this:

string=$(<tmpFile)
year='echo $string | cut -c8-11'
monthname='echo $string | cut -c4-6'
day='echo $string | cut -c1-2'

case $monthname in
Jan) typeset -RZ2 month=1;;
Feb) typeset -RZ2 month=2;;
Mar) typeset -RZ2 month=3;;
Apr) typeset -RZ2 month=4;;
May) typeset -RZ2 month=5;;
Jun) typeset -RZ2 month=6;;
Jul) typeset -RZ2 month=7;;
Aug) typeset -RZ2 month=8;;
Sep) typeset -RZ2 month=9;;
Oct) typeset -R2 month=10;;
Nov) typeset -R2 month=11;;
Dec) typeset -R2 month=12;;
*) print "Wrong date value extracted from source file";;
esac
#from the above, I can convert Mon(eg. Jan,Mar,...) to 01,02....12.
Next, I try to use sed to update the target file:

sed -e "s/ RE / replacement " targetFile >tmpTargetFile
mv tmpTargetFile targetFile

My question here is, how can I set "RE" to the last line and the specific postion I want in target file
e.g:The latest update time is: xxxxxx where the xxxxxxx is the old date value I want to replace with my new date value.

and to set replacement to be of my value $month/$date/$year got from script above.

Thanks a lot for ur help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Print start date to end date, given $1 & $2 in ksh

Dear all, I have an user passing 2 parameter 31/03/2015 and 02/04/2015 to a ksh script. How to print the start date to end date. Expected output is : 31/03/2015 01/04/2015 02/04/2015 Note : 1. Im using aix and ksh 2. I have tried to convert the given input into a date, didnt... (0 Replies)
Discussion started by: mr.rajaravi
0 Replies

2. Shell Programming and Scripting

FILE_ID extraction from file name and save it in CSV file after looping through each folders

FILE_ID extraction from file name and save it in CSV file after looping through each folders My files are located in UNIX Server, i want to extract file_id and file_name from each file .and save it in a CSV file. How do I do that? I have folders in unix environment, directory structure is... (15 Replies)
Discussion started by: princetd001
15 Replies

3. UNIX for Dummies Questions & Answers

Unable to convert date into no. using date -d +%s syntax in ksh shell

hi friends, I m trying to write a script which compares to dates. for this i am converting dates into no using synatx as below v2=`date | awk '{print $2,$3,$4}'` v3=`date +%s -d "$v2"` this syntax is working in bash shell ,but fails in ksh shell. please suggest on this. (12 Replies)
Discussion started by: Jcpratap
12 Replies

4. Shell Programming and Scripting

ksh compare dates INSIDE a file (ie date A is > date B)

In KSH, I am pasting 2 almost identical files together and each one has a date and time on each line. I need to determine if the first instance of the date/time is greater than the 2nd instance of the date/time. If the first instance is greater, I just need to echo that line. I thought I would... (4 Replies)
Discussion started by: right_coaster
4 Replies

5. Shell Programming and Scripting

File Extraction

Hi, I have three files as below: AA.DAT20110505063903.Z AA.DAT20110405062903.Z AA.DAT20110305061903.Z All the above files are appended with Date and timestamp in compressed format. I need to extract AA.DAT20110505063903.Z(which is the latest file) from one server and uncompress it... (2 Replies)
Discussion started by: pyaranoid
2 Replies

6. Shell Programming and Scripting

How to Get 60 days Old date from current date in KSH script

Hi i am writing a cron job. so for it i need the 60 days old date form current date in variable. Like today date is 27 jan 2011 then output value will be stote in variable in formet Nov 27. i am using EST date, and tried lot of solution and see lot of post but it did not helpful for me. so... (3 Replies)
Discussion started by: Himanshu_soni
3 Replies

7. Shell Programming and Scripting

Date and time range extraction via Awk or analysis script?

Hello does anyone know of an awk that will extract log file entries between a specific date and time range, eg: awk '/15\/Dec\/2010:16:10:00/, /15\/Dec\/2010:16:15:00/' access_log but one that works? Or a free command line log file analysis tool/script? I'd like to be able to view... (2 Replies)
Discussion started by: competitions
2 Replies

8. Shell Programming and Scripting

want to get previous date from date command in ksh

I want to get previous date from date command. I am using ksh shell. Exmp: today is 2008.09.04 I want the result : 2008.09.03 Please help. Thanks in advance. (4 Replies)
Discussion started by: rinku
4 Replies

9. Shell Programming and Scripting

Report file extraction based on Date range

Hi all, Iam writing a script, which will extract all the files from Start_Date to End_Date. Files are date stamped as YYYYMMDD. For ex: Start_Date='20051001' End_Date='20060331' extract files such as........ ramp_20050810.rpt ramp_20050915.rpt ramp_20051001.rpt ramp_20051010.rpt... (2 Replies)
Discussion started by: ganapati
2 Replies

10. UNIX for Dummies Questions & Answers

help on file extraction

Hello, Im trying to extract a portion of a big file. Using unique pattern /occurrence , (ex. loginname1,logoff and loginname2,logoff ), I like to print the lines that contain the patterns and the lines between them. Also, create a file for every login occurrence. Thanks for everyone's... (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question