The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need a regular expression tony3101 Shell Programming and Scripting 4 06-05-2008 03:13 AM
regular expression and awk nickg UNIX for Dummies Questions & Answers 2 08-16-2007 05:23 PM
regular expression...help!! andy2000 UNIX for Dummies Questions & Answers 6 07-18-2007 05:10 PM
help in regular expression Rakesh Ranjan High Level Programming 5 10-25-2006 01:00 PM
Regular Expression + Aritmetical Expression Z0mby Shell Programming and Scripting 2 05-21-2002 10:59 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-30-2008
uttamhoode uttamhoode is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 18
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
  #2 (permalink)  
Old 05-30-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Something like this?
Code:
awk '
$0=="GS"{ 
  printf("%s\n%s\n%s\n%s\n",$0,"setlinewidth","25 110 moveto","75 110 lineto")
  next
}
{print}
' file
Regards
  #3 (permalink)  
Old 05-30-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
You need to add -0777 to the Perl command line to make it read the whole file in one go, rather than one line at a time. You also need to add an /s modifier to the regex substitution to make dot match a newline (you ought to have needed this in your proof of concept on $data, too).
  #4 (permalink)  
Old 05-30-2008
uttamhoode uttamhoode is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 18
can u please check this.it didnt work for me


perl -0777 -p -i -e 's/%%Page: (4) 4(.*?)GS/%%Page: (4) 4 $1 GS setlinewidth \n 25 110 moveto \n 75 110 lineto/s' filename
  #5 (permalink)  
Old 05-30-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Try this:

Code:
perl -i.bck -00 -ple'
s/(%%Page: \(4\) 4.*?GS)/$1\nsetlinewidth \n25 110 moveto \n75 110 lineto/gs
' filename
If you have empty lines between Page: and GS you should use -0777 as era already mentioned.
  #6 (permalink)  
Old 05-30-2008
uttamhoode uttamhoode is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 18
hi all,
thanks for the reply

all the solutions are working fine(after using -0777).
actully i am calling above commands in perl script and i forgot to escape speacial charactes (% $ \n etc).


#!/usr/bin/perl

......................
.........................


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

..................
................

Thanks and Regards,
uttam hoode
Closed Thread

Bookmarks

Tags
perl, perl regex, regex

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate 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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:31 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0