Vi single character change


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Vi single character change
# 1  
Old 08-30-2006
Java 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  
Old 08-30-2006
Brett, buddy...
Code:
%s/\([^_][^_]*\)_\([^_][^_]*\)_\([^_][^_]*\)/\1+\2+\3/g

or similarelly with sed:
Code:
sed 's/\([^_][^_]*\)_\([^_][^_]*\)_\([^_][^_]*\)/\1+\2+\3/g' myFile.txt > myNewFile.txt

# 3  
Old 08-30-2006
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  
Old 08-30-2006
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  
Old 08-30-2006
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  
Old 08-30-2006
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 '_'
# 7  
Old 08-30-2006
Thanks a lot for clarifying

Thanks Vgresh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to repeat a character in a field if it's a single character?

I have a csv dataset like this : C,rs18768 G,rs13785 GA,rs1065 G,rs1801279 T,rs9274407 A,rs730012 I'm thinking of use like awk, sed to covert the dataset to this format: (if it's two character, then keep the same) CC,rs18768 GG,rs13785 GA,rs1065 GG,rs1801279 TT,rs9274407... (7 Replies)
Discussion started by: nengcheng
7 Replies

2. Shell Programming and Scripting

How to match character with single quote?

I need to check whether first character of variable is single quote. I tried the below constructions but they are all not working (always return true) if (test `echo "$REGEXP" |cut -c1` != "'"); then echo "TRUE"; fi if (test `echo "$REGEXP" |cut -c1` != '\''); then echo "TRUE"; fi if (test... (5 Replies)
Discussion started by: urello
5 Replies

3. Programming

Macro to put single quotes around character

Dear Mates, I am trying to write a macro to replace with its arguments with singles quotes around each argument. #define DECR(a,b,c,d,e) decr('a','b','c','d','e') if the usage is DECR(k,e,y,s,\0) I want it to be replaced with the following decr('k','e','y','s','\0') However it... (2 Replies)
Discussion started by: tamil.pamaran
2 Replies

4. UNIX for Dummies Questions & Answers

Backspace to delete a single character

Hi I normally use backspace to delete a single on my local machine in cygwin. Now I am trying cygwin on a virtual machine and when I type 'backspace', instead of a single character being deleted, the entire word gets deleted. How do I change the settings for it? I read somewhere that I... (2 Replies)
Discussion started by: ladyAnne
2 Replies

5. Shell Programming and Scripting

awk - setting fs to equal any single character

Hi Does anyone know how to set any character as the field separator with awk/nawk on a solaris 10 box. I have tried using /./ regex but this doesnt work either and im out of ideas. thanks (7 Replies)
Discussion started by: chronics
7 Replies

6. Shell Programming and Scripting

Replace multiple occurances of same character with a single character.

Hi all, Greetings, I have the following scenario, The contents of main file are like : Unix|||||forum|||||||||||||||is||||||the||best so||||||be|||||on||||||||||||||||||||||||||||||||||||||||||||it And i need the output in the following form: Unix=forum=is=the=best so=be=on=it ... (3 Replies)
Discussion started by: dipanchandra
3 Replies

7. UNIX for Dummies Questions & Answers

Change case of single character in pattern

Using UNIX tools, but not using GAWK (NAWK is OK), is there a more elegant way to achieve this: sed ' s/_a/_A/1 s/_b/_B/1 s/_c/_C/1 rest of alpahabet ' I want to change the case of a single character in each string of a text file. The character will be matched by regex '_' and only... (2 Replies)
Discussion started by: uiop44
2 Replies

8. UNIX for Dummies Questions & Answers

Trying to remove single character from a line

Here is a sample code grep '903' -i user.txt | tail -2 | awk '{print $2}' | sed 's/B//g' the input file has data as such 903-xxx-xxxxB 903-xxx-xxxxB It is a dialer file i want to remove the "B" any help thanks (5 Replies)
Discussion started by: Iz3k34l
5 Replies

9. Shell Programming and Scripting

Single character wildcard for SED

Could someone tell me the single character wildcard for SED? I have the file below: $ more input2 AAA /A/B/C BBB /D/E/F CCC /G/H/I DDD I want to remove all strings which contain forward slashs "/" to get the below: AAA BBB CCC I tried to do it in SED by the command below but I... (8 Replies)
Discussion started by: stevefox
8 Replies

10. Programming

reading a single character in C

Can anyone help me????? My problem is that i want to read only one charcter from keyboard. Each time my program waits to press enter or ^d. I don't want that. As soon as i press a charcter it should proceed to next statement in program without pressing enter or ^d. please help... (3 Replies)
Discussion started by: alodha
3 Replies
Login or Register to Ask a Question