MeetP
1
This is an vi editor question. I do not know is this a right place to ask this question or not?
I have a file with the following contents,
10 11
20 21
30 31
I want to copy first column that is 10,20,30 after second column, so that output will look like the following,
10 11 10
20 21 20
30 31 30
How can I do that in VI editor through command mode (i.e. ":")?
MeetP
3
%s/\(.\) ./& \1/
Substitute
---------- Post updated at 12:56 PM ---------- Previous update was at 12:54 PM ----------
%s/\(.\) ./& \1/
s :substitute
.* means number of characters
\ means do not take other meaning
what does & mean?
Can you explain the command please?
%s/\(.*\) .*/& \1/
Explanation:
%s -> substitute in the whole file
The \( and \) pair are stored in a buffer. The first \( and \) pair will represent by \1, the second \( and \) is \2 and so on
\(.*\) -> The first pair is everything before the last space
.* -> (space followed by a dot and an asterix) the last space and everything thereafter
& -> represent the entire line here
& \1/ -> means substitute the line with entire line followed by a space and the first pair