Issues with sorting in reverse order

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Issues with sorting in reverse order
# 1  
Old 11-30-2016
Issues with sorting in reverse order

I have a unix script that outputs a summary file to the mac desktop.

The file is called summary.txt

I am trying to configure such so that the summary.txt file lists the content contained within such in reverse sort order.

I have used sort -r but it does not seem to work.

I would be grateful for any steering on this as I am newish to the language.

my code as it stands is:

Code:
#!/bin/bash
# the below read command takes in a user command in the terminal in terms of a series of arguements
read -p "Good Morning, Please enter your file type name for sorting [ENTER]:" all_extensions
if cd /Users/christopherdorman/desktop
  then  while read extension
      do    destination="folder$extension"
# the mkdir creates new directories for the filetypes inputted in the terminal
        mkdir -p "$destination"
        mv  -v unsorted/*."$extension" "$destination"
      done   <<< "${all_extensions// /$'\n'}"
# the all extensions code focuses on everything else that has not been inputed in the command and looks to house these is a misc folder
        mkdir -p foldermisc 
        if mv  -v unsorted/* "foldermisc"
# the below echo confirms to the user back in the terminal that the processing of this is complete. 
      then  echo "Good News, the rest of Your files have been successfully processed"
        fi
# The below code creates a file list in each created directory and lists the files by size order. the cat file list creates a summary file on the desktop which details the files in each directory.
    for i in folder*/; do
        ls -S "$i" > "${i}filelist" 
        cat "${i}filelist" >> ~/desktop/summary.txt
	sort -r ~/desktop/summary.txt    
    done	
fi


Last edited by Braveheart; 12-01-2016 at 07:56 AM.. Reason: Changed CODE tags to ICODE tags for inline code.
# 2  
Old 11-30-2016
It is nice to know that what you are doing isn't working, but if we don't know what you are trying to do it is hard to guess at how to fix it.

You are currently taking multiple lists of files sorted by decreasing file size and then sorting them together in reverse alphanumeric order.

What are you trying to do?

It is also a good idea to tell us what operating system and shell you're using.

Does one of the following come close to what you're trying to do?
Code:
ls -Sr folder*/ > ~/Desktop/summary.txt

