LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 05-30-2008, 07:01 AM   #1
uttam_h
LQ Newbie
 
Registered: Sep 2002
Location: mangalore,india
Distribution: redhat
Posts: 28

Rep: Reputation: 15
regular expression (.*?)


hi all,
i have a text file with below content

...............................
...............................
...............................
...............................
%%Page: (4) 4
%%PageBoundingBox: 34 -30 584 831
%%BeginPageSetup
%%BeginFeature: *PageSize A4
595 842 SetPageSize
%%EndFeature
%%BeginFeature: *Duplex None
false false SetDuplexMode
%%EndFeature
%%EndPageSetup
GS
.........................
................................
..........................................

i want to add some lines after GS so in perl i tried this

$data = ~s/%%Page: (4) 4(.*?)GS/%%Page: (4) 4 $1 GS setlinewidth \n 25 110 moveto \n 75 110 lineto/g;

where $data holds file content. this is working fine and the output is


...............................
...............................
...............................
...............................
%%Page: (4) 4
%%PageBoundingBox: 34 -30 584 831
%%BeginPageSetup
%%BeginFeature: *PageSize A4
595 842 SetPageSize
%%EndFeature
%%BeginFeature: *Duplex None
false false SetDuplexMode
%%EndFeature
%%EndPageSetup
GS
setlinewidth
25 110 moveto
75 110 lineto
.........................
................................
..........................................

but when i tried this with perl command line it is not working

perl -p -i -e 's/%%Page: (4) 4(.*?)GS/%%Page: (4) 4 $1 GS setlinewidth \n 25 110 moveto \n 75 110 lineto/g' FILENAME

Kindly suggest me the solution(in sed/awk/perl etc)


Thanks in advance

uttam hoode
 
Old 05-30-2008, 09:30 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Would this help:

sed 's/^GS/GS\nsetlinewidth\n25 110 moveto\n75 110 lineto/' text.file.with.below.content

Sample run:
Code:
sed 's/^GS/GS\nsetlinewidth\n25 110 moveto\n75 110 lineto/' infile
...............................
...............................
...............................
...............................
%%Page: (4) 4
%%PageBoundingBox: 34 -30 584 831
%%BeginPageSetup
%%BeginFeature: *PageSize A4
595 842 SetPageSize
%%EndFeature
%%BeginFeature: *Duplex None
false false SetDuplexMode
%%EndFeature
%%EndPageSetup
GS
setlinewidth
25 110 moveto
75 110 lineto
.........................
................................
..........................................
Hope this helps.
 
Old 05-30-2008, 01:36 PM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
The problem with the original is that you end up reading line by line, so you never get the entire preamble. Solutions include:
  1. Match only the relevant portion of the last line as druuna suggested.
  2. Read the file either in slurp mode or in paragraph mode (see perldoc perlrun and the -0 flag).
 
Old 05-30-2008, 01:52 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
An ugly, but working solution is to iterate over the lines of the file and set a flag when you're on the right page, and only test for the GS line when you know you're on the right page. e.g.

Code:
#!/usr/bin/perl

use strict;

my $do_replace = 0;
my $text_to_insert = "setlinewidth 25 110\nmoveto 75 110 lineto";

while(<>)
{
    if (/^%%Page: \(4\) 4/) { $do_replace = 1; }

    if ($do_replace)
    {
        if (s/GS/GS\n$text_to_insert/) { $do_replace = 0; }
    }

    print;
}
 
Old 05-30-2008, 05:00 PM   #5
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
If the file looks the way you describe it, why not this one liner?
Code:
perl -i.bak -pe 's/^GS$/GS\nsetlinewidth\n25 110 moveto\n75 110 lineto/' file-name
It works just fine in a test here.
 
Old 05-30-2008, 05:42 PM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Quote:
Originally Posted by Telemachos View Post
If the file looks the way you describe it, why not this one liner?
Code:
perl -i.bak -pe 's/^GS$/GS\nsetlinewidth\n25 110 moveto\n75 110 lineto/' file-name
It works just fine in a test here.
But it doesn't work when there are multiple pages and the requirement is to alter only some specific page - it alters all pages.
 
Old 05-30-2008, 05:45 PM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Here's a one-liner with sed which modifies only one page:
Code:
sed '/^%%Page: (4)/,/^%%Page: / s/GS/GS\nsetlinewidth 25 110\nmoveto 75 110 lineto/' test.ps > test_modified.ps
 
  


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
about regular expression '=~m//' littletransformer Programming 7 03-25-2008 08:31 PM
Need help with Regular Expression subaruwrx Linux - Newbie 6 09-04-2004 07:48 PM
Anyone know regular expression? ahhua Linux - Software 1 12-04-2003 08:13 AM
Regular Expression Help WeNdeL Linux - General 1 08-14-2003 10:08 AM
Regular Expression slizadel Programming 4 07-28-2003 05:16 AM


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