Vim: help with substitution


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Vim: help with substitution
# 1  
Old 07-13-2009
Vim: help with substitution

What is the appropriate command should i use to insert a character(example A) in front of line 1 to line 200...Pls help

Last edited by Yogesh Sawant; 07-19-2009 at 06:53 AM.. Reason: changed the title with a more meaningful one
# 2  
Old 07-13-2009
793589,

Try this:
Code:
:1,200s/^/A/

Regards,
Davf

Last edited by Yogesh Sawant; 07-19-2009 at 06:48 AM.. Reason: added code tags
# 3  
Old 07-18-2009
Thanks..
Is workable.

How about if i need to replace a character in the middle of sentence from line 10 to 50?

exmaple, i need to replace the number "5" from all lines below with "X"?
What is the useful vim command that i can apply for..

ddpadsgag506xghssuyj
ddpadsgag546xghssuys
ddsadsgag596xghssuy_te
ddsadsgag506xghssuy_pe
ddsadsgag526xghssuys
ddsadsgag506ighssuys
ddsadsgag506pghssuyk
# 4  
Old 07-19-2009
to replace number 5 with X in lines 10 to 50:
Code:
:10,50s/5/X/

# 5  
Old 07-21-2009
Bug

Yes, thanks many & very appreciate that..
By the way, what if i've a various numeric number at all the line in my file and i just intend to replace the 1st digit only with character "X". Example,
my file
ddpadsgag506xghssuyj_3#ods
ddpadsga446xghssuys_47#523
ddsadsgagh396xghssuy_te_3.4
ddsadsgag506xghssuy_pe_3.35
ddsadsgag326xghssuys
ddsadsgag506ighssuys_eng
ddsadsgag506pghssuyk

result
ddpadsgagX06xghssuyj_3#ods
ddpadsgaX46xghssuys_47#523
ddsadsgaghX96xghssuy_te_3.4
ddsadsgagX06xghssuy_pe_3.35
ddsadsgagX26xghssuys
ddsadsgagX06ighssuys_eng
ddsadsgagX06pghssuyk
And what command should i apply to eliminate the content after _3 at end of the line. Example,
my file
ddpadsgagX06xghssuyj_3#ods
ddsadsgaghX96xghssuy_te_3.4
ddsadsgagX06xghssuy_pe_3.35

result
ddpadsgagX06xghssuyj_3
ddsadsgaghX96xghssuy_te_3
ddsadsgagX06xghssuy_pe_3
# 6  
Old 07-22-2009
Quote:
Originally Posted by 793589
Yes, thanks many & very appreciate that..
By the way, what if i've a various numeric number at all the line in my file and i just intend to replace the 1st digit only with character "X". Example,
my file
ddpadsgag506xghssuyj_3#ods
ddpadsga446xghssuys_47#523
ddsadsgagh396xghssuy_te_3.4
ddsadsgag506xghssuy_pe_3.35
ddsadsgag326xghssuys
ddsadsgag506ighssuys_eng
ddsadsgag506pghssuyk

result
ddpadsgagX06xghssuyj_3#ods
ddpadsgaX46xghssuys_47#523
ddsadsgaghX96xghssuy_te_3.4
ddsadsgagX06xghssuy_pe_3.35
ddsadsgagX26xghssuys
ddsadsgagX06ighssuys_eng
ddsadsgagX06pghssuyk
try this (i haven't tried myself):
Code:
perl -pi -e 's/(\d)(\d\d)/X\2/'

Quote:
Originally Posted by 793589
And what command should i apply to eliminate the content after _3 at end of the line. Example,
my file
ddpadsgagX06xghssuyj_3#ods
ddsadsgaghX96xghssuy_te_3.4
ddsadsgagX06xghssuy_pe_3.35

result
ddpadsgagX06xghssuyj_3
ddsadsgaghX96xghssuy_te_3
ddsadsgagX06xghssuy_pe_3
try this (i haven't tried myself):
Code:
perl -pi -e 's/_3(.*)$/_3/'

# 7  
Old 07-22-2009
Using sed command:
Code:
 sed 's/[0-9]/X/; s/_3.*$/_3/' yourfile

It works with your sample I/O

Last edited by thanhdat; 07-22-2009 at 06:45 AM.. Reason: eliminate the content after _3
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Vim :f

Can someone please tell me what this does? :f word I thought that was the way to save files with a different name but after a quick google search I saw it was like this. :w filename (4 Replies)
Discussion started by: cokedude
4 Replies

2. Solaris

Very Importan - Vim Settings - Error while opening a File using vim

I downloaded vim.7.2 and compiled the vim source . Added the vim binary path to PATH (Because iam not the root of the box) when i load the file using vim it throws me an error Error detected while processing /home2/e3003091/.vimrc: line 2: E185: Cannot find color scheme darkblue line... (0 Replies)
Discussion started by: girija
0 Replies

3. Red Hat

Trying to understand how to use vim

Hi there. I'm new at UNIX, but want to learn and understand it. I'm reading the Fedora and RHEL book, but I'm not sure how to use vim to create an alias in my .bash_profile. Can you point me in the right direction please? ;) (2 Replies)
Discussion started by: hrh0901
2 Replies

4. Shell Programming and Scripting

Difference between "Command substitution" and "Process substitution"

Hi, What is the actual difference between these two? Why the following code works for process substitution and fails for command substitution? while IFS= read -r line; do echo $line; done < <(cat file)executes successfully and display the contents of the file But, while IFS='\n' read -r... (3 Replies)
Discussion started by: royalibrahim
3 Replies

5. UNIX for Advanced & Expert Users

vim

Does anyone know the fast way to go to tab/2 distance in vim. I mean set four spaces. (1 Reply)
Discussion started by: mirusnet
1 Replies

6. UNIX and Linux Applications

Help with Vim

How do I use compilers and syntax highlighting in (g)Vim? Specifically, I need a Common Lisp compiler and a C++ compiler. Thanks in advance! (Note: Disregard my other topic.) (1 Reply)
Discussion started by: led3234
1 Replies

7. UNIX for Advanced & Expert Users

About Vim

Hello, what is the maximum size of a file that we can open using vim. (3 Replies)
Discussion started by: nagalenoj
3 Replies

8. UNIX for Dummies Questions & Answers

Need some help with vim

I use vim for writing code. I was using nano but found that vim is a much advanced editor. I am trying to achieve something like this: When trying a loop or a function, I generally type: function name() {} Now when I take my cursor one position to the left and press the return key, it give... (4 Replies)
Discussion started by: Legend986
4 Replies

9. Solaris

Vim 7.0

I want to install VIM 7.0 from sunfreeware.com onto a server. The security guys are giving me a hard time because they found this security vulnerability. http://xforce.iss.net/xforce/xfdb/6259 This vulnerability came out in 2001, and it looks like it was mostly for linux, but I cant be... (1 Reply)
Discussion started by: BG_JrAdmin
1 Replies

10. UNIX for Dummies Questions & Answers

vi/vim : complex pattern substitution

I use vim. I have a lot of SQL queries to write, and am hoping there is some wild command I can use in vim to make this simpler. From a file that is a list of fields, like the excerpt, for example: orderdetail.ccntyfips orderdetail.citemord orderdetail.coffdetid I want to go to this: ... (2 Replies)
Discussion started by: c444l
2 Replies
Login or Register to Ask a Question