perl or awk print strings between words

hi everyone,

1.txt

981 I field1 > field2.a: aa, ..si01To:<f:a@a.com>From: <f:a@a.com>;tag=DVNgfRZBZRMi96 <f:a@1:333>;ZZZZZ: 12345

the output

field1 field2 <f:a@a.com>

the output is cut the string 3rd and 5th field, and get the value betwee "To:" and "From:", please advice.

Thanks

i know i can

awk 'BEGIN{FS="To:";RS="From:"}/</{print $2}' 1

to get <f:a@a.com>, but how to at the same time get 3rd and 5th field.

This is only way i can figure out, but very not 'smart'

cat 1 | cut -f3,5 -d' ';awk 'BEGIN{FS="To:";RS="From:"}/</{print $2}' 1

but when 1.txt has many lines, then problem :frowning:

if multiple lines, then i can use cat first, then awk, then paste two files. haha, very not 'smart' solution

$
$
$ cat f2
981 I field1 > field2.a: aa, ..si01To:<f:a@a.com>From: <f:a@a.com>;tag=DVNgfRZBZRMi96 <f:a@1:333>;ZZZZZ: 12345
$
$
$ perl -lne '/^\w+\s+\w+\s+(\w+)\s+\>\s+(\w+).*?To:(.*?)From:.*$/ and print "$1 $2 $3"' f2
field1 field2 <f:a@a.com>
$
$ perl -lane '$F[4]=~s/(\w+).*/$1/; $F[6]=~s/.*To:(.*?)From:/$1/; print "$F[2] $F[4] $F[6]"' f2
field1 field2 <f:a@a.com>
$
$

tyler_durden

1 Like