I have a tab-delimited file like:
name1 verb1 name2 verb2 etc...
I want to delete the second column. I tried editing one of these answers, but being not familiar with sed
and awk
, I couldn't.
Can anyone help me?
I have a tab-delimited file like:
name1 verb1 name2 verb2 etc...
I want to delete the second column. I tried editing one of these answers, but being not familiar with sed
and awk
, I couldn't.
Can anyone help me?
The accepted answer is much more elegant than this (I upvoted it!) but if you do not remember it you can use vim
visual block mode directly. Open vim and go (normal mode) to the first corner of the column, like this:
Type CTRL-V
and you can move the cursor to select the column, this is midway:
To go at the end, press G
:
the block seems broken because we are on the last line which is blank; simply go up one line (with the up arrow or k
) to see it again...:
Now you simply press x
to delete the block:
In vim, you should be able to use the command
:%s/\t[^\t]*//
(substitute TAB followed by zero or more occurrences of any character except TAB with nothing). If your file has only two columns you could use a slightly simpler :%s/\t.*
or :%s/\t.*$
which replace the first TAB and any following characters up to the end of the line.
I would use cut
for this
cut -f1,3- file.txt > newfile.txt
mv newfile.txt file.txt
You can use this as a filter within vim, too (this will replace all the lines in the file; you could also use (for example) 2,9
instead of %
to process lines 2-9, or select the lines you want with V
):
:%!cut -f1,3-
-f1,3-
means 'print field one, followed by field three and all fields until the end of the row'. By default, cut
uses a tab as its delimiter; if you need something else, use the -d
option (see man cut
).
You can try either:
:%norm WdW
or manually using visual mode:
To do it in-place from the command line, try:
$ ex +':exe ":%norm f\<Tab>dE"' -scx file
Related: How to write literal for Tab key to use for motion?
See also:
To delete a column starting 83 characters from the left, up to the 94th character, you can try this (move your cursor around until you get to the sides of the columns you want to delete, and use Control-G to see what your cursor's position is):
:%s/\(^.\{83}\).\{11}/\1/
In short: Match the first 83 characters in each line, remember what was matched, now match the next 11 characters, and replace that entire bunch of 94 characters with those first matched 83 characters.
From http://www.peterscheie.com/unix/vi_del_columns.html .
More verbosely, here's the entire process: Hit the Escape key, followed by the colon, to get to the colon prompt. Now we say, "On every line %
, we'll do a search-and-replace s
and here's the regular expression we're going to look for /
. Remember this next part \(
: Starting at the beginning of the line ^
, match any character .
, do that 83 times \{83}
, that's the end of the remembering \)
. Now match any character .
, do that 11 times \{11}
. Now I've told you what to look for, next we'll do what to replace it with /
. Take the first thing I asked you to remember and use that to replace the stuff we matched \1
. Finally, that's the end of our search-and-replace /
.
But beware, in his article he talks about "colrm" as if that's part of vim, but it's not. It's a shell escape, and colrm is not on my Windows machine :-) .
x
to delete)?