Need help with first shell script pls.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with first shell script pls.
# 1  
Old 08-16-2011
Need help with first shell script pls.

Hi,

I'm trying to extract information from one file to update another one and am a bit stuck.

the first file is made up of tags e.g.

<item>a@b.com</item>
jksdhfjkdsh sldkjfds l klsjdf
<item> c@d.com </item>

what i'd like to do is extract the email addresses between these tags, sometimes they have spaces between the email address and the tag as well.

I'm able to grep for the lines but havent been able to extract the string I need.

I then would like to take the email addresses found and update a line in another file which is:

To: x@x.com

I'd like this line to have the email addresses from the other file e.g.

To: a@b.com, c@d.com

Thanks for all your help.
# 2  
Old 08-16-2011
Hi,

Once you grepped the right line, you can use this :
... | sed -e "s/^<item> *\([^ ]*\) *<\/item>/\1/"
It will simply select everything between item tags, without spaces. So, it does not check if the mail adress is right.

After, to update your second file, if the "To: " is unique, you can do :
sed -i -e "s/^\(To:.*\)$/\1, $newAdress/" myFile.txt

Hopping it will help you ^^
Bye
This User Gave Thanks to LeNouveau For This Post:
# 3  
Old 08-16-2011
Thanks for the tip, it didnt quite work though. I tested it as follows:

echo "<item> testst <item>" | sed -e "s/^<item> *\([^]*\) *<\/item>/\1/"
sed: -e expression #1, char 33: Unmatched [ or [^

---------- Post updated at 04:25 PM ---------- Previous update was at 04:24 PM ----------

I corrected the item tag but still same error:

echo "<item> testst </item>" | sed -e "s/^<item> *\([^]*\) *<\/item>/\1/"
sed: -e expression #1, char 33: Unmatched [ or [^

---------- Post updated at 04:27 PM ---------- Previous update was at 04:25 PM ----------

Hi,

Sorry it did work, I didnt realise at first there is a space in here [^ ].

Can you explain what the code is doing please?

I'll try the second part now Smilie

---------- Post updated at 04:37 PM ---------- Previous update was at 04:27 PM ----------

Hi,

I'm trying to do:

cat file | sed -e "s/^<item> *\([^]*\) *<\/item>/\1/"

but it just seems to output all the file rather than just extracting the strings I need.

Thanks for your help again.
# 4  
Old 08-16-2011
newb1000, this may be a simpler way to do it, and follow:
Code:
echo "<item> a@b.com   </item>" | sed -e 's/<item>//' -e 's/<\/item>//' -e 's/ //'

What is happening here is, you search for pattern that you are looking for, for example, <item>, and replace it with nothing - the part between the second and the third /.

- GP
This User Gave Thanks to g.pi For This Post:
# 5  
Old 08-16-2011
Yes, the space is useful ^^

So, let's explain what happens : sed -e "s/^<item> *\([^ ]*\) *<\/item>/\1/"

"s/blabla1/blabla2/" means we'll replace blabla1 by blabla2.
"^<item> *" means we want a line, which starts with "<empty>" and spaces (or not)
" *<\/item>" means we want a line, which finishes by spaces (or not) and </item>. The backslash is used to not interpret the following slash.
\(blabla\) means "blabla" is useful and we need to save it for after.
[^ ]* means we are looking for everything except spaces.
"\1", in the second part is calling the first important thing saved in the first part -> \(blabla\)

So, we are selecting all the line surrounded by <item>...</item> and only keep what is inside, except space.
# 6  
Old 08-16-2011
Thanks, that does make sense, basically you're removing everything apart from what I'm searching for.

I tried this with:

cat file | grep "<item>" | sed <and your command>

which leaves me with the email addresses I want.

How is it best to then save these results and apply them to the other file?

Quote:
Originally Posted by g.pi
newb1000, this may be a simpler way to do it, and follow:
Code:
echo "<item> a@b.com   </item>" | sed -e 's/<item>//' -e 's/<\/item>//' -e 's/ //'

What is happening here is, you search for pattern that you are looking for, for example, <item>, and replace it with nothing - the part between the second and the third /.

- GP
---------- Post updated at 04:54 PM ---------- Previous update was at 04:51 PM ----------

Thanks man, that makes sense as well now.

There is one bit, I'm still a bit confused with though:

[^ ]*

doesnt that mean look to the start of the line again for one or more spaces?

Quote:
Originally Posted by LeNouveau
Yes, the space is useful ^^

So, let's explain what happens : sed -e "s/^<item> *\([^ ]*\) *<\/item>/\1/"

