189k views
0 votes
We have a text file called with the following content.

90,Smith,Lauren,10-20-1990
88,Brown,Thomas,09-15-1960
100,Green,Elizabeth,01-03-1988
Note that, each line of the patients.txt file is formatted as id,lastName,firstName,dateOfBirth
Write a unix command to sort the patients alphabetically by last name and print the results to another text file called sortedPatients.txt

1 Answer

6 votes

Final answer:

Use the Unix command 'sort -t, -k2 patients.txt > sortedPatients.txt' to sort the file by last name and output to another file.

Step-by-step explanation:

To sort the patients.txt file alphabetically by last name and write the output to another text file called sortedPatients.txt, you can use the following Unix command:

sort -t, -k2 patients.txt > sortedPatients.txt

This command uses the sort program of Unix. Here's what each part of the command does:

-t, specifies that the delimiter between fields is a comma.

-k2 tells the sort command to sort the input based on the second field, which is the last name in this case.

The > symbol is used for redirecting the sorted output into the sortedPatients.txt file.

The complete command sort -t',' -k2,2 patients.txt sorts the content of patients.txt alphabetically based on last names and outputs the result. The > operator is then used to redirect the output to a new file called sortedPatients.txt.

The Unix command provided effectively sorts the patients in the patients.txt file alphabetically by last name and saves the sorted results in a new text file called sortedPatients.txt. This command ensures that the order is based on the second field (last name) while considering the comma as the delimiter.

User Barfuin
by
7.2k points