The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extraction of various lines from a hugh file srsahu75 Shell Programming and Scripting 5 05-07-2008 05:17 AM
Shell script for text extraction from a file vignesh53 Shell Programming and Scripting 3 02-05-2008 05:16 AM
Changing Creation Date to a Prespecified Date of a File In Unix monkfan UNIX for Dummies Questions & Answers 4 11-28-2006 04:15 AM
Report file extraction based on Date range ganapati Shell Programming and Scripting 2 07-13-2006 08:26 AM
help on file extraction apalex UNIX for Dummies Questions & Answers 1 05-01-2001 07:29 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 04-18-2006
Registered User
 

Join Date: Apr 2006
Posts: 4
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
Reply With Quote
Forum Sponsor
  #2  
Old 04-18-2006
Registered User
 

Join Date: Apr 2006
Posts: 4
Unhappy Nobody gives any tips?

come on, anyone who knows, pls help
Any kind of share of ur knowledge is deeply appreciated.
Thanks a lot!
Reply With Quote
  #3  
Old 04-18-2006
Registered User
 

Join Date: Mar 2006
Location: Gurgaon
Posts: 26
well show what u have done..so far.......may be we can help , once u get started with it
Reply With Quote
  #4  
Old 04-19-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,698
Bumping up questions is against the rules.
Reply With Quote
  #5  
Old 04-19-2006
Registered User
 

Join Date: Apr 2006
Posts: 4
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
Reply With Quote
  #6  
Old 04-19-2006
Registered User
 

Join Date: Mar 2006
Location: South Yorkshire, UK
Posts: 114
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
Reply With Quote
  #7  
Old 04-20-2006
Registered User
 

Join Date: Apr 2006
Posts: 4
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:52 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0