Using awk to read a file and process


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Using awk to read a file and process
# 1  
Old 11-06-2013
Using awk to read a file and process

I am fairly green using awk so I don't have anything started but what I am trying to do is: I have a file called "contacts" and in the file are 3 fields separate by ;. I want to read each line and send an email script. I have the email function working but just need to know how to setup awk to read each line and process. This is the sample of the contact file, it will grow so it will eventually have more than 3 lines Smilie
Code:
8535;abccompany@yahoo.com;Dan
4200;123bakerbest@gmail.com;Tom
4300;daddy@godaddy.com;Jerry

Here is what I know how to do but not sure how to do a do while loop or for next loop with awk
Code:
counter=`wc -l "contacts" |awk '{print $1'}`
echo $counter
awk -F';' '{ print $1 $2 $3 }' contacts
echo "test"

Results give me the count of how many lines using wc with awk

Code:
3
8535abccompany@yahoo.comDan
4200123bakerbest@gmail.comTom
4300daddy@godaddy.comJerry
test

# 2  
Old 11-06-2013
So what exactly do you want to do with which of the fields of each line?
# 3  
Old 11-06-2013
Quote:
Originally Posted by RudiC
So what exactly do you want to do with which of the fields of each line?
My goal is to read one line of the contacts and then send an email using $2 as the send to email address and a file attached to the email that would be unique for that email address. I know how to arrange the email script I just don't know how to write the awk script to read each line then send the email then read the next line and send an email until the end of the file. thanks
# 4  
Old 11-06-2013
Don't use awk for that; use simple shell:
Code:
while IFS=";" read var1 addr name; do mail -a file -s "Subject" $addr; ...; done < contacts

This User Gave Thanks to RudiC For This Post:
# 5  
Old 11-06-2013
Quote:
Originally Posted by RudiC
Don't use awk for that; use simple shell:
Code:
while IFS=";" read var1 addr name; do mail -a file -s "Subject" $addr; ...; done < contacts

I tried to adapt that code you suggested but it continues to fail. my email script requires the \ slash to go at the end of each line and I think that is creating problems. The other variables you see below, $mr and $yr are passed from previous prompts in the script.
Code:
while IFS=";" read an email to; do
          exec /u/star/sendEmail \ ;
             -f username@baby.net \ ;
             -t $email   \ ;
             -s smtp.windstream.net:587 \ ;
             -xu username     \  ;
             -xp password   \     ;
             -u "Monthly Statement" \ ;
             -m "$to, Please see attached account Statement" \ ;
             -a /u/star/data/20$yr/$mr/$an$mr$yr.pdf ;
echo $email $to $an $mr $yr >> /u/tmp/my.log; done < contacts

The above script results in nothing being sent and the email program giving an error about missing parameters. I have used the above email script without the ; separating each line and it works without the while statement added. Not sure how to continue each line but I believe that is the problem.
# 6  
Old 11-06-2013
DON'T use exec - it will replace your current shell and you're ending up in nowhereland.

Are you sure you need that backslash (normally the "escape" char")? Are you sure you need that semicolon (shell's command end char)? Usual *nix syntax is to put all -options in a row, for a very long line you can use an "escaped <newline>" as a continuation char.
Try (without exec!) :
Code:
/u/star/sendmail -f ... -t $email -s ... -xu ...\
-xp ... -u ...

What be your system (OS, shell)?
# 7  
Old 11-06-2013
Quote:
Originally Posted by RudiC
DON'T use exec - it will replace your current shell and you're ending up in nowhereland.

Are you sure you need that backslash (normally the "escape" char")? Are you sure you need that semicolon (shell's command end char)? Usual *nix syntax is to put all -options in a row, for a very long line you can use an "escaped <newline>" as a continuation char.
Try (without exec!) :
Code:
/u/star/sendmail -f ... -t $email -s ... -xu ...\
-xp ... -u ...

What be your system (OS, shell)?
I am using SCO 5.0.7 and using a bourne shell
Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

2. Shell Programming and Scripting

Process to read a new file entry and execute a command

I need to develop a process/daemon which will constantly monitor a file for new entry and execute a command. for eg, there is a file /var/log/inotify.log When a new entry like below gets appeneded to this file, execute the command as follows. /home/user/public_html/bad.php|CREATE ... (2 Replies)
Discussion started by: anil510
2 Replies

3. Shell Programming and Scripting

Perl: How to read text from file and process $variable in that data too.

In the hello.htm have the sentenses: Hello $name How are you? The perl script: $name = "David"; open(HEADER,"hello.htm"); while(<HEADER>) { $html .= $_; } close(HEADER); print "$html";I making something about template. But it can't process the $name variable. (4 Replies)
Discussion started by: natong
4 Replies

4. Shell Programming and Scripting

How to read the input from a file in background process?

Hi I have a script to run some other scripts automatically. But while running the script it should take the input value from a text file instead of taking from the keyboard. Please find last two lines of the script below. Here ans should be taken from a text file ineerly without displaying this... (1 Reply)
Discussion started by: shirdi
1 Replies

5. Solaris

file open/read/write/close/access by process

Hi want to know what file (descriptor+filename+socket) is being accessed by particular process on solaris. Purpose : while running perf. test, needs to find where is the bottleneck. We are providing concurrnet load for around 1 hr and needs to capture data related to file usage pattern... (1 Reply)
Discussion started by: raxitsheth
1 Replies

6. Shell Programming and Scripting

How to read/process a .gz file, one line at a time?

Hello I'm stuck trying to solve this KSH issue and I'm hoping someone out there can offer some suggestions. I want to read lots of large .gz files one line at a time in order to compare its Error entries with a list of known errors. I can't simply do "foreach ERROR do gzcat *.gz |grep... (2 Replies)
Discussion started by: dayscripter
2 Replies

7. Shell Programming and Scripting

Read from file > process > output to file

Hi all, OK, I am totally new to shell scripting as I just started today. All I want from shell scripting is something very basic, and I've been searching for tutorials on "processing files" and I could not find a suitable one for my level. I want to read two columns from a data file, one raw... (3 Replies)
Discussion started by: Lorna
3 Replies

8. Shell Programming and Scripting

How to re-read a file in AWK

Hi , i am having some problem with re-reading the same file in AWK. here is the scenario. function 1 { some_string > " file1 " # i have redirected output to file1. ........... ........ } Now in function 2 { ... (1 Reply)
Discussion started by: madhaviece
1 Replies

9. Shell Programming and Scripting

File Read using awk

Hi, I need to read two colums(4th and 5th) from a file and do some manipulation Input file is 401500 IOC Q 14 14 406200 LC Q 1 1 410124 IOC Q 5 4 410124 LC Q 11 8 410132 IOC Q 230 229 410148 IOC Q ... (3 Replies)
Discussion started by: rejirajraghav
3 Replies

10. Shell Programming and Scripting

Help with awk - read from file

Hi, I've got a file like the following: Starting to process segment 0 (and symmetry related segments) Number of (cancelled) singularities: 0 Number of (cancelled) negative numerators: 0 Segment 0: 5.49secs Starting to process segment 1 (and symmetry related segments) Number of... (7 Replies)
Discussion started by: giorgos193
7 Replies
Login or Register to Ask a Question