Split fields from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split fields from a file
# 1  
Old 09-05-2014
Split fields from a file

Hi All,

I have a file where a list of email id's are stored [seperated by space] as shown below:

In my shell script, i am sending emails to above id's
My requirement is to seperate the email id's into 2 groups..
emailid1@blh.com emaild2@blh.com emailid3@blh.com should come in one group and asdf@blah.com should come in another group, so that i can call email functionality twice. Currently, i am using below code to achive this:

Code:
group1=`cat $folder/split_email_users.txt | tr ' ' '\n' | grep -w "group1@blah.com"`
group2=`cat $folder/split_email_users.txt | tr ' ' '\n' | grep -v "group1@blah.com" | tr '\n' ' '`

Is there a better way to achieve this? Without using multiple commands. Please help.....

Regards,
# 2  
Old 09-05-2014
Hi,

Following command may help you.

Code:
echo "emailid1@blh.com emaild2@blh.com asdf@blah.com emailid3@blh.com" | awk '{for(i=1;i<=NF;i++){if($i ~ /asdf@blah/){group2=$i} else {group1=group1?group1 OFS $i:$i}}} END{print "group1= "group1 ORS "group2= "group2}'

Output will be as follows.

EDIT: To get values into variables we can use following.

Code:
group1=`awk '{for(i=1;i<=NF;i++){if($i !~ /asdf@blah/){group1=group1?group1 OFS $i:$i}}} {print group1}' test2`
echo $group1
emailid1@blh.com emaild2@blh.com emailid3@blh.com
group=`awk '{for(i=1;i<=NF;i++){if($i ~ /asdf@blah/){group2=group2?group2 OFS $i:$i}}} {print group2}' test2`
echo $group
asdf@blah.com

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-05-2014 at 05:52 AM.. Reason: Added solution in getting values in variables
# 3  
Old 09-05-2014
With Shell:
Code:
group1= group2=
while read line
do 
  for i in $line
  do   
    case $i in
      (*@blah.com)
         group1="$group1 $i" ;; 
      (*) 
         group2="$group2 $i"
    esac
  done
done < file
echo "\$group1: $group1"
echo "\$group2: $group2"






----
@ravinder: That will not put it into shell variables...

Last edited by Scrutinizer; 09-05-2014 at 05:14 AM..
# 4  
Old 09-05-2014
With a recent shell, try using arrays
Code:
TMP=(emailid1@blh.com emaild2@blh.com asdf@blah.com emailid3@blh.com)
group1=${TMP[@]//*@blah.com/ }
group2=${TMP[@]//*@blh.com/ }

# 5  
Old 09-05-2014
Thanks RavinderSingh13 and Scrutinizer. Will try the approaches suggested by you and update.

Hi RudiC, sorry for the typo error... but my input file is like below

Can i still use arrays and achieve the results? Pls let me know.

Regards,
# 6  
Old 09-05-2014
If you aren't keying off of a difference in the data following the @, how is your script supposed to determine which email addresses are supposed to be in which group???
# 7  
Old 09-05-2014
After making up your mind to answer Don Cragun's question, you of course can use that. E.g.:
Code:
group1=${TMP[@]//emailid*/}

It might become a bit difficult if you then want all the rest, everything that doesnot begin with "emailid".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to add plus or minus to fields and split another field

In the tab-delimited input below I am trying to use awk to -10 from $2 and +10 to $3. Something like awk -F'\t' -v OFS='\t' -v s=10 '{split($4,a,":"); print $1,$2-s,$3+s,a,$5,$6} | awk {split(a,b,"-"); print $1,$2-s,$3+s,b-s,b+s,$5,$6}' input should do that. I also need to -10 from $4... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Split a free form text delimited by space to words with other fields

Hi, I need your help for below with shell scripting or perl I/P key, Sentence customer1, I am David customer2, I am Taylor O/P Key, Words Customer1,I Customer1,am Customer1,David Customer2,I Customer2,am Customer2,Taylor (4 Replies)
Discussion started by: monishathampi
4 Replies

3. Shell Programming and Scripting

awk split lines without knowing the number of fields a-priori

I want to use awk to split fields and put them into a file but I don't know the number of fields for example, in the following line Ports: 22/filtered/tcp//ssh///, 53/open/tcp//tcpwrapped///, 111/filtered/tcp//rpcbind///, 543/filtered/tcp//klogin///, 544/filtered/tcp//kshell///,... (3 Replies)
Discussion started by: esolvepolito
3 Replies

4. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 Replies

5. Shell Programming and Scripting

AWK:Split fields separated by semicolon

Hi all, I have a .vcf file which contains 8 coulmns and the data under each column as shown below, CHROM POS ID REF ALT QUAL FILTER INFO 1 3000012 . A G 126 ... (6 Replies)
Discussion started by: mehar
6 Replies

6. Shell Programming and Scripting

how to split the row(array) in to fields and store in to oracle database in unix

Hi, the csv file with the delimeter #. A#B#C#D#E#F#G#H 1#2#3#4#5#6#7#8 Z#x#c#V 7#2#8#9 N. I want to read the file line by line and store in rowarray. then the rowarray content should be spilt or made to fields using the delimeter #. i am not sure can we store the fields in to... (3 Replies)
Discussion started by: barani75
3 Replies

7. Web Development

split the fields in a column into 3 columns

Have a column "address" which is combination of city, region and postal code like. Format is : city<comma><space>region<space>postal code abc, xyz 123456 All these three city, region and postal code are not mandatory. There can be any one of the above. In that case a nell... (2 Replies)
Discussion started by: rakshit
2 Replies

8. Shell Programming and Scripting

How to split a field into two fields?

Hi, I have a comma delimited text file where character fields (as opposed to numeric and date fields) are always enclosed with double quotes. Records are separated by the newline character. In a shell script I would like to split a particular field into two separate fields (enclosed with double... (4 Replies)
Discussion started by: vbrown
4 Replies

9. Shell Programming and Scripting

split varibles and store fields into shell varible array

I need to split a long varible which is a whole line read from a file into fields and store them in an array, the fields are delimited by pipe and a field may contain white spaces. I tried the following concept test and it has problem with field 5 which contain a space, appearently so because... (3 Replies)
Discussion started by: gratus
3 Replies

10. Shell Programming and Scripting

returning split fields

I have a variable with data in this format field1;field2;field3 I wanted to split the variable like this field1 field2 field3 this statement was working fine echo $key_val | awk '{gsub(";" , "\n"))' but sometimes we get the data in the variable in this format... (3 Replies)
Discussion started by: mervin2006
3 Replies
Login or Register to Ask a Question