Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help needed to sort file names using bash shell script

 
Pranav Bhasker
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All, I am new to Unix. I need some help in sorting file names. I have a list of files as below:

FILE10_11_9_1_C.sql
FILE11_11_9_1_P.sql
FILE12_11-9-1-P.sql
FILE13_11-9-1-P.sql
FILE14_11_9_1_P.sql
FILE15_11_9_1_P.sql
FILE16_11_9_1_P.sql
FILE17_11_9_1_U.sql
FILE17_11_9_1_U.sql
FILE5_11_9_1_A.sql
FILE7_PKG_C.sql
FILE8_PKG_CV.sql
File1_11_9_1_E.sql
File2_11_9_1_E.sql
File3_11_9_1_C.sql
FILE4_11_9_1_P.sql

I want to sort it numerically till first underscore is encountered. For ex- as below:

File1_11_9_1
File2_11_9_1
.
.
.
File8_Pkg
File10_11_9_1

I am using the following command : ls ${path}/*.sql | sed s/^.*\\/\// | sort -n -k4 > ${path}/filelist.txt;

But, its not giving the desired result.

PLEASE ADVICE

Thanks,
Pranav
 
vijay sarathi bharath
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


found here:
http://theunixshell.blogspot.in/2013/02/sorting-files-based-upon-numeric-value.html
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch both of you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic