Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Loop delimiters and pass as parameter

Posted on 2011-03-13
9
Medium Priority
?
382 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 400 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 1600 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

916 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