mirfan
1
[root@linux~]# cat email.txt | grep -i "To:"
To: <test@example.com>
[root@linux~]# cat email.txt | grep -i "Subject"
Subject: Test
Subject: How are you.
I need to print only test@example.com from To field need to eliminate "< & >" from To field and need to print entire subject after Subject:
It should be
[root@linux~]# cat email.txt | grep -i "To:"
test@example.com
[root@linux~]# cat email.txt | grep -i "Subject"
Test
How are you.
for email
cat email.txt | awk -F\< '/To:/{ print $2 }' | td -d '>'
for subject
cat email.txt | awk -F: '/Subject/{ print $2 }' | tr -d '>'
OR using sed:
sed -n 's/^To: <\(.*\)>/\1/p' email.txt
sed -n 's/^Subject: \(.*\)/\1/p' email.txt
awk '{gsub(/[<>]/,x)}sub(/^(To|Subject): /,x)' infile
clx
5
Also,
$ awk -F: '/To:/ || /Subject:/ {gsub(/^[ ]+|<|>/,"",$2); print $2}' email.txt
test@example.com
Test
How are you.
$
ctsgnb
6
nawk '(/^To/||/^Subject/){sub(".*"$2,$2)}1' infile
sed 's/^To: //;s/^Subject: //;s/[<>]//g' infile
mirfan
7
Thanks for the replies.
But sometimes To field contains email addresses in multiple lines. but following listed commands printing only 1st line. e.g.
To: test@example.com, test1@example.com,
test2@example.com, test3@example.com,
test4@example.com,
how to print entire To: field emails.
test@example.com, test1@example.com,
test2@example.com, test3@example.com,
test4@example.com,
Would be helpful posting your file email.txt's contents..
sed '/^Sub.*/,/^$/d;/To: /s///' email.txt