LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-12-2012, 02:18 PM   #1
Eppo
Member
 
Registered: Feb 2007
Location: NY
Distribution: Arch, Ubuntu
Posts: 77

Rep: Reputation: 27
perl script to modify text file


ok, so lets say i have a text file formatted something like this:
Code:
blah blah blah
blah blah blah
****************
*              *
*              *
****************


blah blah blah
blah blah blah
****************
*              *
*              *
****************

etc...
basically what i need to do is get that box in between the two blah blah blah lines.
I cant think of an elegant way to accomplish this..
can anyone give me a hand?
thanks
 
Old 04-12-2012, 02:21 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Would this do:
Code:
grep '*' file
Sample:
Code:
 $ cat file 
blah blah blah
blah blah blah
****************
*              *
*              *
****************


blah blah blah
blah blah blah
****************
*              *
*              *
****************
$ grep '*' file 
****************
*              *
*              *
****************
****************
*              *
*              *
****************
Hope this helps.
 
Old 04-13-2012, 08:14 AM   #3
Eppo
Member
 
Registered: Feb 2007
Location: NY
Distribution: Arch, Ubuntu
Posts: 77

Original Poster
Rep: Reputation: 27
the actual format is a bit more complicated than i simplified above, its actually more like a block of text, then the box then another block of text. this repeats thoughout the file.
So just to see if there is a better way of doing it, what i did was to create 3 arrays, BEGIN, BOX, END, I just did a push(@ARRAY, "$"); based on whether the line came from the first block of text or the box, or the end block of text. once i hit the beginning of the first block again i would print out the arrays in the revised order, and then set them to ''.
I just realized while i was typing this, that i'm going to have to add to it, because i will lose the last group because it will hit EOF without seeing the beginning of a new BEGIN block of text.
Is the way i did it fine, or is there a more elegant way of doing it?
thanks

Joe
 
Old 04-13-2012, 09:23 AM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Help us to help you. You gave a sample input file (that's good) and some words (also good). Construct a sample output file which corresponds to your sample input and post it here. With a "Before and After" example we can better understand your needs and also judge if our proposed solution fits those needs.

Daniel B. Martin
 
Old 04-14-2012, 04:53 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Better still, compose a verbal description of the layout. Use terms that give absolute and relative orientation. For instance, "A" always starts/ends/contains "XX" or "YY", and the sections to extract start "n" lines following "A" and end with "ZZ". Once you've done this, the code will almost write itself in many programming languages. A sample of the input data, and the desired output is also very helpful; like painting a picture. Most often, there is a very large universe of algorithms that will get you from the single example input to the single example output, but very few algorithms that will work in all cases.

--- rod.
 
  


Reply


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
could perl script modify a file directly ?? casualzone Programming 14 08-07-2011 03:32 AM
Perl Script to replace the number from text file kishorebabue Linux - Newbie 8 07-02-2010 09:53 AM
run script to modify text file alejandroye Programming 11 03-21-2010 11:40 AM
Modify a text files with awk/sed/perl climber75 Programming 15 08-05-2008 03:35 PM
Create a script that will modify a text file KSUA Linux - General 5 03-16-2008 08:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:57 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