"s/blabla1/blabla2/" means we'll replace blabla1 by blabla2.
"^<item> *" means we want a line, which starts with "<empty>" and spaces (or not)
" *<\/item>" means we want a line, which finishes by spaces (or not) and </item>. The backslash is used to not interpret the following slash.
\(blabla\) means "blabla" is useful and we need to save it for after.
[^ ]* means we are looking for everything except spaces.
"\1", in the second part is calling the first important thing saved in the first part -> \(blabla\)

So, we are selecting all the line surrounded by <item>...</item> and only keep what is inside, except space.
---------- Post updated at 05:01 PM ---------- Previous update was at 04:54 PM ----------

Hi LeNouveau,

There is something interesting, if I run:

cat file | grep "<item>" | sed <your command> nothing happens

but if i do echo "<item>.......</item>" | sed <your command> then I can see the string gets extracted.

If I try the first method with g.pi's sed command, it works as expected.
Do you know what's happening differently with yours?

Thanks
# 7  
Old 08-16-2011
Usually, when you put something between [], that means you're looking for one of the things inside. But if you insert "^" at the begining, that means you're looking something that is not inside.
It inverts rule matching.

My function is more restrictives than g.pi's.
For example, if there is a space before the "<item>", because of my "^<item>", it will not match anymore and it become useless.

My first idea was :
... | grep -o "[a-zA-Z0-9.-]@[a-zA-Z0-9.-]"
but if there is a not expected character, it does not work anymore.

So, I think the simplest way is g.pi's ^^'
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script with following awk command pls help

Hi I want to create a shell script with the following awk command & also get the filenames in output. awk '/<catetcsecuretty0>/ {p=1} /<catvarlogmessages0>/ {p=0} p' *.xml As there will be multiple outputs related to many xml files I cannot identify which output belongs to which file ... (5 Replies)
Discussion started by: sharp488
5 Replies

2. Shell Programming and Scripting

Shell script for adding a table in mysql with 10,000 lines ... pls help

Hi , I am new to shell scripting . I need to write a shell script in sql to add 10,000 lines of data in a table . Pls help guys :) ---------- Post updated at 07:08 PM ---------- Previous update was at 03:40 PM ---------- guys please help !!! (3 Replies)
Discussion started by: vinumahalingam
3 Replies

3. Shell Programming and Scripting

Error in Shell Script - Can anyone help Pls

Please find my shell script below ------------------------------------- #!/usr/bin/ksh ORAUSER=$1 P_REQUEST_ID=$4 current_time=`date +%m%d%y.%H%M%S` echo "Process started at `date +%m/%d/%y.%H:%M:%S`" #Intialize Variables export SHLIB_PATH=/usr/local/lib ext=".pdf" ps_ext=".ps"... (4 Replies)
Discussion started by: uuuunnnn
4 Replies

4. Shell Programming and Scripting

shell script, pls help

# for i in `cat oo`;do ls -ld $i;done ls: /var/tmp/i: No such file or directory ls: i: No such file or directory ls: /var/tmp/ii: No such file or directory ls: i: No such file or directory ls: /var/tmp/iii: No such file or directory ls: i: No such file or directory ls: /var/tmp/iiii: No such... (2 Replies)
Discussion started by: cpttak
2 Replies

5. Shell Programming and Scripting

Shell Script Required? Pls. help me

Hi All, I have Information in the file like, ============ Interface Information ==================== +++++++++++++++++ NMInterface ++++++++++++++ ObjID:251c55a2-2257-71dd-0f68-9887a1f10000 NNMObjID:82857 EntityName:aust00m1.mis.amat.com ] Description:ATM9/0/0-atm layer Discovered in... (22 Replies)
Discussion started by: ntgobinath
22 Replies

6. Shell Programming and Scripting

Shell Script Requirements pls

Moderators note: This user has been banned for persistent rule breaking despite being warned that this would be the result. (0 Replies)
Discussion started by: tt1ect
0 Replies

7. Shell Programming and Scripting

Unix shell script couldn't be executed. Pls help!

I have wrriten a script to call sql script to do some work in database. However, the script couldn't be executed. The only information was: ksh: ./updt_attrib.ksh cannot execute. Please help me to identify where the problem is. I post script here for your reference. Thanks a lot. #!/bin/ksh ... (8 Replies)
Discussion started by: duke0001
8 Replies

8. Shell Programming and Scripting

Passing value from shell script to .pls file

I have a shell script which takes at the command prompt options like ss1.sh -F SCOTT -T JOHN F- From User T- To User I want to pass the From User(SCOTT) Value to another script ss2.pls (This script runs a PL/SQL Program). Depending on the FromUser value in the ss1.sh script i have to... (4 Replies)
Discussion started by: dreams5617
4 Replies
Login or Register to Ask a Question