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
Ok.....thanks for ur remind
so far, I have thought of procedures to do this as below:
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
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...
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...
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).
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
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:
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.