Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Help with code

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-09-2013
Sara_84 Sara_84 is offline
Registered User
 
Join Date: Feb 2012
Last Activity: 21 November 2013, 7:20 AM EST
Posts: 53
Thanks: 38
Thanked 0 Times in 0 Posts
Help with code

I have problem with this piece of code. I tried to use awk to send mail but it failed. Please help me


Code:
for i in $( cat /tmp/a)

do

cat /tmp/b | grep $i | awk ' {mail -s  " $2 is your new ID"   $1@companydomain.com }

done


Code:
cat a
Joe
Mic
Noah

Cat b 
Alan 2313
Joe 223
Mic 768
Charlie 565
Noah 2656

Sponsored Links
    #2  
Old Unix and Linux 01-09-2013
Jotne's Unix or Linux Image
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 20 September 2014, 2:08 AM EDT
Posts: 1,040
Thanks: 62
Thanked 212 Times in 202 Posts

Code:
cat /tmp/b | grep $i | awk ' {mail -s  " $2 is your new ID"   $1@companydomain.com }'

No need to cat and grep

Code:
awk '$0~inp {mail -s  " $2 is your new ID  $1@companydomain.com" }' inp=$i /tmp/b

Not sure if this work, but you do miss a final ' in your code
Sponsored Links
    #3  
Old Unix and Linux 01-09-2013
Sara_84 Sara_84 is offline
Registered User
 
Join Date: Feb 2012
Last Activity: 21 November 2013, 7:20 AM EST
Posts: 53
Thanks: 38
Thanked 0 Times in 0 Posts
It is still not working

It gives me error ( invalid char @ in expression ) !!
    #4  
Old Unix and Linux 01-09-2013
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 3 December 2016, 7:19 PM EST
Location: San Jose, CA, USA
Posts: 9,722
Thanks: 459
Thanked 3,320 Times in 2,840 Posts
Here are a couple of ways to do what you want:

Code:
#!/bin/ksh
echo simplififed translation from given code
while read i
do      grep $i /tmp/b | awk '{printf("echo mail -s \"%s is your new ID\" %s@companydomain.com", $2, $1)}' | ksh
done < /tmp/a

echo awk replacement
awk 'FNR == NR {
        newID[$1] = $2
        next
}
{       printf("echo mail -s \"%s is your new ID\" %s@companydomain.com\n",
                newID[$1], $1);
}' /tmp/b /tmp/a | ksh

I use ksh and I added echo commands in several places so it will show what it will do rather than actually invoke mail to send messages. You'll need to change /bin/ksh in the first line of the script to the absolute pathname of a shell on your system that processes traditional Bourne shell command syntax (e.g., bash, ksh, or sh) to run the script and then remove echo from the awk printf() command after you verify that the script does what you want.

Choose whichever code segment you prefer to use. They both should do what you want. The first segment is a simplified and corrected version of what it looked like you were trying to do. The second one produces the same results, but should be more efficient.
The Following 2 Users Say Thank You to Don Cragun For This Useful Post:
jim mcnamara (01-09-2013), Sara_84 (01-10-2013)
Sponsored Links
    #5  
Old Unix and Linux 01-10-2013
Sara_84 Sara_84 is offline
Registered User
 
Join Date: Feb 2012
Last Activity: 21 November 2013, 7:20 AM EST
Posts: 53
Thanks: 38
Thanked 0 Times in 0 Posts
Thanks Cragun . It's working now
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Go to a line of code , skip few lines of code Paarth Linux 5 12-26-2012 11:31 AM
Block of code replacement in Java source code through Unix script hareeshram Shell Programming and Scripting 2 01-30-2012 11:54 AM
If 922 Code does not exist on 03 Record, 901 Code will be there instead, move 03 R sgoud UNIX for Dummies Questions & Answers 1 06-28-2011 02:55 PM
how i prepare a c++ code(c code) for implementing my own protocol format amitpansuria Programming 1 09-07-2007 12:09 AM



All times are GMT -4. The time now is 09:51 PM.