LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-22-2009, 06:58 AM   #1
vijay_babu1981
Member
 
Registered: Nov 2004
Location: india
Distribution: fedora 10
Posts: 82

Rep: Reputation: 19
Help with Regular Expression in VIM search and replace


I have two type of lines in a text file

example
Code:
x.junk(a,b,YAHOO);
y.junk(c,d);
I want to search and replace the second type of line with the first type i.e. with YAHOO included, so that I have


Code:
x.junk(a,b,YAHOO);
y.junk(c,d,YAHOO);
I am trying to use
Code:
:%s/\(.*\)\.junk(\(.*\),\(.*\)^\<YAHOO\>);/\1.junk(\2,\3,YAHOO);/
In the above code ^\<YAHOO\> search is not working (I want lines "except YAHOO"). I know that [^ab] will work for "except a,b" characters. How can I say "except word" in regular expressions?

Regards,
 
Old 10-22-2009, 07:19 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
I don't speak VIM, so I'll work this in SED.

How about:

Code:
sed '/YAHOO/!s/);/,YAHOO);/'
 
Old 10-22-2009, 07:32 AM   #3
vijay_babu1981
Member
 
Registered: Nov 2004
Location: india
Distribution: fedora 10
Posts: 82

Original Poster
Rep: Reputation: 19
Hi pixellany,
Thanks for reply. I dont speak much in sed but I feel that
Code:
sed '/YAHOO/!s/);/,YAHOO);/'
will work if only the said two types of lines are present. That is, it will search all lines without YAHOO and insert YAHOO.

Unfortunately I have thousands of lines in the text file (C code; each line ends with ');') with all sort of junk in it. After running this code I will have YAHOO in all lines.

I may be mistaken, I have not tried your code. Interestingly equivalent of collecting chars between // to form a word and then negation ("!s") in VIM is what I need to do this in VIM.

Regards
 
Old 10-22-2009, 07:34 AM   #4
vijay_babu1981
Member
 
Registered: Nov 2004
Location: india
Distribution: fedora 10
Posts: 82

Original Poster
Rep: Reputation: 19
Quote:
Originally Posted by pixellany View Post
I don't speak VIM, so I'll work this in SED.

How about:

Code:
sed '/YAHOO/!s/);/,YAHOO);/'
Can I use the rest of the regular expression which I wrote in my original post as it is in sed along with your suggestion?

I mean will the back-referencing using \1 etc will work?

Regards
 
Old 10-22-2009, 07:39 AM   #5
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 420

Rep: Reputation: 86
Quote:
x.junk(a,b,YAHOO);
y.junk(c,d);
I take it that your lines start with a lowercase letter
followed by a '.' (dot) followed by an opening parenthesis '('
then a lowercase letter followed by a comma ',' and a lowercase letter
in which case this should work in vi:

Code:
:%s/^\([a-z]\.junk([a-z],[a-z]\)\()\)/\1,YAHOO\2/
It looks for strings that start with a single letter
followed by '.' then 'junk(' then a letter, then ','
then a letter
if the next character is ')' it inserts ',' then 'YAHOO'
before the closing ')'
 
Old 10-22-2009, 08:28 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
OK---so we need to know the complete set of conditions that you want to match (or not match).

If I remember correctly, a back-reference only works with the "s" command.
 
Old 10-22-2009, 08:33 AM   #7
vijay_babu1981
Member
 
Registered: Nov 2004
Location: india
Distribution: fedora 10
Posts: 82

Original Poster
Rep: Reputation: 19
Hi vonbiber

I was trying to simplify the problem with the example I stated. Unfortunately your solution does not help in my actual case which is much more complicated.

All i am looking for is "exception of a word". I want to exclude lines where some known words (or collection of chars) appear. The sed example of grouping with // and then negation !s was perfect. I dont know whether sed will take other parts of my regular expression such as back referencing with \1 etc. I am trying to avoid reading tutorials on sed so its equivalent in vim will be useful.

Regards
 
Old 10-22-2009, 08:45 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Here is another trick:

Code:
sed '/patt1/{/patt2/!s/a/b/}'
In all lines containing "patt1", but not containing "patt2", replace the first instance of "a" with "b".

You can continue nesting, but I don't know how deep

Code:
sed '/ab/{/cd/{/xy/!s/foo/bar/}}'
Matches a line containing "ab" and "cd", but NOT "xy"

And you could put and address RANGE in front of the whole thing.....
 
Old 10-22-2009, 08:49 AM   #9
vijay_babu1981
Member
 
Registered: Nov 2004
Location: india
Distribution: fedora 10
Posts: 82

Original Poster
Rep: Reputation: 19
Okay so here is the actual problem with most of its complexity:

I have following type of lines (apart from other garbage)
Code:
xy.junk(x1y1,x2y2,x3y3);
xy.junk(x1y1,x2y2,x3y3(k+j));
ab.junk(a1b1,a2b2,a3b3,"ab",YAHOO,GOOGLE);
ab.junk(a1b1,a2b2,a3b3(g+q)-7*r,"ab",YAHOO,YAHOO);
Now we can see that function junk has sometimes 3 arguments, sometimes 6.

I want that function to always have 6 arguments.

Code:
xy.junk(x1y1,x2y2,x3y3);
xy.junk(x1y1,x2y2,x3y3(k+j));
should become
Code:
xy.junk(x1y1,x2y2,x3y3,"xy",YAHOO,GOOGLE);
xy.junk(x1y1,x2y2,x3y3(k+j),"xy",YAHOO,GOOGLE);
rest of the lines should be untouched.

Now i am trying to search
Code:
some1.junk(some2,some3,some4);
and avoid anything of the form
Code:
some1.junk(some2,some3,some4,some5,...);
I will need to refer some1 so that I can put it as the 4th argument within quotes.

Regards
 
  


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
What is meaning about the regular expression pertaining to vim script? haochao Programming 2 03-25-2009 01:08 AM
vim search and replace only one line hakmed Programming 5 11-07-2006 08:13 PM
vim: search pattern / replace with file naflan Programming 5 01-07-2006 04:16 PM
Kate - Regular Expression Replace and Other Features? apachedude Linux - Software 0 01-04-2005 02:49 PM
regular expression search/replace question (HELP!!! :) amytys Programming 5 09-06-2004 03:36 PM


All times are GMT -5. The time now is 04:56 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration