Score:0

bash: sort files by a number inside them

us flag

I need to generate 100 .txt files with a random number inside each one of them. Then I need to rename the files so that the file names are named from 1.txt to 100.txt in ascending order based on the value that is stored inside each file. The file named 1.txt should have the smallest number stored in it and the file named 100.txt shouild have the largest number stored in it

I tried this code:

#!/bin/bash
for i in $(seq 1 100)
do
echo $RANDOM > ${i}.txt
done
cat *.txt | sort
us flag
I tested the last script, but the files are not named from 1.txt to 100.txt did I make a mistake?
karel avatar
sa flag
Thanks for clarifying your question. I edited the last script, so that the output files are named from 1.txt to 100.txt as you requested.
us flag
Thanks for helping me out!
karel avatar
sa flag
Your question is much clearer now.
Score:0
ar flag

You could first generate all your random numbers, and then sort them before writing them to the files:

for i in {1..100}; do
    echo $RANDOM
done \
| sort -n \
| while read n; do
     (( x+=1 ))
     echo "$n" > "$x.txt"
  done

Or the same as a one-liner:

for i in {1..100}; do echo $RANDOM; done | sort -n | while read n; do (( x+=1 )); echo "$n" > "$x.txt"; done

Note also that to have sort sort strings of numbers numerically, you need its --numeric-sort (or -n) option.

Score:0
sa flag

Make a new directory and name the directory whatever name you choose. Change directories with cd to the new directory that you made, so that all of the files that are created by the shell script will be created inside the new directory and only these newly created files will be renamed. Then run the following shell script.

#!/bin/bash
for i in $(seq 1 100)
do
echo $RANDOM > ${i}.txt # Put a random number inside each file.
mv ${i}.txt $(head -1 ${i}.txt).txt # Rename the files.
done
ls *.txt | sort -n # Sort the files in ascending order.

Your question is ambiguous because I could also rename the files in ascending order with new names that range from 1.txt to 100.txt. If that's how your want the files to be renamed use this shell script instead.

#!/bin/bash
for i in $(seq 1 100)
do
echo $RANDOM > ${i}.txt # put a random number inside each file
mv ${i}.txt $(head -1 ${i}.txt).txt # rename files
done
# ls *.txt | sort -n # I commented out this line and added a new line after it.
i=1; for filename in `ls *.txt | sort -n`; do mv "$filename" "$((i++)).txt"; done
Doug Smythies avatar
gn flag
The question seems like a homework type question, and as such I think we shouldn't give an answer.
karel avatar
sa flag
@DougSmythies I sympathize with you, but the consensus of the Ask Ubuntu community for the past 10 years is that bash shell scripts are on topic at Ask Ubuntu and programming assignments in other programming languages including homework in other programming languages are off topic. Your only recourse is to submit a request for discussion of this issue to Ask Ubuntu Meta. I would be happy to accept whatever the consensus of the Ask Ubuntu community is on this matter.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.