The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
regexp help TinCanFury Shell Programming and Scripting 0 04-06-2008 03:21 PM
In Help, Substitute Text ... solidhelix08 Shell Programming and Scripting 6 02-07-2008 01:21 AM
regexp with sed again!!! andy2000 Shell Programming and Scripting 2 05-03-2007 07:15 AM
Substitute in vi kingdbag UNIX for Dummies Questions & Answers 5 01-17-2007 10:42 AM
regexp penfold UNIX for Advanced & Expert Users 1 06-28-2005 04:45 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-10-2004
Registered User
 

Join Date: Dec 2004
Location: Paris
Posts: 20
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
vi + regexp + substitute

Another enigma (for me)

Using VI I edit some files in which I want to erase many lines I don't need anymore

usually I do something like that:
Code:
:%s/line_to_suppress//g
and it is doing fine

but in this case, the pattern to erase contains some special characters

so I do it like that:
Code:
:%s#line_to_suppress_with_special_characters##g
but, the only problem left is that I can't predict what will be the beginning of the line so I wish I could do something like that:
Code:
:%s/*line_to_suppress//g
but of course if I protect the pattern with # to prevent special characters to annoy me... the * don't work anymore

the best way would be to suppress each line containing my pattern, but I don't know how to do that

I'm sure it's pretty easy for many among you, so if one have a bit of time, it would help me a lot

Thanx
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 12-10-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
I'm not too sure that I follow - but you can supress the interpretation of metacharacters by backslash escaping them...

e.g.
Code:
:%s/^.*my_patter_has_special_chars\*\/\$\^_in_it.*$//
As you can see, the .* at the beginning and end of line are still treated as metacharacters (as are the anchors).

Cheers
ZB
Reply With Quote
  #3 (permalink)  
Old 12-10-2004
Registered User
 

Join Date: Dec 2004
Location: Paris
Posts: 20
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Quote:
Originally posted by zazzybob
you can supress the interpretation of metacharacters by backslash escaping them...

e.g.
Code:
:%s/^.*my_patter_has_special_chars\*\/\$\^_in_it.*$//
As you can see, the .* at the beginning and end of line are still treated as metacharacters (as are the anchors).
ok but my_pattern_has_special_chars looks like
Code:
blabla 'toto' rep:/titi/toto/
so I protect it between #

if I understand what you mean, I should do like this:
Code:
:%s/^.*blabla\ \'toto\'\ rep\:\/titi\/toto\///
Am I right? (if it is the case, it si ok because my pattern never change)
Reply With Quote
  #4 (permalink)  
Old 12-10-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Quote:
Originally posted by Lomic
if I understand what you mean, I should do like this:
Code:
:%s/^.*blabla\ \'toto\'\ rep\:\/titi\/toto\///
Am I right? (if it is the case, it si ok because my pattern never change)
Looking at that, all you really need to do is
Code:
:%s/^.*blabla 'toto' rep:\/titi\/toto\///
i.e. it's only the slashes that need to be escaped.

Cheers
ZB
Reply With Quote
  #5 (permalink)  
Old 12-10-2004
Registered User
 

Join Date: Dec 2004
Location: Paris
Posts: 20
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Quote:
Originally posted by zazzybob
i.e. it's only the slashes that need to be escaped.
you're right, it is nearly ok now! (good step forward for me)

but one more question

it erase the lines, but it doesn't suppress them...

any hint?
Reply With Quote
  #6 (permalink)  
Old 12-10-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
What do you mean suppress?

Cheers
ZB
Reply With Quote
  #7 (permalink)  
Old 12-10-2004
Registered User
 

Join Date: Dec 2004
Location: Paris
Posts: 20
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Quote:
Originally posted by zazzybob
What do you mean suppress?
File before:
xxxxxxxxxxx
xxxxxxxxxxx
blabla 'toto' rep:/titi/toto/
xxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxx
blabla 'toto' rep:/titi/toto
/blabla 'toto' rep:/titi/toto/
xxxxxxxxxxx
blabla 'toto' rep:/titi/toto/
blabla 'toto' rep:/titi/toto/
xxxxxxxxxxx

File after:
xxxxxxxxxxx
xxxxxxxxxxx

xxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxx


xxxxxxxxxxx


xxxxxxxxxxx

Result I wish to have:
xxxxxxxxxxx
xxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxx
xxxxxxxxxxx
xxxxxxxxxxx

Reply With Quote
  #8 (permalink)  
Old 12-10-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
I think I'd rather use sed than vi
Code:
sed "/blabla\ 'toto'\ rep:\/titi\/toto\//d" infile > outfile
Notice that quoting and escapes have changed here...

Cheers
ZB
Reply With Quote
  #9 (permalink)  
Old 12-10-2004
Registered User
 

Join Date: Dec 2004
Location: Paris
Posts: 20
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Quote:
Originally posted by zazzybob
I think I'd rather use sed than vi
Code:
sed "/blabla\ 'toto'\ rep:\/titi\/toto\//d" infile > outfile
Notice that quoting and escapes have changed here...
ok, and like this, is it ok for the allways unknown start of the line (*) ?

and is it possible without trouble that infile=outfile?
Reply With Quote
  #10 (permalink)  
Old 12-10-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Yes, you can add metacharacters such as

sed "/^.*blabla\ 'toto'\ rep:\/titi\/toto\/.*$/d" infile > outfile

Also; you can use the -i flag to sed if your version supports it to edit the file in-place, although I personally wouldn't recommend it - at least this way with redirection you keep your original intact incase you form your "sed" badly and ruin the file!

Cheers
ZB
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
421 service not available, remote server has closed connection ^m automate ftp autosys awk trim bash eval bash exec bash for loop close_wait command copy/move folder in unix couldn't set locale correctly curses.h cut command in unix dead.letter find grep find null character in a unix file grep multiple lines grep or grep recursive inaddr_any inappropriate ioctl for device ksh if logrotate.conf lynx javascript mailx attachment mget mtime ping port remove first character from string in k shell replace space by comma , perl script scp recursive segmentation fault(coredump) sftp script snoop unix stale nfs file handle syn_sent tar exclude test: argument expected unix unix .profile unix forum unix forums unix internals unix interview questions unix mtime unix simulator unix.com vi substitute vi+substitute+end+of+line+character while loop within while loop shell script


All times are GMT -7. The time now is 07:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101