BASH: rearrange lines in a file by line number & print in that order
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
BASH: rearrange lines in a file by line number & print in that order
Here's what I've tried with sed and here's what I'm getting.
From this list in a file called "baseball"
Code:
Bats
Balls
Bases
Caps
Mitts
Uniforms
if I try
Code:
sed -n -e '1p;' -e '3p;' baseball
I get
Code:
Bats
Bases
but if I should add another line number, as I did here
Code:
sed -n '2p; 4p; 1p' baseball
I get
Code:
Bats
Balls
Caps
which are the right lines, just not in the order I asked for them. When I add another line number reference (e.g: 5p), it returns the first four lines in the file, but not line 5, which is what I asked for.
I'm thinking that because sed processes things in a stream (hence the name), and can find and return single lines, line pairs and ranges, it can't do anything as fancy as return many lines by their line number in any order that "goes against the stream."
So what does do this?
Or better, what would be a better way of reading in lines like the ones in baseball, and returning them in a "custom" order?
BZT
Last edited by SilversleevesX; 09-20-2011 at 12:51 PM.
I added the word "Catchers" to the data file, and tried to use it to print every line in an arbitrary order
Code:
mapfile -t someArray <baseball
for i in 6 3 2 4 5 1 ; do echo "${someArray[$i]}"; done
The result was
Code:
Catchers
Caps
Bases
Mitts
Uniforms
Balls
That looked satisfactory. So I tried it on another 6-line text file
Quote:
Adele,25
Gemma J,24
Eloise,25
McKenzie,25
Annabella,23
Lana C,28
using something other than someArray as my variable.
Code:
mapfile -t cuteAussies <girls-image07.txt
for i in 6 3 2 4 5 1 ; do echo "${cuteAussies[$i]}"; done
This gave me
Quote:
McKenzie,25
Eloise,25
Annabella,23
Lana C,28
Gemma J,24
Here you'll notice there's no 6th line, but a blank line instead. There's also one missing from the set in the for/do/done loop, and as a result the lines that do appear aren't in the order I "requested."
Why should that be? Could the source data text file be corrupt, or is there another possibility?
BZT
Last edited by SilversleevesX; 09-20-2011 at 03:32 PM.
Reason: Dressing it up a bit.
Yes, sed operates sequentially from start to finish. It reads in one line at a time and attempts to apply all the given expressions to them. So the output of the OP examples will always be in the order that they appear in the file.
Yeah, its historical.
Basically way back (1970 - ish), after a couple of attempts in various langs, Unix was re-written in C, and C arrays begin at zero because they are defined as mem_address + offset.
Most tools/langs developed on *nix follow the same pattern.
You could do the same process as mapfile / for loop with awk. Simply save all the data and then print the order you like.
If you use NR as the index you will also be able to use 1 to 6 for the line numbers.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.