2

File1:

Brazil
New Zealand
Argentina
Uruguay

File2:

Chile
Russia
Latvia
Switzerland

I realize that the command

sort -n File1 File2

will sort all the files merged together and display it in stdout.

But I only want to sort the files individually and not together.

Is there a 1 line bash command that sort both at the same time and display it's contents separately in stdout?

Something that'd give me stdout of:

File 1:
Argentina
Brazil
New Zealand
Uruguay

File 2:
Chile
Latvia
Russia
Switzerland
  • 2
    sort file1; echo; sort file2 – Jeff Schaller May 29 '16 at 16:45
  • Note that the -n option means to do a numeric sort which is irrelevant for text even if it works. – Julie Pelletier May 29 '16 at 16:46
5

For two files, you can just run two commands:

echo File 1: ; sort File1 ; echo File2: ; sort File2

For more files, a loop might be more suitable:

for file in *.txt ; do echo "$file" ; sort "$file" ; done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.