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. Shell Script Page.

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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-13-2008
Registered User
 

Join Date: May 2008
Posts: 3
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-13-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,289
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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 07:49 AM.
Reply With Quote
  #3 (permalink)  
Old 05-13-2008
Registered User
 

Join Date: May 2008
Posts: 3
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Jim,

Exactly what I needed - thanks!
Reply With Quote
  #4 (permalink)  
Old 05-13-2008
Registered User
 

Join Date: May 2008
Posts: 3
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
  #5 (permalink)  
Old 05-13-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,289
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
"inappropriate ioctl for device" 421 service not available, remote server has closed connection ^m autosys awk trim bash eval bash exec bash for loop boot: cannot open kernel/sparcv9/unix close_wait command copy/move folder in unix curses.h cut command in unix dead.letter find grep find null character in a unix file grep multiple lines grep or grep recursive grep unique inappropriate ioctl for device logrotate.conf lynx javascript mailx attachment mget mtime perl array length ping port remove first character from string in k shell replace space by comma , perl script scp recursive segmentation fault(coredump) sftp batch sftp script snoop unix stale nfs file handle syn_sent tar exclude unix unix .profile unix com unix forum unix forums unix interview questions unix mtime unix simulator unix.com vi tab size vi+substitute+end+of+line+character while loop within while loop shell script


All times are GMT -7. The time now is 12:30 PM.


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

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101