Shell script to sort and execute files sequentially


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to sort and execute files sequentially
# 1  
Old 09-22-2009
Shell script to sort and execute files sequentially

Hi,

I want to sort my files under a directory and execute them sequentially.
For ex:

my diir contains files: a_5.sql, ab_2.sql,abc_3.sql, acbd_1 ,ab_4.sql, etc. I want to execute the files based on the last number after underscore sequentially(i.e.. _1,_2,etc) .

Can anybody help me?
# 2  
Old 09-22-2009
Hi!
I think this should do the trick:
Code:
ls | sort -nr | xargs exec

substitute exec with the proper command you need to actually execute those *.sql files
# 3  
Old 09-22-2009
Something like this:
Code:
$ ls -1 *.sql | perl -e 'print sort { $a=~/_(\d+)/; $c=$1; $b=~/_(\d+)/; $d=$1; $c <=> $d; } <>;'
ab_2.sql
abc_3.sql
ab_4.sql
a_5.sql

Update: What happens with Aleksejs' variant:
Code:
$ ls -1 *.sql| sort -nr
abc_3.sql
ab_4.sql
ab_2.sql
a_5.sql
$ ls -1 *.sql| sort -n
a_5.sql
ab_2.sql
ab_4.sql
abc_3.sql

# 4  
Old 09-22-2009
Hi,

Try something like this:
Code:
for f in $(ls *.sql | sort -t_ -k2,2);do ./$f;done

# 5  
Old 09-22-2009
Hmm... if I run
Code:
touch abc_3.sql ab_4.sql ab_2.sql a_5.sql acbd_1
ls | sort -nr

then I get:
Code:
acbd_1
abc_3.sql
ab_4.sql
ab_2.sql
a_5.sql

Pludi' variant seems more adaptable, of course.
# 6  
Old 09-22-2009
Quote:
Originally Posted by pludi
Something like this:
Code:
$ ls -1 *.sql | perl -e 'print sort { $a=~/_(\d+)/; $c=$1; $b=~/_(\d+)/; $d=$1; $c <=> $d; } <>;'
ab_2.sql
abc_3.sql
ab_4.sql
a_5.sql

Update: What happens with Aleksejs' variant:
Code:
$ ls -1 *.sql| sort -nr
abc_3.sql
ab_4.sql
ab_2.sql
a_5.sql
$ ls -1 *.sql| sort -n
a_5.sql
ab_2.sql
ab_4.sql
abc_3.sql

The above script works perfectly as expected but the difference is it is in perl. anyways thanks also for the quick reply.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to execute 10 sql files from a folder through sqlplus in shell script?

I am new to shell scripting and i want to know how to execute the *.sql files from a folder through sqlplus in shell script and files should be execute in sequentially one by one while execution if any ORA error it has to exit from sqlplus session 1) scripts from external folder 2) logs has... (1 Reply)
Discussion started by: sreekanth Reddy
1 Replies

2. Shell Programming and Scripting

Sequentially rename multiple files

Hello team, We wish to develop a script as follows : 1. Rename multiple files in the following way: example Original file names : test.txt and dummy.txt New file names : test.$(date +"%F").AAAAA<serialno_1>.BBBBBB.p and dummy.$(date +"%F").AAAAA<serialno_2>.BBBBBB.p 2. The... (3 Replies)
Discussion started by: H squared
3 Replies

3. Homework & Coursework Questions

Shell script calling Perl function, sort and find data, write to new files

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I must write a shell script that calls two external Perl functions--one of which sorts the data in a file, and... (6 Replies)
Discussion started by: kowit010
6 Replies

4. Shell Programming and Scripting

sort the files based on timestamp and execute sorted files in order

Hi I have a requirement like below I need to sort the files based on the timestamp in the file name and run them in sorted order and then archive all the files which are one day old to temp directory My files looks like this PGABOLTXML1D_201108121235.xml... (1 Reply)
Discussion started by: saidutta123
1 Replies

5. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies

6. Programming

need help with shell script filtering files and sort! newbie question?

Hi folks, I would like to get familiar with shell script programing. The first task is: write a shell script that: scans your home-folder + sub-directory for all txt-files that all users of your group are allowed to read and write then output these files sorted by date of last... (4 Replies)
Discussion started by: rollinator
4 Replies

7. Shell Programming and Scripting

How to use ssh execute other shell script on other host (shell script include nohup)?

i want use ssh on the host01 to execute autoexec.sh on the host02 like following : host01> ssh host02 autoexec.sh autoexec.sh include nohup command like follwing : nohup /home/jack/deletedata.sh & after i execute ssh host02 autoexec.sh one the host01. i can't found deletedata.sh... (1 Reply)
Discussion started by: orablue
1 Replies

8. UNIX for Dummies Questions & Answers

Shell script which will sort all the files in a directory with the timestamp they were created

Team, Pls help writing a shell script which will sort all the files in a directory with the timestamp they were created. (like ls -lrt) (6 Replies)
Discussion started by: asappidi
6 Replies

9. Shell Programming and Scripting

sequentially run the shell scripts.

Hi All, I have a master script , which would run 4 scripts in sequence. When the first script gets executed , I need to check if a particular process has been completed. If it is completed , only then proceed with the second script. This same rule applies to script3 and script4. Can someone... (4 Replies)
Discussion started by: nua7
4 Replies

10. UNIX for Advanced & Expert Users

FTP Files Sequentially

Hi Gurus, i have to transfer files one by one from ftp server to target server all files which is to be transferred lies in one ftp folder i have to move those files sequentially from ftp to target and must verify files for successful transmission . then i have to delete corresponding... (1 Reply)
Discussion started by: harim
1 Replies
Login or Register to Ask a Question