LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-02-2013, 05:15 AM   #1
nameprabhat
LQ Newbie
 
Registered: Jan 2013
Posts: 4

Rep: Reputation: Disabled
How to add a perticular string in a file via VI editor


i have a file with 500 lines i need to add a particular string in starting/end/anywhere of each and every line of this file.

example data

123
1234123
124324
1243254523
124235423

I need to add "a" in front of every line. outcome data.

a123
a1234123
a124324
a1243254523
a124235423

this needs to do only by Vi editor.

we can do it by awk but how to perform the same in Vi editing.
how can i do this. Please help.

Regards, Prabhat

Last edited by nameprabhat; 06-02-2013 at 05:23 AM.
 
Old 06-02-2013, 05:19 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
Originally Posted by nameprabhat View Post
i have a file with 500 lines i need to add a particular string in starting/end/anywhere of each and every line of this file.

how can i do this.
Can you explain a bit more what it is you exactly want to do? Which string? At what location(s)?

You might want to include examples (before/after).

BTW: Wouldn't sed or awk be a better choice to do this task?
 
1 members found this post helpful.
Old 06-02-2013, 05:40 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
In command mode you can do the following:
Code:
:%s/^/a/
Don't know what your vi experience is so here's a little breakdown of that command:

The % tells vi to do the changes on all lines (you can also give a range instead 4,10 -> lines 4 to 10)
The s/^/a/ tells vi to insert an a, at the beginning of the line (the ^).

As you might have noticed this looks like the way sed does things (both use the ed command under the hood).

Also have a look here:
BTW: Don't edit your original post after an answer was given, post a new entry instead. If you don't there is a big chance your changes will not be noticed (we do get an e-mail when someone post a new entry, not when someone edits an already existing one).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] search a directory and add file contents when find string j-me Linux - General 3 01-11-2013 09:54 AM
[SOLVED] how to search a perticular file in fedora ??? abi Linux - Newbie 6 02-02-2012 02:23 AM
PHP: Find a string in a text file and add something to that line aocferreira Programming 1 05-26-2011 02:52 PM
Finding Perticular Character in a file dina3e Programming 6 07-11-2008 11:07 PM
quick way to add string to a text file ufmale Linux - Newbie 1 03-11-2008 05:03 PM


All times are GMT -5. The time now is 11:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration