Reading specific lines from a file using index or keywords


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Reading specific lines from a file using index or keywords
# 1  
Old 04-16-2012
Data Reading specific lines from a file using index or keywords

Hello

I want to read from a file which contains email addresses.
The file format is like this.

Code:
[FROM]
from@mail.com
[TO]
to1@mail.com
to2@mail.com
[CC]
cc@mail.com
[BCC]
bcc@mail.com

I'll have to read from such file and assign the email addresses to respective variables.

Code:
frommail = from@mail.com
tomail = to1@mail.com,to2@mail.com
ccmail = cc@mail.com
bccmail = bcc@mail.com

like this.

How can i do it??? cos email addresses are one email per line so it can be dynamic. Any suggestion??

Thanks
Kyaw Lwin Phyo

Last edited by Scrutinizer; 04-16-2012 at 04:47 AM.. Reason: extra code tags
# 2  
Old 04-16-2012
What Operatinng System and version are you using? What programming language or Shell do you use?
The suggested output format looks like "csh" syntax which may be why nobody is answering this post.
This User Gave Thanks to methyl For This Post:
# 3  
Old 04-16-2012
hi methyl,
the shown file is the input, not output.
I'm using k shell, HP UX.

thanks for reminder... Smilie
# 4  
Old 04-16-2012
Try:
Code:
read frommail tomail ccmail bccmail << EOF
$(awk '{gsub(/^\n|\n$/,x,$2); gsub(ORS,",",$2); A[$1]=$2} END{print A["FROM"] OFS A["TO"] OFS A["CC"] OFS A["BCC"]}' RS=\[ FS=\] infile)
EOF

echo "$tomail"


Last edited by Scrutinizer; 04-16-2012 at 09:37 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 04-16-2012
Try...
Code:
$ cat file1
[FROM]
from@mail.com
[TO]
to1@mail.com
to2@mail.com
[CC]
cc@mail.com
[BCC]
bcc@mail.com

$ awk 'sub(/]/,"=",$1){sub(/ /,"");gsub(/ /,",");print}' RS=[ file1
FROM=from@mail.com
TO=to1@mail.com,to2@mail.com
CC=cc@mail.com
BCC=bcc@mail.com

$ eval $(awk 'sub(/]/,"=",$1){sub(/ /,"");gsub(/ /,",");print}' RS=[ file1)

$ echo $FROM
from@mail.com

$ echo $TO
to1@mail.com,to2@mail.com

$ echo $CC
cc@mail.com

$ echo $BCC
bcc@mail.com

$

This User Gave Thanks to Ygor For This Post:
# 6  
Old 04-17-2012
Hi Ygor,

Thanks for the solution.
however, when i ran this, in the to email.
it is only showing me
Code:
,to2@mail.com

not
Code:
to1@mail.com,to2@mail.com

and can you please give me some explaination for this script???

I really don't know head or tail of unix yet. Smilie

Thanks.

---------- Post updated at 11:19 PM ---------- Previous update was at 08:48 PM ----------

Quote:
Originally Posted by Scrutinizer
Try:
Code:
read frommail tomail ccmail bccmail << EOF
$(awk '{gsub(/^\n|\n$/,x,$2); gsub(ORS,",",$2); A[$1]=$2} END{print A["FROM"] OFS A["TO"] OFS A["CC"] OFS A["BCC"]}' RS=\[ FS=\] infile)
EOF

echo "$tomail"

Hi Scrutinizer,
it is not showing me anything when i echo those 4 variables...
Please suggest....

Thanks.
# 7  
Old 04-17-2012
Make sure there are no spaces after EOF and that it is at the beginning of the line..
This User Gave Thanks to Scrutinizer For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Copy Lines between Keywords & paste them to another file

hi, I have Multiple files with the following data : File1 100414 DR1 END XXXXX Test1 Test2 Test3 Test4 Test5 Test6 END 100514 DR2 END XXXXX Test7 Test8 Test9 Test10 Test11 Test12 END 100614 DR3 (5 Replies)
Discussion started by: newageBATMAN
5 Replies

2. UNIX for Dummies Questions & Answers

Quick UNIX command to display specific lines in the middle of a file from/to specific word

This could be a really dummy question. I have a log text file. What unix command to extract line from specific string to another specific string. Is it something similar to?: more +/"string" file_name Thanks (4 Replies)
Discussion started by: aku
4 Replies

3. Shell Programming and Scripting

Extract lines from file using keywords using script

Hi I need some lines of text from input file using keywords. Inputfile IP IS 10.238.52.65 pun-ras-bng-mhs-01#context bsnl.in Card Status : 1:0, 2:1, 3:1, 4:1, 5:0, 6:0, 7:0, 8:0, 9:1, 10:0, 11:0, 12:0, 13:0, 14:1, Max Circuits: 1: 0, 2: 32768, ... (5 Replies)
Discussion started by: surender reddy
5 Replies

4. Shell Programming and Scripting

copy range of lines in a file based on keywords from another file

Hi Guys, I have the following problem. I have original file (org.txt) that looks like this module v_1(.....) //arbitrary number of text lines endmodule module v_2(....) //arbitrary number of text lines endmodule module v_3(...) //arbitrary number of text lines endmodule module... (6 Replies)
Discussion started by: kaaliakahn
6 Replies

5. UNIX for Dummies Questions & Answers

Reading a specific line from a file

Hi All, I am having 100 lines a text file say a.txt. I want read the 'nth' line from that file inside a script. Kindly tell us how to that. (2 Replies)
Discussion started by: boopathyvasagam
2 Replies

6. UNIX and Linux Applications

Reading a file for specific words

Hi I have a script where the user calls it with arguments like so: ./import.sh -s DNSNAME -d DBNAME I want to check that the database entered is valid by going through a passwd.ds file and checking if the database exists there. If it doesn't, the I need to send a message to my log... (4 Replies)
Discussion started by: ladyAnne
4 Replies

7. Shell Programming and Scripting

awk: reading into an array and then print the value corresponding to index

I am beginner in awk awk 'BEGIN{for(i=1;(getline<"opnoise")>0;i++) arr=$1}{print arr}' In the above script, opnoise is a file, I am reading it into an array and then printing the value corresponding to index 20. Well this is not my real objective, but I have posted this example to describe... (19 Replies)
Discussion started by: akshaykr2
19 Replies

8. Filesystems, Disks and Memory

why the inode index of file system starts from 1 unlike array index(0)

why do inode indices starts from 1 unlike array indexes which starts from 0 its a question from "the design of unix operating system" of maurice j bach id be glad if i get to know the answer quickly :) (0 Replies)
Discussion started by: sairamdevotee
0 Replies

9. UNIX for Dummies Questions & Answers

Reading specific part of file

I have a requirement to go to particular line in the file and from there read the contents till it meets a particular criteria. For eg if the contents of the file is like 81 abcd ------------------- Line 1 82 cdfe ------------------- Line 2 83 dfj ------------------- Line 3 84 df... (5 Replies)
Discussion started by: guptan
5 Replies

10. Shell Programming and Scripting

reading specific line from file

Hi all... I not a expert unix script programmer, Kindly adjust. My requirement is that, i have a file which contains the about 10 lines - say 1 2 3 ... 8 war of the worlds: => text in this line 9 9000,80,78,77,334,445 => this line contains some numbers separted by commas 10 ... (10 Replies)
Discussion started by: cool_boss2121
10 Replies
Login or Register to Ask a Question