Remove a column using vi editor


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Remove a column using vi editor
# 1  
Old 03-19-2014
Remove a column using vi editor

How do i remove a column using vi editor

Assuming the file to be of format

Code:
1:  010 0xad45  sp1 -  11:29:51.498583949 500249144  Event1 rst bcfe jhv rgc
456:  010 0xadb  sp2 -  11:29:51.498600605     4464  Event0abcrd adabc aasd
45:  010 0x10 sp0 -  11:29:51.498614165    13560   Back adxca msg to end
4969:  010 0xjill  spx -  11:29:51.498632789    18624  global service desk to the rescue

I want to be able to remove column 7 from the file using vi
AND
i would like to move column 7 to the beginning of the line using vi

Any suggestions.

I have searched in the forum, examples assume fixed size of the columns or using cut or awk.
Thank You.

Last edited by sp0; 03-19-2014 at 06:12 PM.. Reason: Change ICODE tags to CODE tags.
# 2  
Old 03-19-2014
Is this a homework assignment?

Please show us the output you're trying to create. The vi utility's definition of the 7th column is the position on the line where the 7th character on the line would be placed if no tab characters or backspace characters were present in the line. I suspect that you're talking about the 7th blank (space or tab) delimited field, but without seeing what trying to do, it isn't worth guessing. Assuming you are talking about fields (rather than character positions) you also need to clearly specify character are sequences of characters act as field delimiters.

Note also that the leading spaces you have shown on the 3rd, 5th, and 7th will have likely throw off your desired results unless you very clearly specify how processing for the 1st line should be different than processing on the other lines.
# 3  
Old 03-19-2014
No this is not a homework assignment.

I know how to do it using cut.
cut -d" " -f1-6,8- filename

Desired output format 1 (remove 7th column)
Code:
1:  010 0xad45  sp1 -  11:29:51.498583949   Event1 rst bcfe jhv rgc 
456:  010 0xadb sp2 -  11:29:51.498600605       Event0abcrd adabc aasd 
45:  010 0x10 sp0 -  11:29:51.498614165       Back adxca msg to end 
4969:  010 0xjill  spx -  11:29:51.498632789      global service desk to the rescue

Desired output format 2 (move 7th column to first column)
Code:
500249144 1:  010 0xad45  sp1 -  11:29:51.498583949   Event1 rst bcfe jhv rgc 
4464 456:  010 0xadb  sp2 -  11:29:51.498600605       Event0abcrd adabc aasd 
13560 45:  010 0x10 sp0 -  11:29:51.498614165      Back adxca msg to end 
18624 4969:  010 0xjill  spx -  11:29:51.498632789     global service desk to the rescue

not sure if i can invoke cut command in vi on the current file?

Last edited by sp0; 03-19-2014 at 06:23 PM.. Reason: does not show up nicely formatted
# 4  
Old 03-19-2014
First note that every field delimiter character matters to the cut utility, so the output from the command:
Code:
cut -d" " -f1-6,8- filename

(with your sample input) is:
Code:
1:  010 0xad45  sp1  11:29:51.498583949 500249144  Event1 rst bcfe jhv rgc
456:  010 0xadb  sp2  11:29:51.498600605     4464  Event0abcrd adabc aasd
45:  010 0x10 sp0 - 11:29:51.498614165    13560   Back adxca msg to end
4969:  010 0xjill  spx  11:29:51.498632789    18624  global service desk to the rescue

which is not even close to what you said you wanted when the 7th field is deleted.

I can produce output similar to what you said you want (although the number of spaces are different on some lines around the deleted field) with the vi commands:
Code:
:g/\([^[:space:]]*[[:space:]]*[^[:space:]]*[[:space:]]*[^[:space:]]*[[:space:]]*[^[:space:]]*[[:space:]]*[^[:space:]]*[[:space:]]*[^[:space:]]*[[:space:]]*\)[^[:space:]]*/s//\1/

to remove the 7th field, and
Code:
:g/\([^[:space:]]*[[:space:]]*[^[:space:]]*[[:space:]]*[^[:space:]]*[[:space:]]*[^[:space:]]*[[:space:]]*[^[:space:]]*[[:space:]]*[^[:space:]]*[[:space:]]*\)\([^[:space:]]*\)/s//\2 \1/

to move the 7th field to the start of the line.
# 5  
Old 03-19-2014
Quote:
Originally Posted by sp0
not sure if i can invoke cut command in vi on the current file?
You can filter ex/vi text through external commands using !. See your documentation for the details.

