Solved

Loop delimiters and pass as parameter

Posted on 2011-03-13
9
348 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
Comment Utility
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
Comment Utility
Thanks Tintin.
Actually the first column can have alphabets as well. Ex: a625,ext525.
0
 
LVL 12

Expert Comment

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

Open in new window

0
 

Author Comment

by:cutie_smily
Comment Utility
The carriage return is not working for me.

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

k-shell
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:cutie_smily
Comment Utility
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
Comment Utility
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
Comment Utility
Amick has come up with a much better solution than mine.  Great job.
0
 
LVL 12

Expert Comment

by:Amick
Comment Utility
@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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

762 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

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now