Solved

Loop delimiters and pass as parameter

Posted on 2011-03-13
9
373 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
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 …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

630 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