LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 09-20-2011, 12:46 PM   #1
SilversleevesX
Member
 
Registered: May 2009
Posts: 181
Blog Entries: 9

Rep: Reputation: 15
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.
 
Old 09-20-2011, 01:38 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
You can do something like this:
Code:
# Load the file into an array
mapfile -t someArray < someFile

# Display records 2, 4, and 1 in order
for i in 2 4 1 ;do
   echo "${someArray[$i]}"
done
 
1 members found this post helpful.
Old 09-20-2011, 03:25 PM   #3
SilversleevesX
Member
 
Registered: May 2009
Posts: 181

Original Poster
Blog Entries: 9

Rep: Reputation: 15
That almost works.

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.
 
Old 09-20-2011, 04:05 PM   #4
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
The index is zero-based by default, and for lines with embedded spaces, you don't want the '-t' option (it would help to read the bash man page).
 
1 members found this post helpful.
Old 09-20-2011, 05:11 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
1 members found this post helpful.
Old 09-20-2011, 05:24 PM   #6
SilversleevesX
Member
 
Registered: May 2009
Posts: 181

Original Poster
Blog Entries: 9

Rep: Reputation: 15
Quote:
Originally Posted by macemoneta View Post
The index is zero-based by default, and for lines with embedded spaces, you don't want the '-t' option (it would help to read the bash man page).
I get you: 0 through 5 (or what have you) not 1 through 6 (likewise).

I'll try that.

BZT
 
Old 09-20-2011, 06:06 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.

Last edited by chrism01; 09-20-2011 at 08:53 PM.
 
1 members found this post helpful.
Old 09-20-2011, 07:31 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] w. bash open file, cut n char from begin of each line, wrt shortened lines to new fil DearWebby Programming 3 12-14-2010 01:28 AM
Rearrange lines in a file coady77 Linux - Newbie 17 06-12-2009 10:35 AM
bash : read every line from text file starting at given line number quadmore Programming 4 02-20-2009 12:29 PM
print a text file with long lines and line number added powah Linux - General 2 05-26-2006 02:02 PM
Print line number X of a file (in shell) rheza Programming 4 01-04-2005 05:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration