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

前端 未结 8 1116
遥遥无期
遥遥无期 2020-12-15 21:53

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

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

I want

相关标签:
8条回答
  • 2020-12-15 22:20

    You could also create a macro (q) that deletes the quotes and then drops down to the next line. Then you can run it a bunch of times by telling vi how many times to execute it. So if you store the macro to say the letter m, then you can run 100@m and it will delete the quotes for 100 lines. For some more information on macros:

    http://vim.wikia.com/wiki/Macros

    0 讨论(0)
  • 2020-12-15 22:26

    The other solutions are good. You can also try...

    :1,$s/^"\(\w\+\)"/\1/gc

    For more Vim regex help also see http://vim.wikia.com/wiki/Search_patterns.

    0 讨论(0)
  • 2020-12-15 22:29
    1. Start visual-block by Ctrl+v.
    2. Jump at the end and select first two columns by pressing: G, EE.
    3. Type: :s/\%V"//g which would result in the following command:

      :'<,'>s/\%V"//g
      

      Press Enter and this will remove all " occurrences in the selected block.

    See: Applying substitutes to a visual block at Vim Wikia

    0 讨论(0)
  • 2020-12-15 22:32

    Say if you want to delete all columns but the first one, the simple and easy way is to input this in Vim:

    :%!awk '{print $1}'
    

    Or you want all columns but the first one, you can also do this:

    :%!awk '{$1="";$0=$0;$1=$1;print}'
    

    Indeed it requires external tool to accomplish the quest, but awk is installed in Linux and Mac by default, and I think folks with no UNIX-like system experience rarely use Vim in Windows, otherwise you probably known how to get a Windows version of awk.

    0 讨论(0)
  • 2020-12-15 22:33

    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.

    0 讨论(0)
  • 2020-12-15 22:38

    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

    0 讨论(0)
提交回复
热议问题