LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to add a perticular string in a file via VI editor (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-add-a-perticular-string-in-a-file-via-vi-editor-4175464436/)

nameprabhat 06-02-2013 04:15 AM

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

druuna 06-02-2013 04:19 AM

Quote:

Originally Posted by nameprabhat (Post 4963931)
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?

druuna 06-02-2013 04:40 AM

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).


All times are GMT -5. The time now is 12:48 AM.