[SOLVED] nawk FS using pipe read variables from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [SOLVED] nawk FS using pipe read variables from file
# 1  
Old 10-04-2012
CPU & Memory [SOLVED] nawk FS using pipe read variables from file

I have a file data_1.out which contains:

Code:
1|abc mail|mail subject|mail body
2|def mail|mail subject|def mail body

I am trying to read the variables from data_1.out and use them to print to 2 different files based on the id (first_column)
The problem is I am not able to read the file using FS in the nawk. Could some help me out. If I substitute the "|" with space in data_1.out below thing works.
Code:
nof=`wc -l data_1.out | awk ' { print $1 }'`
no_of_lines=`expr $nof - 0`
z=1

while [[ $z -le $no_of_lines ]]
do

cat data_1.out | nawk -v I="$z" 'NR==I { print $0 }' | read id mail_id mail_subject mail_body

printf "From: abc@unix.com
To: ${mail_id}
Subject: IDI : ${mail_subject}
${mail_body} " >> m_file_${id}.out

z=$((z + 1))
done


Thanks for your help.

Last edited by Scott; 10-04-2012 at 03:46 PM.. Reason: Code tags, please...
# 2  
Old 10-04-2012
You don't need awk at all here, or wc, or expr, or backticks. You can do this all in one while read loop.

Code:
while IFS="|" read ID MAIL_ID MAIL_SUBJECT MAIL_BODY
do
        cat <<EOF >> m_file_${ID}.out
From: abc@unix.com
To: ${MAIL_ID}
Subject: IDI : ${MAIL_SUBJECT}
${MAIL_BODY}
EOF
done

# 3  
Old 10-04-2012
Or if you want to keep your script try:
Code:
 
sed 's/|/ /g' data_1.out | awk -v I="$z" 'NR==I { print $0 }' | read id mail_id mail_subject mail_body

# 4  
Old 10-04-2012
read at the end of the pipe does not work in most bourne shell, only KSH.

You can use -F"|" on awk instead of sedding | to space.
# 5  
Old 10-04-2012
Quote:
Originally Posted by Corona688
read at the end of the pipe does not work in most bourne shell, only KSH.

You can use -F"|" on awk instead of sedding | to space.
I tried using the -F"|", but it doesn't seem to work.

I have tried the while IFS but it is not exiting the loop.
# 6  
Old 10-04-2012
is read working on your machine..

lets try...(not tested this code..) Hope this works...Smilie

Code:
while read line
do
echo $line | awk  -F "|" '{ print $0 }' OFS=" " | read id mail_id mail_subject mail_body

printf "From: abc@unix.com
To: ${mail_id}
Subject: IDI : ${mail_subject}
${mail_body} " >> m_file_${id}.out

done<input_file

# 7  
Old 10-04-2012
Quote:
Originally Posted by sol_nov
I have tried the while IFS but it is not exiting the loop.
That final EOF needs to be at the beginning of the line, you can't indent it.

If that doesn't help, show your code please.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read variables from a file and then export it.

Hi I want to read variables from one file and then set it as environment variable; The text file is test.txt which contains SPEED:1000 IP:172.26.126.11 My code is: while read line; do var1=`echo $line | awk 'BEGIN {FS=":"} { print $1 }'` echo $var1 var2=`echo $line | awk 'BEGIN {FS=":"}... (8 Replies)
Discussion started by: SSM
8 Replies

2. Shell Programming and Scripting

[Solved] awk command to read sequentially from a file until last record

Hello, I have a file that looks like this: Generated geometry (...some special descriptor) 1 0.56784 1.45783 -0.87965 8 1.29873 -0.8767 1.098789 ... ... ... ... Generated geometry (....come special descriptor) ... .... ... ... ... ... ... ... and... (4 Replies)
Discussion started by: jaldo0805
4 Replies

3. Shell Programming and Scripting

[Solved] Not able to read the next line from a file after doing ssh

Hi, I am trying to write a code, where it reads the jobnames from a file and checks for the logs in the server. If the log is not found in the first server, then it will ssh to the 2nd server and get the details from there. I will need to save the date in a variable. But the problem is that,... (0 Replies)
Discussion started by: ajayakunuri
0 Replies

4. Shell Programming and Scripting

Bash Script Help...search then read from file: change text and pipe back...

Hello, I am trying to make a bash script that can pull data from a file and then change one part of said data. I want to search by username and pull the full line. That way there is a way to replace just one part of that line then return it back to the file. My Data is stored like: ... (1 Reply)
Discussion started by: serverfull
1 Replies

5. Shell Programming and Scripting

nawk variables

Hi - The following nawk is not working and trying to understand why! nawk -v t="internal_order" '/SAP_RM_ADMIN_SCHEMA/ && ("" toupper(t)) || /SAP_RM_ADMIN_SCHEMA/ && ("" tolower(t))' PBFD100.ksh My intention is to retrieve the line containing SAP_RM_ADMIN_SCHEMA.internal_order but its just not... (6 Replies)
Discussion started by: anduzzi
6 Replies

6. Shell Programming and Scripting

Can I use nawk -v with two or more variables?

Hi, Is it possible in awk/nawk to pass two or more variables in the -v flag? That is: X=1 Y=2 nawk -v X=$X Y=$Y..... Thanks in advance. (7 Replies)
Discussion started by: daytripper1021
7 Replies

7. Shell Programming and Scripting

Read variables and their values from file

Hi, I want to read the variables and the values from the txt file and compare these values with the ones computed by script. for ex: say var.txt contains the variable names and their values: one 1 two 2 three 3 The value of variables "one" "two" and "three" will be computed in the script... (3 Replies)
Discussion started by: bhushana
3 Replies

8. Shell Programming and Scripting

[Solved] Find Specific records from file and add totals into variables

Hi Eveyone, I am working on one shell script to find the specific records from data file and add the totals into variables and print them. you can find the sample data file below for more clarification. Sample Data File: PXSTYL00__20090803USA CHCART00__20090803IND... (7 Replies)
Discussion started by: veeru
7 Replies

9. Shell Programming and Scripting

how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me in the script i have a nawk block which has a variable count nawk{ . . . count=count+1 print count } now i want to access the value of the count variable outside the awk block,like.. s=`expr count / m` (m is... (5 Replies)
Discussion started by: saniya
5 Replies

10. Shell Programming and Scripting

nawk and variables

Hi guy's Im trying to pass variables into nawk and then match them on a value within a record but it don't seem to be working. If i put in the dates i want to see then it works fine.. #!/usr/bin/ksh -x YEST=$(/usr/local/bin/perl -e... (8 Replies)
Discussion started by: plimpix
8 Replies
Login or Register to Ask a Question

Featured Tech Videos