Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deleting a column in vi 3

Status
Not open for further replies.

praywin

Programmer
Feb 6, 2003
31
IN
Hi,
I want to delete a column from a file while inside a vi session. I know the awk solution I want to know if it is possible using vi.
Cheers,
Pravin.
 
umm ... i guess you could possibly use a global substitute ... but if you can it'd probably be better and easier to use awk or sed.

can you give an example of the before and after?
 
If you know how to do it in awk, why would you even need to do it in vi?

 
Well its more like I am in the middle of a session in vi . I don't want to save the file, go to the shell, run awk and come back to vi for more editing.

Lets say I am editing a file like this

1 Pravin - He loves ice creams
2 Rahul - He too loves ice creams

Now I open it in vi and change it to

1 Pravin - He loves girls
2 Rahul - He just loves ice creams

Now I realise I want to remove the line numbers so that it becomes

Pravin - He loves girls
Rahul - He just loves ice creams

It would be nice if I could do it without exiting vi .. for I may change my mind again ;-)

Cheers,
Pravin.
 
For the example you gave, this will work:
Code:
:s/[1-9] /

If you can give a more specific example, I can give a more specific solution :)
 
Well .. it works for this example but I want a more generic solution where I can remove a column of data from a file I am editing in vi. Lets say no search pattern works. All you have is space " " separated data

adfkj asdflkj wqeroiu adfk
adsk qwep dai qwer oupu
adsfu qwer cou wer cou

Now I want to remvoe the second column, Can I do it?

Cheers,
Pravin.
 
well you'd want to do it globally ...
:g/^[0-9][0-9]* /s///
 
:%s/ [^\ ]* / /

Well again this works only for the example I gave. It won't work for the third or fourth column.

However this is very valuable starging point

:%s/ [^\ ]* / /

The input can be represented as

[^\ ]*[ [^\ ]* ]* (bad regexp I guess)

So we somehow have to pick the n'th [ [^\ ]* ] pattern and remove it.

Thanks for your help
Cheers,
Pravin.


 
Delete 4th column....

:w
then
:%!cut -d' ' -f1,2,3,5- %
 
Thanks. I guess this should solve the basic problem of not having to exit vi.
Cheers,
Pravin.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top