Hey everyone!
This is my first time posting on this forum.
I am kind of new to Linux and bash, but I have been learning. I recently read the article Using head Command in Linux, and I completed all of the exercises except one.
For Intermediate Users #1: " Display the first five lines of multiple files, sorted alphabetically by file name (Hint: pipe to sort command)"
Obviously, my first attempt was like this:
head -n 5 haruki.txt agatha.txt | sort
But that sorts all of the lines along with the file names, so everything gets jumbled. Like below:
==> agatha.txt <==
A Wild Sheep Chase (1982)
Hard-Boiled Wonderland and the End of the World (1985)
==> haruki.txt <==
Hear the Wind Sing (1979)
Norwegian Wood (1987)
Pinball, 1973 (1980)
The Man in the Brown Suit
The Murder on the Links
The Mysterious Affair at Styles
The Secret Adversary
The Secret of Chimneys
My reading of the prompt for this task is that top 5 lines of agatha.txt would be sorted, then the top 5 lines of haruki.txt would be sorted, so the above isn’t right. I have tried reading the man page for sort, but there wasn’t anything there that would allow me to ignore the title line, and then only sort the the titles per file. Well, other than sorting the files outside of the head command and then running the head command (which would work).
Is there a more elegant way to do this, or am I just misreading the prompt and the above is what was wanted?
Thanks!