(which gives you each of your directories with files within each directory sorted by decreasing size) or:
Code:
ls -Srd folder*/* > ~/Desktop/summary.txt

(which gives you all of the files in all of those directories in one list sorted by decreasing size).
# 3  
Old 12-01-2016
I have to second Don Cragun - without further details it's hard to tell what and where it fails. One comment - how about trying
Code:
sort -r ~/desktop/summary.txt

# 4  
Old 12-01-2016
I'm stumped on the point of sorting the file at the end. The first line you write to identify the file will be very unlikely to stay with the content, and the content itself may not stay in order either.

What are you trying to achieve? There is probably a far neater way to achieve it as Don alludes to, but we would need to know what inputs/files you have and what is the required output.


For instance:-
Quote:
I have three directories (for testing) dir-a, dirb and dir-c
They all contain separate files file-a, file-b & file-c respectively
Each file contains my data.

The output I want is:-
Code:
dir-c/file-c:
Data-c line1
Data-c line2
Data-c line3
dir-b/file-b:
Data-b line1
Data-b line2
Data-b line3
dir-a/file-a:
Data-a line1
Data-a line2
Data-a line3

You would be best to tell us which bits need to be sorted (reverse) and which bits need to remain as they are.


I'm sure we can help if we fully understand the problem.


Kind regards,
Robin
# 5  
Old 12-01-2016
Gents, I am using Yosemite 10.10.5 with Bourne Again Shell. Bash

Say the summary.txt that is written to the desktop has content of:

Code:
banana.jpg
pear.jpg
tomato.jpg
apple.jpg
cucumber.png
yam.docx

I am trying to get it to sort in reverse alphabetical order.

Thus....

Code:
yam.docx
Tomato.jpg
Pear.jpg
Cucumber.png
apple.jpg

NB: I want this sort only for the summary.txt

---------- Post updated at 11:55 AM ---------- Previous update was at 11:53 AM ----------




I will post the entire code block above.

---------- Post updated at 11:59 AM ---------- Previous update was at 11:55 AM ----------


Code:
sort -r ~/desktop/summary.txt

is what I have in the code block right now and it is not working. Please revert above where I have listed my entire code.

Last edited by rbatte1; 12-01-2016 at 11:04 AM.. Reason: Added CODE tags
# 6  
Old 12-01-2016
It would help if you could show what you do get that you do not like.

I cannot see a reason that your command doesn't work for the input given and the expected output shown.



Robin
# 7  
Old 12-01-2016
It is not a question of what I do not like, What I am trying to do is quite elementary in a lot of other languages. I want the written output contained in the summary.txt listed in reverse alphabetical (going from Z at the top to A at the bottom) as I wrote in the above example. As it stands the summary.txt files contents are are not listed in this manner and I cannot find a method thus far to do so
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete records in reverse order

Hi all, i have dynamic file 'xyz.txt', records always look likes below format ... 0000021 RET 31-MAR-1984 FAP 0000021 DTA 14-JAN-2003 CNV 0000021 DTA 25-MAR-2012 DTA 0000021 DTA 26-MAR-2012 DTA ################################################# 0000021 DTA ... (4 Replies)
Discussion started by: krupasindhu18
4 Replies

2. Shell Programming and Scripting

Sorting strings in reverse order

Hello, I have a large database of words and would like them sorted in reverse order i.e. from the end up. An example will make this clear: I have tried to write a program in Perl which basically takes the string from the end and tries to sort from that end but it does not seem... (5 Replies)
Discussion started by: gimley
5 Replies

3. Shell Programming and Scripting

print in reverse order

Hi, I want to print the item in reverse order such that the output would look like 00 50 50 23 40 22 02 96 Below is the input: 00 05 05 32 04 22 20 69 Video tutorial on how to use code tags in The UNIX and Linux Forums. (5 Replies)
Discussion started by: reignangel2003
5 Replies

4. UNIX for Dummies Questions & Answers

printing fields in reverse order

command/script(apart from awk) to print the fields in reverse order that is last field has to come first and so on and first field has to go last Input store-id date sale ............. ............. ... (3 Replies)
Discussion started by: tsurendra
3 Replies

5. UNIX for Dummies Questions & Answers

How to print arguments in reverse order?

Hey all, How do I make a script print its arguments in reverse order? Thanks (5 Replies)
Discussion started by: unclepickle1
5 Replies

6. Shell Programming and Scripting

How to get fields in reverse order?

i am having lines like below seperated by "|" (pipe) abc|xyz 123|567 i have to get the above in reverse order xyz|abc 567|123 Pls help (5 Replies)
Discussion started by: suryanarayana
5 Replies

7. Shell Programming and Scripting

cut a field, but with reverse order

Hi Everyone, I have one a.txt: a b 001 c b b 002 c c c, not 002 c The output should be 001 002 002 If i use cut -f 3 -d' ', this does not work on the 3rd line, so i thought is any way to cut the field counting from the end? or any perl thing can do this?:confused: ... (3 Replies)
Discussion started by: jimmy_y
3 Replies

8. UNIX for Dummies Questions & Answers

sort -reverse order

I need to sort the particular column only in reverse order how i can give it.. if i give the -r option the whole file is getting sorted in reverse order. 1st 2nd col 3rd C col 4th col 5th col ------------------------------------------- C... (7 Replies)
Discussion started by: sivakumar.rj
7 Replies

9. Shell Programming and Scripting

sort a file in reverse order

I a file with log entries... I want to sort it so that the last line in the file is first and the first line is last.. eg. Sample file 1 h a f 8 6 After sort should look like 6 8 f a h 1 (11 Replies)
Discussion started by: frustrated1
11 Replies
Login or Register to Ask a Question