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:
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?
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.
#!/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.
You could try something like this (using the statements that you wrote):
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
It's to show you where the query goes and how to write it. You can put whatever you want in there.
---------- Post updated at 01:44 PM ---------- Previous update was at 01:38 PM ----------
Also:
table="contact_master"
column="subscribed"
awk -F">" '/@/{print $1}' RS="<" file |
while read email
do
echo "UPDATE $table SET $column='No' WHERE email='$email'"
done | mysql -uroot
Pretty sure you need those ' quotes around $email or mysql will complain. And piping everything into one instance of mysql means you won't need to login seperately for every single line.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE contact_master SET subscribed='No' WHERE email=''' at line 2