04-27-2008
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 04:36 AM..
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
i want to sort time field given by who command as a whole
i have tried like this
who|sort -n +4 -5 (1 Reply)
Discussion started by: rahulspatil_111
1 Replies
2. Shell Programming and Scripting
Hello all
I have data like below where the column with values (PRI, SEC ) is the char field and the rest are Numeric Fields.
200707,9580,58,7,2,1,PRI,1,1,137,205594,0,5,10,-45.51,-45.51
200707,9580,58,7,2,1,SEC,1,1,137,205594,0,5,10,-45.51,45.51... (1 Reply)
Discussion started by: vasuarjula
1 Replies
3. Shell Programming and Scripting
Hi,
I'm a learner of PERL programming.
I've a input file with the below data:
SWAT_5, 1703, 2010-09-21
SWAT_6, 2345, 2010-09-21
SWAT_7, 1792, 2010-09-21
SWAT_8, 1662, 2010-09-21
SWAT_9, 1888, 2010-09-21
VXHARP_1, 171, 2010-09-21
I need to sort this data based on the second... (6 Replies)
Discussion started by: ganapati
6 Replies
4. Shell Programming and Scripting
Hi,
I am unable to sort data on the first field
$cat t
Jim,212121,Seattle
Bill,404404,Seattle
Steve,246810,Nevada
Scott,212277,LosAngeles
Jim,212121,Ohio
sort -t"," -k1,2 t
Bill,404404,Seattle
Jim,212121,Ohio
Jim,212121,Seattle
Scott,212277,LosAngeles
Steve,246810,Nevada (7 Replies)
Discussion started by: Shivdatta
7 Replies
5. UNIX for Dummies Questions & Answers
dear all,
i have .dat files named as:
34.dat
2.dat
16.dat
107.dat
i would like to sort them by their filenames as:
2.dat
16.dat
34.dat
107.dat
i have tried numerous combinations of sort and ls command (in vain) to obtain :
107.dat
16.dat
2.dat
34.dat (1 Reply)
Discussion started by: chen.xiao.po
1 Replies
6. Shell Programming and Scripting
Hi,
I'm quite new to scripting, but know a few AWK statements.
I have the following line in my script:
hostname=`echo $file | awk 'BEGIN{FS=OFS="."}{$NF=""; NF--; print}'`
I use this in my script to rename files, which are similar to this:
name.mvkf.mkvfm.mkfvm.1
To the... (4 Replies)
Discussion started by: TauntaunHerder
4 Replies
7. Shell Programming and Scripting
Hi
I am using this
cat substitutionFeats.txt | gawk '{$0=gensub(/\t/,"blabla",1);print}' | gawk '{print length, $0}' | sort -n | sort -r
and the "sort -n" command doesn't work as expected: it leads to a wrong ordering:
64 Adjustable cuffs
64 Abrasion-
64 Abrasion pas
647 Sanitized 647... (4 Replies)
Discussion started by: louisJ
4 Replies
8. UNIX for Dummies Questions & Answers
This should be extremely simple and someone will probably answer it in .5 seconds. I need to know what:
VARIABLE=${0##*/}
does? I do not have a shell handy to just try it in. I am reading through some scripts and need to understand this line. Any help is appreciated.
Many thanks!
-... (3 Replies)
Discussion started by: chrisgoetz
3 Replies
9. Shell Programming and Scripting
Input file:
100%ABC2 3.44E-12 USA
A2M%H02579 0E0 UK
100%ABC2 5.34E-8 UK
100%ABC2 3.25E-12 USA
A2M%H02579 5E-45 UK
Output file:
100%ABC2 3.44E-12 USA
100%ABC2 3.25E-12 USA
100%ABC2 5.34E-8 UK
A2M%H02579 0E0 UK
A2M%H02579 5E-45 UK
Code try:
sort -k1,1 -g -k2 -r input.txt... (2 Replies)
Discussion started by: perl_beginner
2 Replies
10. UNIX for Advanced & Expert Users
From googling and reading man pages I figured out this sorts the first column by numeric values.
sort -g -k 1,1
Why does the -n option not work? The man pages were a bit confusing.
And what if I want to sort the second column numerically? I haven't been able to figure that out. The file... (7 Replies)
Discussion started by: cokedude
7 Replies
LEARN ABOUT CENTOS
shell-quote
SHELL-QUOTE(1) User Contributed Perl Documentation SHELL-QUOTE(1)
NAME
shell-quote - quote arguments for safe use, unmodified in a shell command
SYNOPSIS
shell-quote [switch]... arg...
DESCRIPTION
shell-quote lets you pass arbitrary strings through the shell so that they won't be changed by the shell. This lets you process commands
or files with embedded white space or shell globbing characters safely. Here are a few examples.
EXAMPLES
ssh preserving args
When running a remote command with ssh, ssh doesn't preserve the separate arguments it receives. It just joins them with spaces and
passes them to "$SHELL -c". This doesn't work as intended:
ssh host touch 'hi there' # fails
It creates 2 files, hi and there. Instead, do this:
cmd=`shell-quote touch 'hi there'`
ssh host "$cmd"
This gives you just 1 file, hi there.
process find output
It's not ordinarily possible to process an arbitrary list of files output by find with a shell script. Anything you put in $IFS to
split up the output could legitimately be in a file's name. Here's how you can do it using shell-quote:
eval set -- `find -type f -print0 | xargs -0 shell-quote --`
debug shell scripts
shell-quote is better than echo for debugging shell scripts.
debug() {
[ -z "$debug" ] || shell-quote "debug:" "$@"
}
With echo you can't tell the difference between "debug 'foo bar'" and "debug foo bar", but with shell-quote you can.
save a command for later
shell-quote can be used to build up a shell command to run later. Say you want the user to be able to give you switches for a command
you're going to run. If you don't want the switches to be re-evaluated by the shell (which is usually a good idea, else there are
things the user can't pass through), you can do something like this:
user_switches=
while [ $# != 0 ]
do
case x$1 in
x--pass-through)
[ $# -gt 1 ] || die "need an argument for $1"
user_switches="$user_switches "`shell-quote -- "$2"`
shift;;
# process other switches
esac
shift
done
# later
eval "shell-quote some-command $user_switches my args"
OPTIONS
--debug
Turn debugging on.
--help
Show the usage message and die.
--version
Show the version number and exit.
AVAILABILITY
The code is licensed under the GNU GPL. Check http://www.argon.org/~roderick/ or CPAN for updated versions.
AUTHOR
Roderick Schertler <roderick@argon.org>
perl v5.16.3 2010-06-11 SHELL-QUOTE(1)