Deleting first column with vim

17,701

Solution 1

This should delete all chars before and including the 1st tab on any line:

:%s/^[^\t]*\t//

Command line cut:

cut -f 2- {filename.txt} > {filenamenew.txt}

cut defaults to tabs; if you want something else like a space add -d " ". -f is fields to copy over. 2- means all from (and including) column 2.

Solution 2

Through awk,

awk -F"\t" '{print FS,$2}' file > newfile

It cuts the first column and prints only the remaining tab and second column.

Through sed,

sed -r 's/^([^\t]*)\t(.*)$/\t\2/g' file > newfile

Solution 3

:%s/[^\t]*\t//

On every line (%), replace (s/ORIGINAL/REPLACEMENT/) the first occurrence of “non-tab characters ([^\t], in any number (*)) followed by a tab \t” by nothing. You can type Tab instead of \t.

Alternatively you can match the shortest sequence of characters (.\{-}) ending in a tab. .*\t would match the longest match for .*, so it would match all but the last column; .\{-} matches the shortest match which is the first column.

:%s/.\{-}\t//

Solution 4

In Vi to remove first column (separated by space), you can do:

:%norm dW

for a column separated by Tab, it's:

:%norm dfCtrl+VTab

So the command which would remove the first column from file (in-place) can be:

ex +"%norm df$(echo -e '\t')" -scwq file

To check the output before saving (dry-run), replace -scwq with -sc'%p|q!'.

Or based on Chris suggestion, like:

ex -c':exe ":%norm df\<Tab>"' -sc'%p|q!' <(echo -e "a a\tb b\tc c")

Alternatively do it in visual mode (if starting from the top-left):

  1. Enter visual-block by Ctrl+v.
  2. Jump at the end and select first column by pressing: G, E (or adjust manually).
  3. Press d to delete the selected block.
Share:
17,701

Related videos on Youtube

bigTree
Author by

bigTree

Updated on September 18, 2022

Comments

  • bigTree
    bigTree over 1 year

    I have a text file.

    number 1_1 \t number1_2 \t etc
    number 2_1 \t number2_2 \t etc
    

    I want to remove the first column of this file (corresponding to number1_1, number2_1 etc, ie the numbers before the first tab for each row). I read this post that proposes a solution to delete the first column (see Peter's answer). However, it doesn't work for me as the numbers have different sizes and I cannot repeat the operation to delete the first column. How can I do then?

    • bigTree
      bigTree almost 10 years
      preferably, but not necessarily (what else do you propose?)
    • Avinash Raj
      Avinash Raj almost 10 years
      @bigTree do you want command line answers?
    • bigTree
      bigTree almost 10 years
      @AvinashRaj Command line answers would be good thanks
  • h3.
    h3. almost 4 years
    @lobi No. It's the % at the beginning that means “for all lines”. A g at the end would repeat the substitution, so it would repeatedly remove the first column until only the last column remains.