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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to parse a string into variables aquimby Shell Programming and Scripting 9 06-12-2009 05:57 PM
Parse String Using Sed racbern Shell Programming and Scripting 4 04-23-2008 01:14 PM
how to parse this string hcliff Shell Programming and Scripting 13 04-02-2008 05:43 AM
String parse question mnreferee Shell Programming and Scripting 5 03-07-2007 12:30 AM
parse a string variable methos Shell Programming and Scripting 3 10-18-2005 05:18 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-13-2008
sandiego_coder sandiego_coder is offline
Registered User
  
 

Join Date: May 2008
Posts: 3
How to parse a string efficiently

I am new to the boards and to shell programming and have a requirement to name new files received with a unique sequence number. I need to look at a particular file pattern that exists and then to increment a sequence by 1 and write the new file.

Example of file names and sequence #

part1_part2_part3_datesequence.dat
or
part1_part2_part3_part4_datesequence.dat

the sequence is a 6 digit right justified 0 filled number attached to the date - 20080501000001.dat, but may be the 4th, 5th, 6th or 7th token of the filename.

I have a script that creates the new files, but I am having trouble with how to efficiently extract the sequence number in order to increment it by 1 for a given file pattern and date and then write the new file.

Any help would be appreciated and I am trying to stay with shell or sed or awk as I do not have access to perl.

thanks!

Mike
  #2 (permalink)  
Old 05-13-2008
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,807
one way:

Code:
#!bin/ksh

file="20080501000001.dat"
echo ${file%%.dat} | read seq
len=`expr length $seq`
let start=$len-14
seq=`expr $seq $start 14`
typeset -i num
echo ${seq##????????} | read num
echo $num


Last edited by jim mcnamara; 05-13-2008 at 11:49 AM..
  #3 (permalink)  
Old 05-13-2008
sandiego_coder sandiego_coder is offline
Registered User
  
 

Join Date: May 2008
Posts: 3
Jim,

Exactly what I needed - thanks!
  #4 (permalink)  
Old 05-13-2008
sandiego_coder sandiego_coder is offline
Registered User
  
 

Join Date: May 2008
Posts: 3
Jim

Could you please breakdown how this works? I am changing up how I return the filenames and it is now not working. I am using an ls -a and get a fully qualified path, but your code above does not seem to handle this.

Thanks!

Mike
  #5 (permalink)  
Old 05-13-2008
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,807
I just used your example filenames.


Code:
# this line was file="20080501000001.dat"
ls -rt  part1_*`date "+%Y%m%d`*.dat | tail -1 read file

This find the last occurrence of the file - ie. the highest sequence number for a file received today. the variable "file" is just the filename with the newsest sequence embedded in it.
Assuming I got your requirements.
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 04:36 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