1826780 Members
1668 Online
109702 Solutions
New Discussion

cut, grep etc....

 
SOLVED
Go to solution
Jonathan Caplette_1
Super Advisor

cut, grep etc....

Hi guys,

I'm really fu*ked up with grep an cut commands. And I'm trying to do a script in which I need to use them...

Here is the structure of the content of the file that I'm trying to read:


*********** START ***********

theemails@areonthatline.com
The subject is on that line

Multiple line of data
Multiple line of data
Multiple line of data
Multiple line of data
Multiple line of data

************ END ************


What I need to do, is to send mail a the emails that are listed on the "real first" line and that subject on the other line. And then the rest of the file I'm gonna put it in the body of the email. What I wanna you guys tell me is how can I extract the emails line, the subject line and the rest of my text file. I just need to put the extraction in variables with them after I'm gonna use my script to send mail with a body... Please just told me how can I use grep or cut to get the info I need!!!

Regards!
Jonathan
6 REPLIES 6
Vince Inman
Frequent Advisor

Re: cut, grep etc....

grep and cut may be a bit to crude to use for the purpose that you have stated. Sed and awk might be more appropriate since the level of granularity you can get with sed and awk is much finer than with grep and/or cut.

however, if grep and cut hav you throughly confused, sed and awk will make your head spin as they are cryptic, but powerful.
Shannon Petry
Honored Contributor
Solution

Re: cut, grep etc....

First, help may be more available without the implied bad word ;)

For your problem, it looks like you have a fixed format at least at the top of your data, which is good.

4th line is subject
3rd line is the email addresses

Lets call your file INFILE. Subject is the easiest to extract, so here it is...
#!/bin/sh
SUBJECT=`head -4 $INFILE|tail -1`

Getting the email addresses will require a loop. We can use head and tail to pull the 3rd line from the file to make a loop. A blank space will be treated as a separator.

for ADDRESS in `head -3 $INFILE|tail -1` ; do

mailx $ADDRESS -s $SUBJECT $INFILE
done

Assuming that you want the whole INFILE mailed, the above will work just fine. If you dont want the subject and addresses from the file mailed, you will have to be a bit more creative with the script.

If this is the case, I'd recommend making a temp file with the data you do want to mail, then wc to count lines, expr to remove calculate lines -4(remove header), then tail to build your temp file.

Hope it helps, and no vast knowledge of grep or cut required for this job.

Regards,
Shannon

Microsoft. When do you want a virus today?
Paula J Frazer-Campbell
Honored Contributor

Re: cut, grep etc....


Well said Shannon

Jonathan please take notice.


Paula
If you can spell SysAdmin then you is one - anon
Jonathan Caplette_1
Super Advisor

Re: cut, grep etc....

Thanks Shannon!!! Why I didn't think about using head and tail... lol!!!

Sorry for the "F" word.... I'll watch it in the future!!!
harry d brown jr
Honored Contributor

Re: cut, grep etc....


Where /tmp/ooo is your INPUT file:


#!/usr/bin/ksh
cd /tmp
rm abcd*
csplit -s -f abcd -n 4 /tmp/ooo '/^\*\*\*\*\*\*\*\*\*\*\*\* END \*\*\*\*\*\*\*\*
\*\*\*\*$/+1' {*}
for i in `ls abcd*`
do
crazy=""
auth=""
subj=""
ema1=""
ema2=""
crazy=`cat $i | head -5 | tail -2`
auth=`echo $crazy|cut -d" " -f1`
subj=`echo $crazy|cut -d" " -f2-`
ema1=`echo $auth|cut -s -d"@" -f1`
ema2=`echo $auth|cut -s -d"@" -f2`
if [ "$ema1" -a "$ema2" ]; then
echo "Thank you for replying" | mailx -s "re: \"${subj}\"" $auth
echo "--------------------------"
fi
done


live free or die
harry
Live Free or Die
Thomas M. Williams_1
Frequent Advisor

Re: cut, grep etc....

Jonathan:

You have some good replies already but I still wanted to put in my two cents. If I am understanding this correctly all you need is 'sed'. The 'sed' command will print any number of lines in a file.
If you have a number of files to parse through then using a loop would be good. So try this:

#!/bin/ksh

for infile in
do
inaddr="`sed -n '3p' ${infile}`"
insubj="`sed -n '4p' ${infile}
sed -n '6,$p' ${infile} | mailx -x${insubj} ${inaddr}
done

exit 0

I was not sure on the exact line numbers so just change the 3, 4, and 6 to whatever you need them to be.

Hope this helps

Thomas
I Think the Clock is Slow ...