Replace, insert n times a specific character

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace, insert n times a specific character
# 1  
Old 09-04-2009
Question Replace, insert n times a specific character

How can using Vim, replace one character with another repeating it 10 times?
Transforming this: 125A986
That: 125##########986
# 2  
Old 09-04-2009
Deleted, did not read correctly the subject :X
# 3  
Old 09-04-2009
Couple ways I can think of doing something like this. Search/replace all instances of 'A' with 'XXXXXXXXXX':


or.. search for the character you want with '/A'. Once found (or before) map a key to perform the switch on the next character in your search:


I tend to use 'v' as my mapped key since it isn't used for anything else.
# 4  
Old 09-08-2009
Peterro, I really use the command:%s/A/XXXXXXXXXX/g", however, I need a command that can be used in situations that have to repeat the character 10, 100, 1000, 10000 times. Understand?
# 5  
Old 09-08-2009
Well, the best I've come up with is to:

:map v iX^[

where X is the character you want as the replacement. Now search for the character you want to replace and when you find it, press 'x' to remove that character and then type:


This will insert 100 of the replacement characters. Hit 'n' to go to the next character to be replaced.

It's not really an ideal solution but it sort of gives you what you're looking for. I was unable to get a search '/' or character replacement 's' to work as a map to be performed multiple times. Maybe someone else has a better solution.
# 6  
Old 09-09-2009
Thanks Peterro, solved my problem. I will continue looking for a script that solves this problem more generic.
# 7  
Old 09-09-2009
Glad it worked, are you really looking for a script solution or one that is in vi/vim like your original post suggested?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to insert new line after a specific character in scripts?

Hi, I'm trying to add a new line after finding a specific String. That is my string: volumes: - ${DIR_WORK}/loadbalancer/html:/var/www/html and I want to change that file to: volumes: - ${DIR_WORK}/loadbalancer/html:/var/www/html extra_hosts: -... (4 Replies)
Discussion started by: siamak
4 Replies

2. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

3. Post Here to Contact Site Administrators and Moderators

Search for a pattern and replace a space at specific position with a Character in File

In file, we have millions of records each of 1000 in length. And at specific position say 800 there is a space, we need to replace it with Character X if the ID in that row starts with 123. So far i have used the below which is replacing space at that position to X but its not checking for... (3 Replies)
Discussion started by: Jagmeet Singh
3 Replies

4. Shell Programming and Scripting

Insert character at specific location in a each line of the file

Hi All, I am trying to write a shell script where it should insert character 'I' in 180th position of each line(except first and last line) of the file. Below is the script for file in /home/test/bharat/*.RET do # Process file echo "File Name=" $file #l_fileName="${file##*/}" ... (19 Replies)
Discussion started by: bharath561989
19 Replies

5. UNIX for Advanced & Expert Users

Replace certain character at specific place with related character

hello i have file with 100k records and each one has certain value that starts at 28th column and certain value that starts at 88th column e.g. 1st file <25>1234567 ..... <88> 8573785485 i have aditional file with values which are related to value that starts at 88th column of the... (1 Reply)
Discussion started by: dell1520
1 Replies

6. Shell Programming and Scripting

How to replace a character in a specific column in a file?

This is a file that I have test line 1 (55) ) test line 2 (45) ) I would like to change all the parens in position 1 of this file to a ); i only want to check position 1 in every line of the file. I have tried different varations of sed, but cannot seem to be able to limit it to... (1 Reply)
Discussion started by: JoeG
1 Replies

7. Shell Programming and Scripting

Help with replace character based on specific location

Hi, I got long list of reference file (column one is refer to the header in input file; column 2 is info of start position in input file; column 3 is info of end position in input file;) shown as below: read_2 10 15 read_3 5 8 read_1 4 10 . . . Input file (huge file with total... (6 Replies)
Discussion started by: perl_beginner
6 Replies

8. Shell Programming and Scripting

how to replace specific character , if possible using sed

My script is extracting data from SQl session, however sometimes the result contains one or multiple space after/before any numerical value. e,g . "123","1 34","1 3 45", "43 5" How to remove these unwanted that I can get the following result : "123","134",1345","435" (1 Reply)
Discussion started by: mady135
1 Replies

9. Shell Programming and Scripting

Using sed to replace specific character and specific position

I am trying to use sed to replace specific characters at a specific position in the file with a different value... can this be done? Example: File: A0199999123 A0199999124 A0199999125 Need to replace 99999 in positions 3-7 with 88888. Any help is appreciated. (5 Replies)
Discussion started by: programmer22
5 Replies

10. Shell Programming and Scripting

Insert character in a specific position of a file

Hi, I need to add Pipe (|) at 5th and 18th position of all records a file. How can I do this? I tried to add it at 5th position using the below code. It didnt work. Please help!!! awk '{substr($0,5,1) ~ /|/}{print}' $input_file > $temp_file (1 Reply)
Discussion started by: gpaulose
1 Replies
Login or Register to Ask a Question