Narrowing sed Results in While Loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Narrowing sed Results in While Loop
# 1  
Old 12-29-2013
Narrowing sed Results in While Loop

Code:
find $SRC -type f -name *.emlx |
while read FILE
do
if :
then sed -n '/From/p' $FILE
fi
done > $DEST-output.txt

The loop above spits out a .txt file with several lines that look like this:

From: John Smith <jsmith@company.com>

How can I narrow that sed result to spit out the email only? Maybe the "From:" line but only include the data in between <> symbols containing @. I'm running these results into a ldapsearch query which is why I need the email only.
# 2  
Old 12-29-2013
You may try following also for just email, if your file only contains email information
Code:
$ echo "From: John Smith <jsmith@company.com>" | sed 's/.*<\|>//g'
jsmith@company.com

Code:
$ echo "From: John Smith <jsmith@company.com>" | grep -Po '(?<=<).*(?=>)'
jsmith@company.com

Code:
$ echo "From: John Smith <jsmith@company.com>" | awk 'gsub(/.*<|>.*/,x)'
jsmith@company.com

--edit--

if your interest is searching line starting with stringFrom then following might be helpful
Code:
$ awk '/^From:/ && gsub(/.*<|>.*/,x)' file

Code:
$ sed -n '/^From/ s/.*<\|>//p' file


Last edited by Akshay Hegde; 12-29-2013 at 05:40 AM..
This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 12-29-2013
Something like this should work for both the angular bracket style (name surname <emailaddr> )and the direct type email addresses (emailaddr):
Code:
awk '/From:/{gsub(/[<>]/,x,$NF); print $NF}'

Quote:
Originally Posted by sudo
Code:
find $SRC -type f -name *.emlx |
while read FILE
do
if :
then sed -n '/From/p' $FILE
fi
done > $DEST-output.txt

It is best to quote the wildcard name specification to avoid unwanted expansion. Also, you could probably use the -exec clause instead of a while loop, then you could also use the + operator for more efficient operation, e.g.:
Code:
find "$SRC" -type f -name '*.emlx' -exec awk '/From:/{gsub(/[<>]/,x,$NF); print $NF}' {} + > "$DEST-output.txt"

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 12-30-2013
There's also mails having "From:" lines ending with <br/>, and HTML- headers replacing < with &lt; and > with &gt; ; some even put the username in parentheses AFTER the email - address - try this to capture all of those as well:
Code:
sed -n 's/&lt;/</;s/&gt;/>/;s/ *[(>].*$//;s/^From:.*[< ]//p' file

This User Gave Thanks to RudiC For This Post:
# 5  
Old 12-30-2013
Hello,

Following may help too.

1st:
Code:
echo "From: John Smith <jsmith@company.com>" |  awk -F"[<>]"  '{print $2}'
jsmith@company.com

2nd:
Code:
echo "From: John Smith <jsmith@company.com>" | sed 's/\(.*: \)\(.*<\)\(.*\)\(>.*\)/\3/g'
jsmith@company.com


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 12-30-2013
Well, how about these mail file lines:
Code:
From: root@xxxx.com (root)      
From: username &lt;username@xxxx.com&gt;<br/>

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Df -h results in a loop

Hello everyone, I am doing a check of the disk space using df -h, I want to combine the result in break line; but the result after while/done is empty: # df -h Filesystem Size Used Avail Use% Mounted on rootfs 20G 14G 4.6G 75% / /dev/root 20G 14G 4.6G 75% /... (15 Replies)
Discussion started by: Abu Rayane
15 Replies

2. Shell Programming and Scripting

Gawk Narrowing Down Search Results

I am using GAWK to search for a specific pattern: gawk '{IGNORECASE=1;} /<a href=/&&/\$/,/<\/a/' index.html <a class=author href="http://washingtondc.craigslist.org/search/?areaID=10&amp;amp;catAbb=sss&amp;amp;query=ps vita" title="craigslist washington, DC | all fo r sale / wanted search &quot;ps... (1 Reply)
Discussion started by: metallica1973
1 Replies

3. Shell Programming and Scripting

Loop through awk results

I have files structured in stanzas, whose title is '', and the rest couples of 'id: value'. I need to find text within the title and return the whole stanzas that match the title. The following works: awk 'BEGIN{RS="";IGNORECASE=1}/^\/' myfileI would need to count all of the occurences, though,... (7 Replies)
Discussion started by: hermes14
7 Replies

4. Shell Programming and Scripting

How to print sed results

Dear all, How can I print results (and of course, send this result to the text file) of sed command. I mean, I want to know which lines of which files sed command has found. For e.g, the result text file should contains: file1.c:line 12 file2.h:line 14 file2.h:line 37 Please help me (10 Replies)
Discussion started by: Hannibal2010
10 Replies

5. Shell Programming and Scripting

Concatenate Loop Results

Hi, I have the following situation: Param1Values = AAAA,BBBB Param1=$(echo $Param1Values| tr "," "\n") for x in $Param1 do db2 select X from Y where Z IN ('$x') done Obviously the above will perform the select 'x' amount of times. Is there a way in which i can... (13 Replies)
Discussion started by: RichZR
13 Replies

6. Shell Programming and Scripting

Not able to store the results of perl recursive function when applied under for loop

Hi Perl Gurus , need URGENT HELP PLEASE !!!!! I have one recursive Perl function which takes path of any directory as argument and returns array containing all the sub folders inside it recursively. Now the problem is that it works well if i use it with one time but the problem is that when... (0 Replies)
Discussion started by: anthriksh2000
0 Replies

7. Shell Programming and Scripting

doing a for loop adding up the results

Hi there If I run a 'swap -l' on my solaris box, i get swapfile dev swaplo blocks free /dev/dsk/c1t0d0s1 54,65 8 67119560 65655144 /dev/dsk/c1t0d0s2 54,65 8 33119522 32655122 I wanted to run a for loop adding up the totals of each column 4 , excluding the... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

8. Shell Programming and Scripting

2 CMD results on the same line while rexing in a loop

Folks, I have a 3 problems. In a sh script, I call a server name from a list and rex to a distant machine to get the boot date. for i in `cat list` do (echo "$i|"; /bin/rexsh $i -l bozo -t10 who -b | cut -d" " -f14-16) >>getBootTimes.out sleep 1 done The results are on 2 lines instead... (8 Replies)
Discussion started by: linux_lou
8 Replies

9. Shell Programming and Scripting

Perl - Iterating a hash through a foreach loop - unexpected results

i've reworked some code from an earlier post, and it isn't working as expected i've simplified it to try and find the problem. i spent hours trying to figure out what is wrong, eventually thinking there was a bug in perl or a problem with my computer. but, i've tried it on 3 machines with the... (5 Replies)
Discussion started by: quantumechanix
5 Replies
Login or Register to Ask a Question