sorting files

ct-me

New Member
Credits
34
Hi, I am sorry if my question is too simple or if it has been already debated.
I have to sort the content of several txt files and to save the sorted ones as different files. I try to better explain:
I have n files named "1.txt, 2.txt,...n.txt". I want to sort the content of these files and to save the results as another set of n files. At the moment, I was able to sort the content of the files but I am not able to save the sorting results as single files. May you help me? Thank you in advance. Cristina
 


f33dm3bits

Gold Member
Gold Supporter
Credits
15,054
If I am understanding you correctly you could do it like this.
This being the unsorted.txt list:
Code:
cat unsorted.txt
train
car
bike
airplane
motorcylce
Then to sort the list and create a different file:
Code:
sort unsorted.txt > sorted.txt
Display the content of sorted.txt:
Code:
cat sorted.txt
airplane
bike
car
motorcylce
train
 

ct-me

New Member
Credits
34
Hi,
thank you for your reply.
The solution that you suggested me is ok when I work with only one file. My problem is that I have to manage several files at the same time. I sorted all the files but I cannot save the reults as separeted files.
I mean, I have the files 1.txt, 2.txt, etc. I sorted the content of each files. Now I want to save the sorted files as 1-sorted.txt, 2.sorted.txt, etc. by using a single command. I have to sort more than 300 files and I dont want to make the sorting one by one.
I hope I was able to explain my situation.

Cristina
 
Last edited:

f33dm3bits

Gold Member
Gold Supporter
Credits
15,054
You can do that in a loop, I don't have time right now to show you but I will try to when I have time later.
 

JasKinasis

Well-Known Member
Credits
5,977
As @f33dm3bits has said - use a loop.
Create the following script in the directory containing all of the text files.

For now, I'll refer to it as sorttxt, but you can call it whatever you like:
Bash:
#!/usr/bin/env bash

# if a "sorted" directory doesn't exist, create it.
if [ ! -d ./sorted ] ; then
    mkdir sorted
fi
for file in ./*.txt ; do
    # sort each file and output to a new file in the "sorted" directory
    sort "$file" > sorted/"$file"
done
Make it executable using chmod +x sorttxt and then run it using ./sorttxt.
NOTE: if you called the script something else, replace sorttxt in the above commands with whatever you called the script.

The script above does the following.
If a subdirectory called "sorted" does not exist in the current directory, then a "sorted" directory is created.
Then we loop through all of the .txt files in the current directory.
Each file is sorted and then written to the "sorted" directory, with their original filenames.

So after running the script in the directory containing the 300 unsorted text files, you will have a sub-directory called "sorted" containing 300 sorted files.
 

wizardfromoz

Super Moderator
Staff member
Gold Supporter
Credits
13,656
Moving this to Command Line.

Welcome to linux.org @ct-me :)
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Members online


Top