Want command for this question

A list of university teachers is made up of records in a fixed format and has the following structure:

teachers.txt: EmpID | Name | Specialization | Department | DOB | CTC

The fields are delimited by a "|" character. The file is available at /tmp/teachers.txt.

Sort the file on the DOB field and store the output at /tmp/sorted.log.

Sample Input File (/tmp/teachers.txt):

8790 | Daniel Clark | Insurance, Accounts | Finance | 1963-06-28 | 19456

3739 | Carol Johnson | Automobile | Engineering | 1975-12-21 | 23467

3497 | Sarah Allen | Testing | Quality Assurance | 1932-11-11 | 45632

4389 | Jennifer Esparza | Marine | Engineering | 1983-03-14 | 98765

4161 | Brandon Leis | Broking and Investment | Finance | 1988-01-01 | 76589

Sample Output File (/tmp/sorted.log):

3497 | Sarah Allen | Testing | Quality Assurance | 1932-11-11 | 45632

8790 | Daniel Clark | Insurance, Accounts | Finance | 1963-06-28 | 19456

3739 | Carol Johnson | Automobile | Engineering | 1975-12-21 | 23467

4389 | Jennifer Esparza | Marine | Engineering | 1983-03-14 | 98765

4161 | Brandon Leis | Broking and Investment | Finance | 1988-01-01 | 76589

Welcome. This site is however not a script-writing service. It seeks to assist with specific issues you have on your own learning journey.

You might like to use the drop-down menu (3 bars, at the top right of the tab), click on FAQ, and pay particular attention to items 3 and 5. Then edit your post accordingly, and we will be glad to help.

sort -t '|' -k5 /tmp/teachers.txt > /tmp/sorted.log

sort = This is the command that is used to sort files.
-t '|' = This option sets the delimiter to the "|" character.
-k5 = This option tells sort to use the 5th field (DOB) as the key for sorting.

Correct if the delimiter is really one | (pipe).
Does not work if the delimiter is space-pipe-space. Then you cannot use the sort command alone...

Please wrap your code and output in triple backticks markdowns. Use the icons at the top of the editor window!

if the delimiter is space-pipe-space (|), then you cannot use the sort command alone because it only supports a single character delimiter. In this case, you can use a combination of sed and sort to extract the fields and sort them. Here's an example of how you can use these commands together

sed 's/ | /|/g' file.txt | sort -t '|' -k1

@RaziWajid
please start using markdown code tags when posting data/code samples.
You can also do the same using the icons of the editor:
Markdown

I've edited your post for now, but please adhere to the forum rules going forward.

@RaziWajid , please, read the forum FAQ's before posting - we do not just write solutions for posters, its a collaboration .

If you continue to post 'solutions' without the requester showing efforts your posts may be muted/deleted.