The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 08:17 AM
Shell script for text extraction from a file vignesh53 Shell Programming and Scripting 3 02-05-2008 08:16 AM
Changing Creation Date to a Prespecified Date of a File In Unix monkfan UNIX for Dummies Questions & Answers 4 11-28-2006 07:15 AM
Report file extraction based on Date range ganapati Shell Programming and Scripting 2 07-13-2006 11:26 AM
help on file extraction apalex UNIX for Dummies Questions & Answers 1 05-01-2001 10:29 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-19-2006
homer_hn homer_hn is offline
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
  #2 (permalink)  
Old 04-19-2006
homer_hn homer_hn is offline
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!
  #3 (permalink)  
Old 04-19-2006
JunkYardWars JunkYardWars is offline
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
  #4 (permalink)  
Old 04-19-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Bumping up questions is against the rules.
  #5 (permalink)  
Old 04-19-2006
homer_hn homer_hn is offline
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
  #6 (permalink)  
Old 04-19-2006
thestevew thestevew is offline
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
  #7 (permalink)  
Old 04-21-2006
homer_hn homer_hn is offline
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:28 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0