cherrun
cherrun

Reputation: 2142

Remove everything around a given pattern

I have a text file with several columns separated by tabs, and thousands of lines like this:

Foo  Bar  [email protected]  01.01.0001  SomeMoreStuff

Now I would like to extract just the e-mail, which is [email protected] in this case.

What would the easiest way be to delete everything before and after the e-mail using vim or any other CLI tool?

Upvotes: 0

Views: 94

Answers (5)

dimba
dimba

Reputation: 27641

The following command will print the third column:

cut -f3 file_name

Upvotes: 4

romainl
romainl

Reputation: 196886

In Vim I would escape the grim prospect of having to come up with a solid substitution by using a simple macro.

First, record the macro:

qq
0
d2f<tab> <-- this is a tab key
f<tab>   <-- this is a tab key
D
q

Second, apply the macro from the line below to the end of the buffer:

:+1,$norm @q

Upvotes: 1

Kent
Kent

Reputation: 195269

using awk;

awk '$0=$3' file

using pure vim

try this line:

:%s/.*\t\([^@]\+@[^\t]\+\).*/\1/

if you are sure the email sits in the 3rd column, you could also try:

:%s/\v([^\t]*\t){2}([^\t]+).*/\2/

or using vim + awk if you like :)

:%!awk '$0=$3' 

Upvotes: 1

Fredrik Pihl
Fredrik Pihl

Reputation: 45670

in bash:

while read -a ARRAY; do echo ${ARRAY[2]}; done < input

perl in awk-mode

perl -lane 'print $F[2]' input

Upvotes: 0

anubhava
anubhava

Reputation: 786291

For command line a simple awk will do the job:

awk '{print $3}' inFile

Upvotes: 1

Related Questions