[Solved] Replace the second pattern alone in VI.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] Replace the second pattern alone in VI.
# 15  
Old 12-03-2013
Quote:
Originally Posted by vgersh99
do cat -vet myFile and post it here within code tags.
Here are the contents of my file:

cat -vet temp.lst

Code:
-- Configure test table connections ---$
$
table10 table_name_10 table10 table_name_10$
table20 table_name_20 table20 table_name_20$
table30 table_name_30 table30 table_name_30$
$
-- Configure view connections ---$
$
view10 view_name_10 view10 view_name_10$
view20 view_name_20 view20 view_name_20$
view30 view_name_30 view30 view_name_30$
$

---------- Post updated at 02:43 PM ---------- Previous update was at 02:40 PM ----------

Quote:
Originally Posted by Don Cragun
In your sample you show that every line contains exactly two occurrences of table_name in each line in your file. Is that true in the file you're editing?

Could some lines have zero or only one occurrence of table_name?

Could any lines have three or more occurrences of table_name?
Thanks, I've posted the contents of my file above. There are just 3 lines with the words "table_name", in 2 columns. I need to replace those ones in the last column (second occurrence).
# 16  
Old 12-03-2013
For the given data the following is an easy way to do it:
Code:
:g/\(.* \)\(table_name\)/s//\1test_\2/

This will prepend "test_" to the last string containing "<space>table_name" on any line that contains one or more occurrences of that string.

PS. In vi (and ed, and ex) replacing the 1st or last occurrence of something on a line is easy. Replacing the 2nd occurrence of something that may appear 2 or more times takes multiple steps.

Last edited by Don Cragun; 12-03-2013 at 03:07 PM.. Reason: Add PS.
This User Gave Thanks to Don Cragun For This Post:
# 17  
Old 12-03-2013
Yeah, it works, thanks! Smilie

I noticed that some of my files have owner prefix for tables like this:

Code:
-- Configure test table connections ---
table10 owner.table_name_10 table10 owner.table_name_10
table20 owner.table_name_20 table20 owner.table_name_20
table30 owner.table_name_30 table30 owner.table_name_30
-- Configure view connections ---
view10 owner.view_name_10 view10 owner.view_name_10
view20 owner.view_name_20 view20 owner.view_name_20
view30 owner.view_name_30 view30 owner.view_name_30

I tried the below options, it didn't do anything:

:g/\(.* \)\(table_name\)/s//\1test_\2/

or

:g/\(.* \)\(owner.table_name\)/s//\1test_\2/


Now the below command looks like it prefixes the last column instead of prefixing the exact pattern:

:g/\(.* \)\(owner\.table_name\)/s//\1test_\2/

Output:

Code:
-- Configure test table connections ---
table10 owner.table_name_10 table10 test_owner.table_name_10
table20 owner.table_name_20 table20 test_owner.table_name_20
table30 owner.table_name_30 table30 test_owner.table_name_30

Required output is:

Code:
-- Configure test table connections ---
table10 owner.table_name_10 table10 owner.test_table_name_10
table20 owner.table_name_20 table20 owner.test_table_name_20
table30 owner.table_name_30 table30 owner.test_table_name_30

# 18  
Old 12-03-2013
A variation of Don's theme - to handle both cases:
Code:
:g/\(.* \)\([^.]*[.]*\)\(table_name\)/s//\1\2test_\3/


Last edited by vgersh99; 12-03-2013 at 03:53 PM..
This User Gave Thanks to vgersh99 For This Post:
# 19  
Old 12-03-2013
Try:
Code:
:g/\(.*[ .]\)\(table_name\)/s//\1test_\2/

which will look for a space or a period before "table_name" instead of:
Code:
:g/\(.* \)\(table_name\)/s//\1test_\2/

Which only accepts a space before "table_name".
This User Gave Thanks to Don Cragun For This Post:
# 20  
Old 12-03-2013
Quote:
Originally Posted by vgersh99
A variation of Don's theme - to handle both cases:
Code:
:g/\(.* \)\([^.]*[.]*\)\(table_name\)/s//\1\2test_\3/

