Sorting issue with directory and file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting issue with directory and file name
# 1  
Old 05-14-2010
Sorting issue with directory and file name

Hi All,

My folder contains the files as:

Code:
$ ls -lrt
total 50
lms_prap_rf_20100422_99.xml
lms_prap_rf_20100422_9.xml
lms_prap_rf_20100422_100.xml
lms_prap_rf_20100422_10.xml
lms_prap_rf_20100426_1.xml

I need to get the sorting of the above file based on numbers. I need first file in ascending order.

I am using the code as
Code:
ls -l lms_prap_rf* | sort -t _ -nk4 -k5 | head -1

which gives me correct one as
Code:
 
lms_prap_rf_20100422_9.xml

but when I am doing it with the directory I am not getting what I want because my directories consist of underscores.

Code:
ls -1 /c09/oracle/XX_TT/12.0.0/TMP_IN/lms_prap_rf* | sort -t _ -nk4 -k5 | head -1

I am getting the wrong answer as
Code:
/c09/oracle/XX_TT/12.0.0/TMP_IN/lms_prap_rf_20100422_10.xml

My main code is looking for
Code:
in_file=`ls -1 $file_path/$file_name* | sort -t _ -nk4 -k5 | head -1`

where
Code:
file_path=]/c09/oracle/XX_TT/12.0.0/TMP_IN

and
Code:
file_name=lms_prap_rf

how to avoid the directory path while doing sort. Please not I need to run this script in 2 or 3 environment where the directory path may different and may have more underscores

Please advice.

Regards,
Sreejit
# 2  
Old 05-14-2010
Code:
#!/bin/ksh
path_to_files=/c09/oracle/XX_TT/12.0.0/TMP_IN
ls ${path_to_files}/lms_prap_rf* | awk -F '[._]' '{print $NF-1, $0} |
    sort -n | head -1 | read dummy fname; basename $fname

change the path_to_files variable for each directory`
# 3  
Old 05-14-2010
Thanks but still the code below

Code:
ls ${path_to_files}/lms_prap_rf* | nawk -F '[._]' '{print $NF-1, $0}' | sort -n | head -1 | read dummy fname; basename $fname

is giving wrong answer

Code:
lms_prap_rf_20100422_10.xml

the answer must be

Code:
lms_prap_rf_20100422_9.xml

because this is the smallest file as per the date 20100422 and the number 9.

the pattern is lms_prap_rf_{date YYYYMMDD}_{number}.xml

Regards,
Sreejit
# 4  
Old 05-14-2010
I think the problem is that your path name contains "_" character. when you use ls path/file_prefix* or ls -l path/file_prefix*. Try to cd into that directory and then do "ls file_prefix*" instead.
# 5  
Old 05-17-2010
Quote:
Originally Posted by plyl
I think the problem is that your path name contains "_" character. when you use ls path/file_prefix* or ls -l path/file_prefix*. Try to cd into that directory and then do "ls file_prefix*" instead.
Thanks Yes I do understand that the issue is with the underscore "_" in my path. But I am using this in my shell script and I don't want to use cd command in there.

Is there any way I can ls on the directory and remove the directory path from my result and do sort on it.?

Thanks and Regards,
Sreejit

---------- Post updated at 10:37 AM ---------- Previous update was at 09:59 AM ----------

Hi All-

I am trying the below, can anybody please let me know if I am doing anything wrong.

Code:
ls ${path_to_files}/lms_prap_rf* | nawk -F/ '{print $NF}' | sort -t _ -nk4 -k5 | head -1

Regards,
Sreejit
# 6  
Old 05-17-2010
try this:

ls -1 /c09/oracle/XX_TT/12.0.0/TMP_IN/lms_prap_rf* |awk -F "/" '{print $2}'| sort -t _ -nk4 -k5
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting issue

Hi All, I am working on a huge pile of data. Its about a service initiating a call and and finishing it. The columns that I have is: Date Time URL Call Type ServiceName 5/11/2015 15:00:00 http-/0.0.0.0:8091-9 going to... (3 Replies)
Discussion started by: Junaid Subhani
3 Replies

2. Homework & Coursework Questions

Sorting files by size in another directory

1. The problem statement, all variables and given/known data: I'm trying to use a directory path to enter a new directory and sort the files there. I'm using the language C with a system call in Unix to sort the files from smallest to largest. 2. Relevant commands, code, scripts, algorithms:... (1 Reply)
Discussion started by: TedFTW
1 Replies

3. Shell Programming and Scripting

sorting issue

I have files with the name myfile_YYMMDD.txt I need to sort them by their date stamp. I know this will work. ls -lrt * | awk '{print $9}' | cut -d "_" -f3 | sort However, it just returns YYMMDD.txt I can then append the myfile_ to it. However, that myfile_ may change. How do... (1 Reply)
Discussion started by: guessingo
1 Replies

4. Shell Programming and Scripting

An Issue with the script which used to remove a file from the current directory.

Hello forum members, I am writing a script to two tasks. 1: displaying the list of the files in the current directory. 2: removing the specifed file from the list. I have written a sample script ,so can u please verfiy and correct. echo Enter list of files ls *.txt read textfile rm -f... (3 Replies)
Discussion started by: sivaranga001
3 Replies

5. Shell Programming and Scripting

Sorting by Full directory path

I have a text file with full list of files with their full path. I wanted to sort it by directory then files then subdirectory by alphabetically. When I used the sort command it doesn't give like what I want. Could somebody help me on this. Here is the ex: This is what I'm getting... (2 Replies)
Discussion started by: javidraaj
2 Replies

6. UNIX for Dummies Questions & Answers

Issue on sorting

Hi, I have a file which has data as path/1.xml path/2.xml,...(each in separate lines) I want to sort this file in order. It is not working when I give "sort filename" The number of "/" in path can vary. Thanks, Floyd (1 Reply)
Discussion started by: asmfloyd
1 Replies

7. Shell Programming and Scripting

Sorting Issue

Hi Guys, I am very new to Unix.Can anyone tell me how to sort a file based on fields. For example, I am having a file whose contents are mentioned below: 2233|charles harris |g.m. |sales |12/12/52| 90000 9876|bill johnson |director... (3 Replies)
Discussion started by: mraghunandanan
3 Replies

8. UNIX for Dummies Questions & Answers

Sorting Directory Listing

If I do an ls -l on a directory I get this: -rw-r--r-- 1 root other 5248094 Jun 24 03:56 monitor.log.7 -rw-r--r-- 1 root other 5248303 Jul 11 11:19 ct.log.1 -rw-r--r-- 1 root other 5248907 Jun 29 06:01 ct_monitor.log.5 -rw-r--r-- 1 root other 5249042 Jun 19... (1 Reply)
Discussion started by: Sepia
1 Replies

9. UNIX for Dummies Questions & Answers

Sorting files in a directory

Hi guys, Probably an easy one, but how do you sort a directory so that the files come first, then subdirectories? ie ./dir1 has file 1 subdir 1 file 2 i need it to become file 1 file 2 subdir 1 as i'm using it in a script to pass each one through a for loop, and would like all... (3 Replies)
Discussion started by: olimiles
3 Replies

10. Shell Programming and Scripting

Sorting files in a directory

Hi guys, Probably an easy one, but how do you sort a directory so that the files come first, then subdirectories? ie ./dir1 contains file 1 subdir 1 file 2 i need it to become file 1 file 2 subdir 1 as i'm using it in a script to pass each one through a for loop, and would... (2 Replies)
Discussion started by: olimiles
2 Replies
Login or Register to Ask a Question