Parsing Strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing Strings
# 1  
Old 11-19-2010
Parsing Strings

Hello All,

I am new to shell scripting and programming. I am looking for a guide on how I can parse specific information from a plain text file with thousands of lines. Specifically I need to parse an email address from each line. The line looks something like this:
Code:
1272574001.H742765P10724.host.domain.com,S=4155:Return-path: <hbelanger@kpmg.com>

I would like to extract the email address (exluding the < and >) and then perform an update statement via mysql with the email address as a value.

Is there an example of this somewhere that I could use to base my needs off of?

What scripting tool would I use, how do I tell that tool to gab all contents BETWEEN the < and >?

How do I then (within one script) use this value to update a DB record?

p.s. I am essentially trying to unsubscribe these email addresses from a contact database..

Thanks for the help.
John

---------- Post updated at 01:19 PM ---------- Previous update was at 01:15 PM ----------

The SQL statement that needs to be run is:
Code:
UPDATE contact_master SET subscribed='No' WHERE email=<value>


Last edited by Scott; 11-19-2010 at 04:14 PM.. Reason: Please use code tags
# 2  
Old 11-19-2010
Perhaps this would work to extract all the email addresses...
Code:
awk -F">" '/@/{print $1}' RS="<" file

(use nawk or /usr/xpg4/bin/awk if your OS is Solaris)

You can then pipe this output into a while read loop and run the SQL statements inside the loop..
# 3  
Old 11-19-2010
That worked like a charm. I have looked up your syntax and now understand how that works. I will now research how to use a while loop. Would you recommend a bash script?
# 4  
Old 11-19-2010
It could be done in awk, but I would use bash or another shell for this, I think it looks cleaner.
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 11-19-2010
I edited my response. It was a typo. It was meant to say 'I will now research'

After reading on bash I can accomplish my mysql execution using a defined variable (variable = email address) but am not sure how to read a file and have the mysql execute the statement for each line (i.e. email) value.

Code:
#!/bin/sh

email="test@test.com"
table="contact_master"
column="subscribed"

qry="select id,data from $table where id in ($ids)"
qry="UPDATE $table SET $column='No' WHERE email=$email"

echo "Executing the following query"
echo $qry

/usr/bin/mysql -u root << eof
$qry
eof

I just dont know how to incorporate the awk statement that parses my emails and loop it into the above bash script which updates DB.
# 6  
Old 11-19-2010
You could try something like this (using the statements that you wrote):
Code:
table="contact_master"
column="subscribed"
# ids=???

awk -F">" '/@/{print $1}' RS="<" file |
while read email
do
  /usr/bin/mysql -u root << EOF
  select id,data from $table where id in ($ids)
  UPDATE $table SET $column='No' WHERE email=$email
EOF
done

# 7  
Old 11-19-2010
I dont understand why I need the ids variable and the purpose of:
Code:
select id,data from $table where id in ($ids)

I'm not sure I need to select any data, just UPDATE

Would you mind clarifying my confusion.

p.s. You've been so helpful, thanks.

-John

Last edited by Scott; 11-19-2010 at 04:15 PM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

--Parsing out strings for repeating delimiters for everyline

Hello: I have some text output, on SunOS 5.11 platform using KSH: I am trying to parse out each string within the () for each line. I tried, as example: perl -lanF"" -e 'print "$F $F $F $F $F $F"' But for some reason, the output gets all garbled after the the first fields.... (8 Replies)
Discussion started by: gilgamesh
8 Replies

2. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

3. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies

4. UNIX for Dummies Questions & Answers

Issue when using egrep to extract strings (too many strings)

Dear all, I have a data like below (n of rows=400,000) and I want to extract the rows with certain strings. I use code below. It works if there is not too many strings for example n of strings <5000. while I have 90,000 strings to extract. If I use the egrep code below, I will get error: ... (3 Replies)
Discussion started by: forevertl
3 Replies

5. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

6. UNIX for Dummies Questions & Answers

Delete strings in file1 based on the list of strings in file2

Hello guys, should be a very easy questn for you: I need to delete strings in file1 based on the list of strings in file2. like file2: word1_word2_ word3_word5_ word3_word4_ word6_word7_ file1: word1_word2_otherwords..,word3_word5_others... (7 Replies)
Discussion started by: roussine
7 Replies

7. Shell Programming and Scripting

Parsing of file for Report Generation (String parsing and splitting)

Hey guys, I have this file generated by me... i want to create some HTML output from it. The problem is that i am really confused about how do I go about reading the file. The file is in the following format: TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
Discussion started by: umar.shaikh
8 Replies

8. Shell Programming and Scripting

Parsing file to match strings

I have a file with the following format 12g data/datasets/cct 8g data/dataset/cct 10 g data/two 5g data/something_different 10g something_different 5g data/two is there a way to loop through this... (1 Reply)
Discussion started by: yawalias
1 Replies

9. Shell Programming and Scripting

Perl parsing compared to Ksh parsing

#! /usr/local/bin/perl -w $ip = "$ARGV"; $rw = "$ARGV"; $snmpg = "/usr/local/bin/snmpbulkget -v2c -Cn1 -Cn2 -Os -c $rw"; $snmpw = "/usr/local/bin/snmpwalk -Os -c $rw"; $syst=`$snmpg $ip system sysName sysObjectID`; sysDescr.0 = STRING: Cisco Internetwork Operating System Software... (1 Reply)
Discussion started by: popeye
1 Replies

10. Shell Programming and Scripting

How to concatenate two strings or several strings into one string in B-shell?

like connect "summer" and "winter" to "summerwinter"? Can anybody help me? thanks a lot. (2 Replies)
Discussion started by: fontana
2 Replies
Login or Register to Ask a Question