LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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-15-2011, 08:08 PM   #1
gazzatav
LQ Newbie
 
Registered: Jul 2010
Location: Stockport UK
Distribution: Ubuntu
Posts: 7

Rep: Reputation: 3
sed - replace alternate occurrences (on different lines)


I'm trying to process a postscript file and I want to change alternate pages by finding the string '%%EndPageSetup' (and then adding a line after it).

I've tried using sed with labels and a branching program to ignore the odd occurrences and process the even ones. In the code below I tried searching for the first and ignoring it by jumping to next and processing it and then to the next odd page and so on.

My logic is obviously flawed here as it doesn't work but I've tried the following:

Code:
#!/bin/bash
sed -e '/%%EndPageSetup/{
b even
:odd
/%%EndPageSetup/ b even
:even
s/%%EndPageSetup/%%EndPageSetup++/ b odd}' filename.ps > newfile.ps
Any help appreciated.

Last edited by gazzatav; 04-15-2011 at 08:16 PM.
 
Old 04-16-2011, 02:33 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Well I am not sure sed is the right tool here as it doesn't really do counting as such.
Maybe awk can help:
Code:
awk '/%%EndPageSetup/{++a}!(a%2){sub(/%%EndPageSetup/,"&++")}1' filename.ps > newfile.ps
 
Old 04-16-2011, 05:01 AM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

grail is right. Whenever it comes to tasks that require counting, 'sed' would not be the first tool of choice. The commands can get quite obfuscated.
However, this is one of the reasons I still like to think about possible sed solutions every now and then; it makes you think "out of the box" - even from a programmer's point of view.
Since you did not provide some representative sample data I had to guess - hope I guessed right:
Code:
%%startPageSetup
some stuff
%%EndPageSetup
%%startPageSetup
some stuff
%%EndPageSetup
%%startPageSetup
some stuff
%%EndPageSetup
%%startPageSetup
some stuff
%%EndPageSetup
%%startPageSetup
some stuff
%%EndPageSetup
%%startPageSetup
some stuff
%%EndPageSetup
And here is how you could process the above sample:
Code:
sed -r '/%%EndPageSetup/ {x;/%%EndPageSetup[[:blank:]]*$/ {s/(%%EndPageSetup).*/\1++/;h;b};g}' file
 
Old 04-16-2011, 09:31 AM   #4
gazzatav
LQ Newbie
 
Registered: Jul 2010
Location: Stockport UK
Distribution: Ubuntu
Posts: 7

Original Poster
Rep: Reputation: 3
Thanks, both of those solutions work perfectly. That is exactly what I wanted. I can now process a postscript file with psbook to obtain signatures (groups of pages) and then move alternate pages out from the gutter as below (to provide a bit more context).

Code:
...
%%EndPageSetup
...
%%EndPageSetup
42 0 translate
...
%%EndPageSetup
...
%%EndPageSetup
42 0 translate
I shall have to read up more on awk and sed. Just for my better understanding: In the awk solution is the 'a' variable initialized with the 1 after the brace? In the sed solution is the 'b' command still a branching instruction? If so where does it branch to?

Thanks again.
 
Old 04-16-2011, 10:20 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
All variables in awk are set to 0 or empty before first invocation. This might help with the 'b' in sed.
 
Old 04-16-2011, 11:46 AM   #6
gazzatav
LQ Newbie
 
Registered: Jul 2010
Location: Stockport UK
Distribution: Ubuntu
Posts: 7

Original Poster
Rep: Reputation: 3
I see, thanks, I'd read that to get my original attempt but missed that b on its own branches to the end of the script.
 
  


Reply

Tags
branch, label, sed



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
[SOLVED] sed remove all occurrences in a string hattori.hanzo Linux - Newbie 5 11-22-2010 04:46 AM
[SOLVED] Need sed help: s/ command won't replace two occurrences of pattern on same line GrapefruiTgirl Programming 7 12-16-2009 02:08 AM
need sed help - how to replace all instances of X except those on lines with Y? BrianK Programming 4 03-25-2008 06:49 PM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
replacement with sed: replace pattern with multiple lines Hcman Programming 5 11-18-2004 07:40 AM

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

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