Solved

Loop delimiters and pass as parameter

Posted on 2011-03-13
9
357 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
  • 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

803 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