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
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
05-30-2008, 07:01 AM
#1
LQ Newbie
Registered: Sep 2002
Location: mangalore,india
Distribution: redhat
Posts: 28
Rep:
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
05-30-2008, 09:30 AM
#2
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,515
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.
05-30-2008, 01:36 PM
#3
HCL Maintainer
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450
Rep:
The problem with the original is that you end up reading line by line, so you never get the entire preamble. Solutions include:
Match only the relevant portion of the last line as druuna suggested.
Read the file either in slurp mode or in paragraph mode (see perldoc perlrun and the -0 flag).
05-30-2008, 01:52 PM
#4
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu (x86), Debian (PPC)
Posts: 3,528
Rep:
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;
}
05-30-2008, 05:00 PM
#5
Member
Registered: May 2007
Distribution: Debian
Posts: 754
Rep:
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.
05-30-2008, 05:42 PM
#6
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu (x86), Debian (PPC)
Posts: 3,528
Rep:
Quote:
Originally Posted by
Telemachos
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.
05-30-2008, 05:45 PM
#7
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu (x86), Debian (PPC)
Posts: 3,528
Rep:
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
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 05:11 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News