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
Linux distro from bash script Samtim74 Shell Programming and Scripting 1 08-19-2008 02:04 AM
Perl code to differentiate numeric and non-numeric input Raynon Shell Programming and Scripting 11 08-04-2007 10:32 AM
how to numeric sort on field time rahulspatil_111 Shell Programming and Scripting 1 04-27-2007 11:52 AM
Sort (bash command) booboo Shell Programming and Scripting 5 03-31-2006 07:18 PM
Floating point numeric comparisions in bash borncrazy Shell Programming and Scripting 2 03-27-2005 07:39 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-27-2008
dlm1065 dlm1065 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 1
help newb at linux and bash need numeric script sort

I am trying to setup to automatically import a series of mysql database files. I am doing manually now and its a royal pain.

All the sql files are sequentially numbered in a format of 4 numbers underscore text with spaces replaced by underscores.

example:
Quote:
logon_updates/2334_third_file.sql
logon_updates/1234_first_file.sql
logon_updates/1345_second_file.sql
There are 3 databases each setup in the same fashion. The main directory has the database structure file higher sequence replacing lower sequence. Each database has its own subdirectory with sequenced updates the files with numbers higher than the structured files need to be loaded in numerical sequenced. There are duplicate sequence numbers in the update directories those all have to be loaded before the next number.

I figured I can do this to gain the proper structure file as they do not have duplicate numbers.

Quote:
list=$(find -name "*logon_structure.sql" -print)
for f in $list;
do
g=$(echo $f | cut -b3-6)
if [ $g -ge $lnum ]; then
lnum=$g
fi
done
I tried the below code getting rid of the directory name with cut but they are in the wrong order, they are in alphabetic not numeric order.
The variable h has the number alone and g has the full file name .
Quote:
list=$(find logon_updates -name "*.sql" -print)
for f in $list;
do
g=$(echo $f | cut -b15-255)
h=$(echo $f | cut -b15-18)
if [ $h -gt $lnum ]; then
echo $g
fi

done
I lack a lot of basic knowledge under linux that I had in a dos/windows environment.

I have been looking at this code so long I can't see the forest for the trees, if you know what I mean. So any suggestions to get me on the right train of thought would be greatly appreciated

Thanks,
dlm1065

Last edited by dlm1065; 04-27-2008 at 03:36 AM..
  #2 (permalink)  
Old 04-27-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
If you only use the output from find once, you don't really need to put it in a variable.

My suggestion would be to extract the sequence number and the database to separate fields, and sort numerically on the sequence number.

Code:
find logon_updates -name "*.sql" -print |
while read f
do
  g=$(echo $f | cut -b15-)
  h=$(echo $f | cut -b15-18)
  echo $h:$g
done | 
sort -t : -k1n
Once you are confident that this works correctly (I don't have the data to test on), you can continue the pipeline:

Code:
... sort -t : -k1n |
cut -d: -f2- |
while read f; do
  sql PERFORM ACTS OF horror WITH "$f" USING BIG STICK
done
If the output from find is fairly regular, probably you could find some clever options which would allow you to pass that directly to sort without the while look. Maybe pass the output from find through sed to temporarily regularize it?

Maybe something like this would work already?

Code:
find -name "*logon_structure.sql" -print |
sort -t / -k2n

Last edited by era; 04-27-2008 at 04:17 AM.. Reason: Oops, sort -t (not -d!)
Sponsored Links
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 10:03 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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