LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
LinkBack Search this Thread
Old 04-04-2012, 12:56 PM   #1
afroresurrection
LQ Newbie
 
Registered: Jan 2012
Posts: 6

Rep: Reputation: Disabled
Changing the order of rows in multiple blocks in a config file.


Alright, so let's say I've got this file that has some rows organized into multiple blocks e.g.

# user 1
---------------------------------------
0. ; jitor
1. [0001](closet-space)
2. secret=pennyRevenge
3. mailbox=0001@default
4. callerid=Jan Itor <555-555-5555>
---------------------------------------

This repeats for every new user. What I'd like to know is if it's possible to reorganize each block so line 0 is deleted and line 4 moves to the top. The numbers on the side have been added for clarification.

Also, I'm not really looking for a specific solution so much as some tips on how I can script this on my own. Thanks!

Last edited by afroresurrection; 04-04-2012 at 12:59 PM.
 
Old 04-04-2012, 01:57 PM   #2
jsveiga
Member
 
Registered: Aug 2005
Location: Brazil
Distribution: debian on servers and embedded, kubuntu elsewhere
Posts: 31

Rep: Reputation: 1
Hi, there's probably an infinite number of ways to do it, but here's one that you can tailor to your needs;
_______________________
Code:
#!/usr/bin/perl
use warnings;
use strict;
my $blockstartre = qr{^#};
my @outorder = (0, 1, 6, 3, 4, 5, 7);
my @buffer = ();
sub output()
{
   foreach (@outorder)
   {
      print $buffer[$_]."\n" if ($_ <= $#buffer);
   }
   @buffer = ();
}
while (<STDIN>)
{
   chomp;
   output() if (m/$blockstartre/);
   push (@buffer, $_);
}
output();
______________________

Usage: cat origfile | thisscript > newfile
Note that the @outorder specifies the block lines and order you want in the output, considering that 0 is the block start identifier (lines starting with #).

Sorry if I spoiled the fun of scripting it yourself, but there's still plenty of room there for sophistication.

BR,

Joao S Veiga
 
Old 04-04-2012, 04:09 PM   #3
afroresurrection
LQ Newbie
 
Registered: Jan 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Much obliged, maybe I can salvage some learning experience with this and convert it to python. Would you mind describing to me how you decided to use the buffer and push sections of the script? Is it just through reading perl documentation?
 
Old 04-05-2012, 07:23 AM   #4
jsveiga
Member
 
Registered: Aug 2005
Location: Brazil
Distribution: debian on servers and embedded, kubuntu elsewhere
Posts: 31

Rep: Reputation: 1
Hi, you are welcome;

I used a buffer to be able to switch the lines positions, since you required that a line jumped "up". Like if you receive a bunch of emails, but you need to forward them to someone else in a different sequence: You cannot forward each one upon receiving, but let them sit in your mailbox until you have a complete set to reorder and forward in any desired sequence - hence the buffering. If you only had required that a line was suppressed, no buffering would be required; I'd just count the lines on each block and print/not print out the lines as soon as they were read. Whenever you need to change the order of an input before outputting (for example, doing a "sort") you'll need to hold the information for a while, so a buffer is required.

I don't think this would be in the perl docs; the decision of using a buffer was easy, because I don't know how to do it without it - Now, if what you meant to ask was about how do I got to know that perl had the specific capabilities I used (push, array manipulation, etc), then yes, somewhere in the past I got that from reading the perl documentation (which is very good; man perlintro if you want a starting point) and the O'Reilly "Camel" book (and lots of experimenting/trial&error/fun).

Perl was born for this kind of stuff as a "Practical Extraction and Report Language", IMHO, it's the best suited tool for this specific type of task, although of course any language can achieve the same results.

BR,

Joao S Veiga
 
  


Reply

Tags
bash, debian, script, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Ninja Blocks available for pre-order, Kickstarter orders shipping now LXer Syndicated Linux News 0 03-30-2012 11:50 AM
How to force bad blocks into CF in order to decrease the capacity asca Linux - Embedded & Single-board computer 2 09-01-2010 02:17 AM
System suddenly changing disk order from BIOS order hscast Fedora 2 02-16-2010 09:09 PM
Changing httpd config file NhaldSev Linux - Software 4 01-11-2007 01:04 AM
Changing Xf86 config file toosdedoos Linux - Newbie 1 09-26-2003 09:17 AM


All times are GMT -5. The time now is 08:20 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration