Solved

Loop delimiters and pass as parameter

Posted on 2011-03-13
9
361 Views
Last Modified: 2012-05-11
input file em.txt:
--------------
123,rf@gm.com,rm@hm.com
525,ps@yh.com,cm@gm.com
625,sw@yh.com,NA
725,NA,m@m.com

Requirement:
Ignore the first column and read each email address from em.txt
If email is NA then skip
If email is valid then pass it as parameter to emailscript.sh

Is there any better way of doing than the below repetition of if code. I will increase the points as the discussion goes.

for i in `cat em.txt`
do  
	em1=`echo $i | cut -f2 -d","`

	if [ ${em1} != "NA" ]
         then
           emailscript.sh $em1
    fi
	em2=`echo $i | cut -f3 -d","`
	if [ ${em2} != "NA" ]
         then
           emailscript.sh $em2
    fi
done

Open in new window

0
Comment
Question by:cutie_smily
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 100 total points
ID: 35125077
If emailscript.sh handles csv addresses, then you could do

for email in $(sed -e "s/[0-9]*,//" -e "s/,*NA,*//" em.txt)
do
   emailscript.sh $email
done

Open in new window


If not, then do

for email in $(sed -e "s/[0-9]*,//" -e "s/,*NA,*//" -e "s/,/ /" em.txt)
do
   emailscript.sh $email
done

Open in new window

0
 

Author Comment

by:cutie_smily
ID: 35125108
Thanks Tintin.
Actually the first column can have alphabets as well. Ex: a625,ext525.
0
 
LVL 12

Expert Comment

by:Amick
ID: 35125277
for email in $(sed 's/,/\n/g' em.txt | grep "@")
do
   emailscript.sh $email
done

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cutie_smily
ID: 35125330
The carriage return is not working for me.

$ sed 's/,/\n/g' tst123
123nrf@gm.comnrm@hm.com

k-shell
0
 

Author Comment

by:cutie_smily
ID: 35125394
Not sure why sed didn't work but tr seem to insert newline.
$ cat tst123 | tr ',' '\n'
123
rf@gm.com
rm@hm.com
0
 
LVL 12

Accepted Solution

by:
Amick earned 400 total points
ID: 35125774
I don't have k-shell installed, but sometimes sed prefers a literal newline character
 sed 's/,/
/g' em.txt | grep @ 

Open in new window

 


If cat and tr works for you, that is fine and you could use this code
for email in $(cat em.txt|  tr ',' '\n'  | grep "@")
do
   emailscript.sh $email
done

Open in new window


You could also use
tr ',' '\n' < em.txt | grep @

Open in new window


In any case, it seems that the newline along with grep provides the datastream you need.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 35126667
Amick has come up with a much better solution than mine.  Great job.
0
 
LVL 12

Expert Comment

by:Amick
ID: 35127342
@Tintin - thanks for the kind words.  They mean a lot coming from an expert with your level of accomplishment.
0
 

Author Comment

by:cutie_smily
ID: 35135448
Thanks for the help. Increasing the points to max as I have another question.

I want to ftp some local files to remote server but I want to backup the remote files before ftp.

My Requirement in detail:
----------------------------
I want to login to ftp account,
create a temp directory,
mover all the *.zip files that are in ftp home directory to this temp directory,
then copy the local machine files to this remote home directory.

I read somewhere that we cannot move the contents in the remote directory to another remote directory so I create a local directory and got all the files from remote machine and did a mput to move all the files back to the new directory. Somehow the script is not executing any commands after the mget. Can you guys please look into it.
cd bkp_files
rm -rf bkp_remote_files
mkdir bkp_remote_files

ftp -in  << EOF >> $LOG
        verbose
        open ${SERVER}
        $LOGON
        mkdir $BKPFILES
        binary
        prompt
        lcd bkp_remote_files
        mget *.zip
        cd $BKPFILES
        mput *.zip
        cd ..
        lcd ..
        mput *.zip
        close
        bye
      EOF

Open in new window




0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 115
numbers ascending pyramid 101 238
What language/protocol is the Angular Chat? 2 92
cscript to activate Windows and Office? 2 85
Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question