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
how to change every 3rd character manas_ranjan Shell Programming and Scripting 2 12-15-2007 06:18 AM
Trying to remove single character from a line Iz3k34l UNIX for Dummies Questions & Answers 5 07-07-2007 01:29 PM
change character case ajaya UNIX for Dummies Questions & Answers 3 03-23-2006 03:22 PM
Single character wildcard for SED stevefox Shell Programming and Scripting 8 02-16-2006 01:23 AM
reading a single character in C alodha High Level Programming 3 01-28-2005 06:22 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 08-30-2006
Brett Taylor Brett Taylor is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 1
Post Vi single character change

I know this is strictly a programming forum - but I consider vi a programming enabler and the question relates to regex you'd use with awk/sed anyway....

I have a file which is 50,000+ lines long and need to change many many instances of

word_word_word

to be

word+word+word

where 'word' can be any 'word' and it is the underscore character that needs to be changed to a plus character.

I'm happy with

:1,$ s/some pattern/new pattern/g

type changes, but can't seem to nail this.

I've tried

:1,$ s/[Aa-Zz]_[Aa-Zz]/[Aa-Zz]+[Aa-Zz]/g

but that puts many [Aa-Zz] patterns in the file......

Any help humbly appreciated.

-----------------------------------------------------------
Brett
  #2 (permalink)  
Old 08-30-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Brett, buddy...
Code:
%s/\([^_][^_]*\)_\([^_][^_]*\)_\([^_][^_]*\)/\1+\2+\3/g
or similarelly with sed:
Code:
sed 's/\([^_][^_]*\)_\([^_][^_]*\)_\([^_][^_]*\)/\1+\2+\3/g' myFile.txt > myNewFile.txt
  #3 (permalink)  
Old 08-30-2006
dsravan dsravan is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 188
Vgresh,

Can you explain what does the numbers 1 2 3 siginfy here in the script.

Quote:
%s/\([^_][^_]*\)_\([^_][^_]*\)_\([^_][^_]*\)/\1+\2+\3/g
Please help me to understand.
  #4 (permalink)  
Old 08-30-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by dsravan
Vgresh,

Can you explain what does the numbers 1 2 3 siginfy here in the script.



Please help me to understand.
quoting 'man sed'
Code:
                  The characters \n, where  n  is  a
                  digit, will be replaced by the text matched by
                  the  corresponding  backreference  expression.
                  For each backslash (\) encountered in scanning
                  replacement from beginning to end, the follow-
                  ing  character  loses  its special meaning (if
                  any).  It is unspecified what special  meaning
                  is  given  to any character other than &, \ or
                  digits.
let me 'color-code' it:
Code:
 %s/\([^_][^_]*\)_\([^_][^_]*\)_\([^_][^_]*\)/\1+\2+\3/g
  #5 (permalink)  
Old 08-30-2006
dsravan dsravan is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 188
One more question

Thanks Vgresh. One more question please.

What does

Quote:
[^_] represent in the code
?

^ signifies starting of the line. But what does _ represent?

I appreciate your reply.

Thanks friend.
  #6 (permalink)  
Old 08-30-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
quoting from 'man regexp':
Code:
 1.4      A non-empty string of characters  enclosed  in  square
           brackets  ([])  is a one-character RE that matches any
           one character in that string. If, however,  the  first
           character  of the string is a circumflex (^), the one-
           character RE matches any character except new-line and
           the remaining characters in the string. The ^ has this
           special meaning only if it occurs first in the string.
           The  minus (-) may be used to indicate a range of con-
           secutive characters; for example, [0-9] is  equivalent
           to  [0123456789].  The - loses this special meaning if
           it occurs first (after an initial ^, if any)  or  last
           in  the  string. The right square bracket (]) does not
           terminate such a string when it is the first character
           within  it  (after an initial ^, if any); for example,
           []a-f] matches either a right square  bracket  (])  or
           one  of  the  ASCII letters a through f inclusive.
given the above, the ' [^_]' means: any character BUT the '_'
Closed Thread

Bookmarks

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 10:20 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