LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 04: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 04:23 AM.
 
Old 06-02-2013, 04:19 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 04:40 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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



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 08:54 AM
[SOLVED] how to search a perticular file in fedora ??? abi Linux - Newbie 6 02-02-2012 01:23 AM
PHP: Find a string in a text file and add something to that line aocferreira Programming 1 05-26-2011 01:52 PM
Finding Perticular Character in a file dina3e Programming 6 07-11-2008 10:07 PM
quick way to add string to a text file ufmale Linux - Newbie 1 03-11-2008 04:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:13 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
Open Source Consulting | Domain Registration