Sorting based on filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting based on filename
# 1  
Old 08-31-2015
Question Sorting based on filename

Hello ,
I have to write a bash script. I will explain the logic based on a scenario.

Scenario :
Suppose I have few files in a Folder X :
Code:
  FILE_201508.list
  FILE_201510.list
  FILE_201507.list
  abc_201510.csv
  xyz_201508.csv
  abc_201507.csv
  def_201507.csv

1) Now , first it should it only search for files starting with FILE_* in descending order and take the file with lowest number i.e FILE_201507.list .
2) (a)Then , corresponding to that file number , it should take other files which has same number in their prefix i.e (abc_201507.txt, def_201507.txt) and change their file name and copying it to a different folder Y.
OR
(b) It should open that .list file which is having few *.csv file names that are in same X folder . it should select those files and then rename them and copy it to a different folder Y .

Last edited by zaxxon; 09-01-2015 at 03:05 AM.. Reason: removed "urgent", added code tags and moved to technical forum
# 2  
Old 08-31-2015
OK...
and what have you done so far?
# 3  
Old 08-31-2015
Really ought to be in some "newbs" forum.

This is in no way the most efficient of methods, but this should get you started. Since ls always sorts the fname:
Code:
fname=`ls FILE* | head -1`

gets you the earliest of the FILE filenames. If you have some aberrant version of ls, stick a sort in there:
Code:
fname=`ls FILE* | sort | head -1`

Now that you have the particular filename you want, you simply have to extract the number, and then do another ls.

This is the real hoakey part, I am sure that monkeying with the $VAR designators in bash could produce the same result:

Code:
num=`echo $f | sed -e 's/FILE_//' -e 's/\.list//'`

filelist=`ls *${num}*`

gives you the list of matching files.

I'm sure you can do the rest. Remember to remove the already-scanned FILE_*.list from the current directory before you loop...
# 4  
Old 08-31-2015
Sounds like homework, and i would have redirected to: https://www.unix.com/homework-and-coursework-questions/
But since there is already an answer...
Code:
for F in FILE*
do 	echo "$F"
	str="${F/*_}"
	for add in [a-z]*_${str/\.*}*
	do 	echo "* $add"
	done
done

Cheers

Last edited by sea; 08-31-2015 at 12:25 PM.. Reason: changed code
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting based on File name

Hi All I have a requirement to list all the files in chronological order based on the date value in the file name.For ex if I have three files as given below ABC_TEST_20160103_1012.txt ABC_TEST_20160229_1112.txt ABC_TEST_20160229_1112.txt I have written code as given below to list out... (2 Replies)
Discussion started by: ginrkf
2 Replies

2. Shell Programming and Scripting

Sorting using filename of a path

Hi all! i have a question how do you sort the filename of a path directory according to alphabetic order. Example: sort according to highlighted text. There maybe space for filename Path=/home/pikamon/Desktop/ABC; Path=/home/pikamon/Desktop/ABD; Path=/home/pikamon/Desktop/Riduan la;... (5 Replies)
Discussion started by: pikamon
5 Replies

3. Shell Programming and Scripting

Sorting based on the second field

Oracle Enterprise Linux 6 This is my file. Two fields separated by space $ cat testfile.txt MARCH9 MARCH4 MARCH1 MARCH5 MARCH2 MARCH326 MARCH821 MARCH7 MARCH6 MARCH2 $ $ The following numeric sort, based on the first field's 6th character works as expected. $ $ sort -n -k 1.6... (7 Replies)
Discussion started by: John K
7 Replies

4. Shell Programming and Scripting

Sorting file based on name

Hi team, We have few files landing to our server based on sequence number. These files have to be processed in the sequence number order. Once the sequence number has reached its maximum, the files with sequence number 0000 has to be processed. For example: IN9997 IN9998 IN9999 IN0000... (7 Replies)
Discussion started by: anijan
7 Replies

5. Shell Programming and Scripting

Sorting based on a particular colum

Hi, I want a flat file(pipe delimited) to be sorted based on 2nd column only. Below is input file to be sorted. AVERS|K50034|A|Y|N|N|Y|Y|Y|||N|N AVERS|K50035|A|Y|N|N|Y|Y|Y|||N|N... (11 Replies)
Discussion started by: Nikhath
11 Replies

6. Shell Programming and Scripting

sorting based on a field

the below is sorted as it is. the fields that i'm interested in are the 4th and 5th field. i want to sort the based on the 4th field. my past attempt to do this was to do something like this: awk '{print $4}'| awk '{print $1":"$2}' datafile | sort | uniq however, if i do that, i lose... (2 Replies)
Discussion started by: SkySmart
2 Replies

7. Shell Programming and Scripting

Sorting file based on names

Hi I have some files in directory and the names of files are like jnhld_15233_2010-11-23 jnhld_15233_2007-10-01 jnhld_15233_2001-05-04 jnhld_15233_2011-11-11 jnhld_15233_2005-06-07 jnhld_15233_2000-04-01 ..etc How can i sort these files based on the date in the file name so that ... (4 Replies)
Discussion started by: morbid_angel
4 Replies

8. UNIX for Dummies Questions & Answers

Sorting words based on length

i need to write a bash script that recive a list of varuables kaka pele ronaldo beckham zidane messi rivaldo gerrard platini i need the program to print the longest word of the list. word in the output appears on a separate line and word order in the output is in the order Llachsicografi costs.... (1 Reply)
Discussion started by: yairpg
1 Replies

9. Shell Programming and Scripting

sorting based on alternative lines

Hello, I have a file with multiple entries. @SFGF-GA2-1_58:5:36:11009:999#0/1 NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN +SFGF-GA2-1_58:5:36:11009:999#0/1 ################################################################################... (10 Replies)
Discussion started by: Diya123
10 Replies

10. Shell Programming and Scripting

Sorting based on columns

Hi, I want a list of entries in 3 space delimited columns. I want to sort entries based on the very first column. Rows can't be changed. For example: If I have... Abc Abc Acc Bca Bda Bdd Cab Cab Cbc Dbc Dca Dda Abc Abc Acc the output should be... Abc Abc Acc Abc Abc Acc Bca... (7 Replies)
Discussion started by: MobileUser
7 Replies
Login or Register to Ask a Question