LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-17-2005, 02:57 PM   #1
Wynd
Member
 
Registered: Jul 2001
Distribution: Slackware 12
Posts: 511

Rep: Reputation: 32
Replace every other occurrence of pattern


How can I use sed (or anything else) to replace every other occurrence of a pattern?
 
Old 11-17-2005, 03:06 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Examples always help ...
Before:


After:



Cheers,
Tink
 
Old 11-17-2005, 03:23 PM   #3
Wynd
Member
 
Registered: Jul 2001
Distribution: Slackware 12
Posts: 511

Original Poster
Rep: Reputation: 32
Sorry. Here's a quick one, replacing every other "foo" with "bar":

Before:
foo foo foo foo foo foo

After:
foo bar foo bar foo bar
 
Old 11-17-2005, 05:21 PM   #4
Wynd
Member
 
Registered: Jul 2001
Distribution: Slackware 12
Posts: 511

Original Poster
Rep: Reputation: 32
Nevermind, I just wrote a C++ program to do it.
 
Old 11-17-2005, 06:11 PM   #5
Dankles
Member
 
Registered: May 2004
Location: /dev/null
Distribution: Slackware
Posts: 245

Rep: Reputation: 31
I would like to know a solution to this problem if any one has it.
also, wynd, could you post your source code in addition to any other replies?
 
Old 11-17-2005, 06:44 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Code:
awk '{for(i=2;i<=NF;i+=2){sub("foo","bar",$i)};print}' file

Cheers,
Tink
 
Old 11-17-2005, 07:25 PM   #7
Dankles
Member
 
Registered: May 2004
Location: /dev/null
Distribution: Slackware
Posts: 245

Rep: Reputation: 31
Quote:
Originally posted by Tinkster
Code:
awk '{for(i=2;i<=NF;i+=2){sub("foo","bar",$i)};print}' file

Cheers,
Tink
Nice work, thanks!
 
Old 12-14-2005, 03:27 PM   #8
Wynd
Member
 
Registered: Jul 2001
Distribution: Slackware 12
Posts: 511

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by Dankles
I would like to know a solution to this problem if any one has it.
also, wynd, could you post your source code in addition to any other replies?
Sorry for the delay but I just saw this post now. I don't know if this is the best/most efficient code, but it did what I wanted.

Code:
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char** argv)
{
    ifstream fin;
    bool bSwitch = false;
    char cInput;

    fin.open(argv[1]);

    if (!fin.is_open())
    {
        cerr << "Error: Could not open file " << argv[1] << endl;
    }
    else
    {
        while (!fin.eof())
        {
            fin.get(cInput);

            if (cInput == '\"') // Character to look for
            {
                if (bSwitch == false)
                {
                    cout << '^'; // Replace with something else
                }
                else
                {
                    cout << '\"'; // Print out unchanged
                }

                bSwitch = !bSwitch;
            }
            else
            {
                cout << cInput;
            }
        }

        cout << endl;
    }

    return 0;
}

Last edited by Wynd; 12-14-2005 at 03:28 PM.
 
Old 12-14-2005, 03:43 PM   #9
Dankles
Member
 
Registered: May 2004
Location: /dev/null
Distribution: Slackware
Posts: 245

Rep: Reputation: 31
Thanks for the code. I'll let you know how it works.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 06:56 AM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
Occurrence book(OB) nderitualex Linux - Software 0 03-21-2005 02:51 AM
replacement with sed: replace pattern with multiple lines Hcman Programming 5 11-18-2004 07:40 AM
search for pattern in files and replace mizuki26 Linux - Newbie 3 01-04-2004 11:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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