Vi editor, copy one column?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Vi editor, copy one column?
# 1  
Old 12-15-2009
Data Vi editor, copy one column?

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,

Code:
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,
Code:
10 11 10
20 21 20
30 31 30

How can I do that in VI editor through command mode (i.e. ":")?

Last edited by Franklin52; 12-15-2009 at 01:57 PM.. Reason: Please use code tags!
# 2  
Old 12-15-2009
Try this:
Code:
%s/\(.*\) .*/& \1/

# 3  
Old 12-15-2009
Can you explain the command.

%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?
# 4  
Old 12-15-2009
Code:
%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
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to copy a column of multiple files and paste into new excel file (next to column)?

I have data of an excel files as given below, file1 org1_1 1 1 2.5 100 org1_2 1 2 5.5 98 org1_3 1 3 7.2 88 file2 org2_1 1 1 2.5 100 org2_2 1 2 5.5 56 org2_3 1 3 7.2 70 I have multiple excel files as above shown. I have to copy column 1, column 4 and paste into a new excel file as... (26 Replies)
Discussion started by: dineshkumarsrk
26 Replies

2. UNIX for Dummies Questions & Answers

Copy/paste in vi editor

Hello guys, I am trying to copy a line in vi editor and paste it with below commands but paste command is not working and instead of paste action prints the p character!! I should also mention that the server is Solaris... 1) crontab -e 2) j to move down 3) yy to copy the line 4) o to... (4 Replies)
Discussion started by: Newman
4 Replies

3. Shell Programming and Scripting

Copy from vi Editor ( unix ) to windows

How to copy the complete content from a file in vi Editor to windows ( notepad ). I can use " select " and paste it to windows but this is restricted to current page. Not allowing me to scroll down or up when selecting the content.:confused: (1 Reply)
Discussion started by: frintocf
1 Replies

4. UNIX for Dummies Questions & Answers

Copy huge data into vi editor

Hi All, HP-UX dev4 B.11.11 U 9000/800 3251073457 I need to copy huge data from windows text file to vi editor. when I tried copy huge data, the format of data is not preserverd and appered to scatterd through the vi, something like give below. Please let me know, how can I correct this? ... (18 Replies)
Discussion started by: alok.behria
18 Replies

5. UNIX for Dummies Questions & Answers

Copy/Paste in Vi editor

Dear All, I have a file containing 12 lines. First 3 lines have 9 values and the remaining 9 lines with no values. I was trying to copy and paste these 9 values of the first 3 lines into last 9 lines simultaneously as A=1.491331, B=1.539000 ..... but I don't know how to cope with this... (9 Replies)
Discussion started by: sullah
9 Replies
Login or Register to Ask a Question