Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove quotes surrounding the first two columns in Vim?

Tags:

replace

vim

Say I have the following style of lines in a text file:

"12" "34" "some text     "
"56" "78" "some more text"
.
.
.
etc.

I want to be able to remove the quotes surrounding the first two columns. What is the best way to do this with Vim (I'm currently using gVim)?

I figured out how to at least delete the beginning quote of each line by using visual mode and then enter the command '<,'>s!^"!!

I'm wondering if there is a way to select an entire column of text (one character going straight down the file... or more than 1, but in this case I would only want one). If it is possible, then would you be able to apply the x command (delete the character) to the entire column.

There could be better ways to do it. I'm looking for any suggestions.


Update

Just and FYI, I combined a couple of the suggestions. My _vimrc file now has the following line in it:

let @q=':%s/"\([0-9]*\)"/\1/g^M'   

(Note: THE ^M is CTRLQ + Enter to emulate pressing the Enter key after running the command)

Now I can use a macro via @q to remove all of the quotes from both number columns in the file.

like image 281
Jason Down Avatar asked Jun 22 '09 17:06

Jason Down


People also ask

How do you remove quotation marks in Linux?

By using the -r option, you can output the result with no quotes.


2 Answers

use visual block commands:

  • start mode with Ctrl-v
  • specify a motion, e.g. G (to the end of the file), or use up / down keys
  • for the selected block specify an action, e.g. 'd' for delete

For more see :h visual-mode

like image 132
Fritz G. Mehner Avatar answered Nov 16 '22 03:11

Fritz G. Mehner


Control-V is used for block select. That would let you select things in the same character column.

It seems like you want to remove the quotes around the numbers. For that use,

:%s/"\([0-9]*\)"/\1/g

Here is a list of what patterns you can do with vim.


There is one more (sort of ugly) form that will restrict to 4 replacements per line.

:%s/^\( *\)"\([ 0-9]*\)"\([ 0-9]*\)"\([ 0-9]*\)"/\1\2\3\4/g

And, if you have sed handy, you can try these from the shell too.

head -4 filename.txt | sed 's/pattern/replacement/g'

that will try your command on the first 4 lines of the file.

like image 35
nik Avatar answered Nov 16 '22 02:11

nik