Regards,
Alister
# 6  
Old 03-20-2014
Thanks for the answer Don, but your solution assumes fixed number of spaces. So it does not work.

But the file i have does not have a constant number of blank spaces between the columns..

I definitely learned that the output is stored int \1, \2 and \3. this is good info.
# 7  
Old 03-20-2014
Quote:
Originally Posted by sp0
Thanks for the answer Don, but your solution assumes fixed number of spaces. So it does not work.

But the file i have does not have a constant number of blank spaces between the columns..

I definitely learned that the output is stored int \1, \2 and \3. this is good info.
Now I am completely confused. Please show me an input line where the suggestion I made worked incorrectly due to the number of spaces between fields!

The BRE [^[:space:]]*[[:space:]]* matches any number of characters that are not in the space character class followed by any number of characters that are in the space character class (for the purposes of this discussion, the space character class contains the characters <space>, <form-feed>, <carriage-return>, <tab>, and <vertical-tab>). My solution assumes a fixed number of fields to be processed; not a fixed number of spaces between fields.

The only constant number of spaces in what I provided is that when I moved field 7 to the start of the line, I added a single <space> after the moved field (which matched what you did in your sample). (I originally wrote it to move the spaces following field 7 in the input to be the separator between moved field and the original 1st field on the line. I went to a single space when I saw that it didn't match what you said you wanted.)

You may also note that your data samples did not always have the same number of spaces between fields in the input and output.

-------------------

The only thing that required a fixed number of spaces was the cut example you provided that you said could be used to do what you wanted. I was just pointing out that cut will not work for this problem because you don't have a fixed number of spaces.

Last edited by Don Cragun; 03-20-2014 at 07:38 PM.. Reason: Add note about cut.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove the values from a certain column without deleting the Column name in a .CSV file

(14 Replies)
Discussion started by: dhruuv369
14 Replies

2. UNIX for Dummies Questions & Answers

Swapping column in vi editor

is there any command in vi editor to turn this 986.000 4.026.000 775.328.625 9.319.003.000 986.000 4.036.000 775.328.625 9.318.803.000 986.000 4.046.000 775.328.625 9.318.603.000 986.000 4.056.000 775.328.625 9.318.403.000 become this... (5 Replies)
Discussion started by: muhnandap
5 Replies

3. UNIX for Dummies Questions & Answers

Removing 2nd Column in Vi Editor

I have text like this M83-306 132 797 M83-312 145 685 M83-315 321 479 M83-319 654 193 M83-350 556 1184 M83-303 222 199 and I want to make it like this M83-306 797 M83-312 685 M83-315 ... (9 Replies)
Discussion started by: muhnandap
9 Replies

4. Shell Programming and Scripting

remove brackets and put it in a column and remove repeated entry

Hi all, I want to remove the remove bracket sign ( ) and put in the separate column I also want to remove the repeated entry like in first row in below input (PA156) is repeated ESR1 (PA156) leflunomide (PA450192) (PA156) leflunomide (PA450192) CHST3 (PA26503) docetaxel... (2 Replies)
Discussion started by: manigrover
2 Replies

5. Shell Programming and Scripting

need to remove duplicates based on key in first column and pattern in last column

Given a file such as this I need to remove the duplicates. 00060011 PAUL BOWSTEIN ad_waq3_921_20100826_010517.txt 00060011 PAUL BOWSTEIN ad_waq3_921_20100827_010528.txt 0624-01 RUT CORPORATION ad_sade3_10_20100827_010528.txt 0624-01 RUT CORPORATION ... (13 Replies)
Discussion started by: script_op2a
13 Replies

6. Ubuntu

How to remove multiple spaces in between word? (VI EDITOR)?

What last line mode command allows me to remove extra spaces in between words in a text? (1 Reply)
Discussion started by: rabeel
1 Replies

7. UNIX for Dummies Questions & Answers

Delete a specific column using vi editor?

Hello Experts, I'm a newbie so please excuse any wrong doings. I have a file that looks like this. abc def ghi jkl mno def abc ghi mno jkl ghi def mno jkl abc I would like the file to look like this abc def ghi jklmno def abc ghi mnojkl ghi def mno jklabc in other... (3 Replies)
Discussion started by: fnebiolo
3 Replies

8. Shell Programming and Scripting

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, 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... (3 Replies)
Discussion started by: MeetP
3 Replies

9. Shell Programming and Scripting

VI editor,column postion

In VI editor ctrl + g is used indicate the line number on which the cursor is placed...similarly is there a way to determine the column number of the cursor position..? (1 Reply)
Discussion started by: vijay_0209
1 Replies
Login or Register to Ask a Question