8

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?

4
  • Do you want a vim specific solution?
    – jobin
    May 26, 2014 at 9:48
  • preferably, but not necessarily (what else do you propose?)
    – bigTree
    May 26, 2014 at 9:48
  • @bigTree do you want command line answers? May 26, 2014 at 10:00
  • 1
    @AvinashRaj Command line answers would be good thanks
    – bigTree
    May 26, 2014 at 10:04

5 Answers 5

7

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.

4

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
1
4

:%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//

1
  • 1
    @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. Jun 25, 2020 at 14:08
3

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.
0
0

To remove the remaining space before the second column I suggest:

:%norm dfCtrl+VTabx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.