That worked just fine!

Thanks Smilie

---------- Post updated at 04:11 PM ---------- Previous update was at 04:10 PM ----------

Quote:
Originally Posted by Don Cragun
Try:
Code:
:g/\(.*[ .]\)\(table_name\)/s//\1test_\2/

which will look for a space or a period before "table_name" instead of:
Code:
:g/\(.* \)\(table_name\)/s//\1test_\2/

Which only accepts a space before "table_name".
That also worked just fine!

Thanks Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to replace two character pattern with another pattern

Not able to paste my content. Please see the attachment :-( (2 Replies)
Discussion started by: vivek d r
2 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Replace NA with column mean

Hi experts, I'm looking for a unix solution to replacing "NA" within a matrix with the mean of the column: $cat file ID a b c d day 10 5 100 50 cat 20 6 200 50 dog NA 8 NA 50 car 15 NA NA ... (3 Replies)
Discussion started by: torchij
3 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Extract a pattern using sed or cut

Hi, Need help to extract a pattern using sed or cut or any other means. Input 'name1',1234567890 'name2',2222222222 'name3',3333333333 Expected output name1,1234567890 name2,2222222222 name3,3333333333 (3 Replies)
Discussion started by: mukulverma2408
3 Replies

4. Shell Programming and Scripting

Linux: replace pattern with pattern

Hi, I am a rookie of Linux. I have a problem on how can I replace a certain pattern in Linux with nothing. Can anyone help me?:( sample.txt: <binding>App189 ABC SampleMachine1 ABC XXX YYY ZZZ </binding> <binding>App190 ABC SampleMachine2 ABC XXX YYY ZZZ </binding> <binding>App191... (3 Replies)
Discussion started by: dirkaulo
3 Replies

5. Shell Programming and Scripting

[SOLVED] Replace a string in nextline after searching a pattern

Hi, I have a requirement where I need to replace a string in a line and this line will be identified by search criteria on previous line: E.g.: I have an xml file and Contents as below: <Root> <NameValue> <name>Global/Text/Data</name> <value>This is valid... (14 Replies)
Discussion started by: mailing2vamsi
14 Replies

6. Shell Programming and Scripting

pattern match and replace another pattern in same line

I have a pattern username:x:32005:32006::/usr/local/user:/bin/bash I need to match the line containing username and replace /bin/bash with /usr/local/my/bin/noshell So it becomes username:x:32005:32006::/usr/local/user:/usr/local/my/bin/noshell (7 Replies)
Discussion started by: anilcliff
7 Replies

7. Shell Programming and Scripting

replace pattern after the first pattern match

I need this. aaa OOOOO bbb ccc OOOOO ddd fff ggg OOOOO iii OOOOO I need all OOOOO replaced with PPPPP, but only change after the pattern ggg. So the first two OOOOO should not be changed. OUTPUT should be :- aaa (2 Replies)
Discussion started by: anilcliff
2 Replies

8. Shell Programming and Scripting

[solved] remove pattern with sed

Hi, i want to remove a certain pattern when i type pwd. pwd will look like this: ..../....../....../Pat_logs/..../....../...../...... the dotted lines are just random directory names, i want it to remove the "Pat_logs/...../....../....../" part so for example: ... (8 Replies)
Discussion started by: a27wang
8 Replies

9. Shell Programming and Scripting

perl:: search for tow pattern and replace the third pattern

Hi i want to search two pattern on same line and replace onther pattern.. INPut file aaaa bbbbb nnnnnn ttttt cccc bbbbb nnnnnn ppppp dddd ccccc nnnnnn ttttt ffff bbbbb oooooo ttttt now i want replace this matrix like.. i want search for "bbbbb","nnnnnn" and search and replace for... (4 Replies)
Discussion started by: nitindreamz
4 Replies

10. Shell Programming and Scripting

SED Search Pattern and Replace with the Pattern

Hello All, I have a string "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031", and I just want to extract LLSV1, but I dont get the expected result when using the sed command below. # echo "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031" | awk '{print... (4 Replies)
Discussion started by: racbern
4 Replies
Login or Register to Ask a Question