assign colon delimited strings to variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting assign colon delimited strings to variables
# 15  
Old 01-29-2009
Wow man thanks!

Storm was way worse than anyone thought it was going to be, killed my home net connection so I could not get back to this until now.

Thank you so much for the script, and it would work fine if their files were consistent, but upon further examination I have now realized they are not.

The DBA's here need to be kicked in the head, or maybe they already have been, that would explain a lot...

ANYWAY

File2's format is the most consistent so I am going to work off of it, just like in the script you have written. Since I can't assume the 3rd field will be an email, since there are some middle names, some have two last names, some have no spaces, some are Jr's, the list goes on and on... I have no intention of trying to write a condition for all of those situations.

So here is my idea:
1. I am going to try to script it to search for an email in each line of File2 (ie ":*@*:" or something like that).

2. Once I've got an email I will search in the other file for a line that has that email somewhere in it.

3. Once I have that line I will get the 7 digit number (which is the ID #) out of it.

4. Then I will also search this same line for the email that ends in the specified string.

5. Then take those two items and add the ID number to the front of the line from file2 and the email ending in the specified string to the end of it and write it back out.

I think this is the best way (and maybe only way) to do it....

I apologize for not realizing that ht was inconsistent before, the DBA said it was and I took their word for it (my mistake there).

Thanks again for trying, I think I have learned what I needed to know from looking at your script, to get this done.

I'll post what I come up with in case it may help someone else in the future.
# 16  
Old 01-29-2009
OK I was wrong I don't know all I need to know to do this...

I can use this:
Code:
while read F2Line
   do
         .....
            search var F2line for the string that is the email
         .....

done<${FILE2}

to look at file two line by line. At that point I need to search that line (stored in variable name F2Line) which will be an undetermined bunch of strings separated by :'s iin one line for the string containing the email address.

How do I do that?
# 17  
Old 01-29-2009
Hammer & Screwdriver Show some of the 'bad' data

Just line I did, show some of the data in an irregular format for the two files.
Something like the following will make it easier to understand:

Code:
> cat file151
bob:johnson:3343:bjohn@email_SB.org,bjohn@emailARG.org,3343@email.org
sol:admin:3344:sadmin@email_SB.org,sadmin@emailARG.org,3344@email.org
joe:sample:3345:jsample@email_SB.org,jsample@emailARG.org,3345@email.org
> cat file152
bob:johnson:bjohn@email_SB.org
sol:admin:3344@email.org
joe:sample:jsample@email_SB.org

# 18  
Old 01-29-2009
OK I will try to give some good examples here. With the occasional extra crap bolded.

file151:
bob:johnson:3343:bjohn@email_SB.org,bjohn@emailARG.org,3343@email.org
sol:admin:3344:sadmin@email_SB.org,sadmin@emailARG.org,3344@email.org
joe:sample:JR:3345:jsample@email_SB.org,jsample@emailARG.org,3345@email.org
john:De:Salva:3346:jds@email.com,jDesalva@emailRAG.edu,jSalva@email.com

Now for file152 what you have there is correct but sometimes you get an extra name field so I will add a few for example:
bob:johnson:bjohn@email_SB.org
sol:admin:3344@email.org
joe:sample:jsample@email_SB.org
bill:jones:jr:bjones@email.edu
bill:jones:john:jr:sampson:bjs@emailARG.edu

Things that are safe to assume:
file152:
This will have an email (and only one email) in each line.
This is the email I will need to trim out and use to get the corresponding line out of file151 to work with...

file151:
This will have at least one line with an email in it that will match the email from the file152.
Each line in this file will have a unique 4 digit number that is the Id number somewhere in it. (I need to get that out and into a variable)
Also somewhere in each line of this file there will be an email ending in "something@ARG.edu" (I need to get that out too)

SO once I get the ID and the something@ARG.edu email out of each line in file 151 I will write the line back out to a new file like this:

IDfrom151:entire line from file 152:something@ARG.edu email from 151

follow me? Or do I need to give more examples lines?

Where the other script fails is in the cut commands, they assume field 3 is email when in fact field 4 could be email, if there is a Jr. in the mix. Or field 5 if there is a Jr. and 2 middle names...
# 19  
Old 01-29-2009
Hammer & Screwdriver Still grappling with that different data

Something I was thinking about was to insert another character to help delimit the first file. It is not all done, and doesn't yet accomplish everything. But, while I had a few spare moments, thought I'd pass this along as a possible approach.
Experiment with that sed command and see what it does to you input file, and then it will be clear how you can cut with the additional delimiter of ~.
Unsure yet if the same type of approach can be done with the 2nd file.


If my first file now looks like:
Code:
> cat file151a
bob:johnson:3343:bjohn@email_SB.org,bjohn@emailARG.org,3343@email.org
sol:admin:3344:sadmin@email_SB.org,sadmin@emailARG.org,3344@email.org
joe:sample:JR:3345:jsample@email_SB.org,jsample@emailARG.org,3345@email.org
john:De:Salva:3346:jds@email.com,jDesalva@emailRAG.edu,jSalva@email.com

Then I can use sed to place a ~ before the first number, and that can help me grab data.
See code below:
Code:
> cat make153a.sh
#! /usr/bin/bash

FILE1="file151a"      #1 input file
FILE1x="file151ax"    #1 input file fixed
FILE2="file152a"      #2 input file
FILE2x="file152ax"    #2 input file fixed
FILE3="file153a"      #output file

MATCH_EM="@emailARG.org"

sed "s/:[0-9]/~&/" ${FILE1} >${FILE1x}

savifs=$IFS
IFS=":"

rm ${FILE3} 2>/dev/null   #delete the file, if it exists

while read FNAME LNAME EMAIL
   do
   EMAIL1=`grep "${EMAIL}" ${FILE1x} | cut -d"~" -f2 | cut -d":" -f3`
   EMAIL2=`echo ${EMAIL1} | tr "," "\n" | grep "${MATCH_EM}" `
   ID=`grep "${EMAIL}" ${FILE1x} | cut -d"~" -f2 | cut -d":" -f2`

   echo "${ID}:${FNAME}:${LNAME}:${EMAIL}:${EMAIL2}"
#   echo "${ID}:${FNAME}:${LNAME}:${EMAIL}:${EMAIL2}" >>${FILE3}
done<${FILE2}

# 20  
Old 01-29-2009
I follow you. Looking at this I now see how you are using the "tr" command, that will help out a lot too..

I have some ideas I am trying too, let me get something together and I will post it and see what you think.
# 21  
Old 01-29-2009
This may be a different direction than you are going but can you help me out here:
Code:
#!/bin/bash

var="bob:johnson:3454:jb@email.edu:test:fu.org::"
echo $var
found=`echo ${var} | tr ":" "\n" | grep ####`
echo $found

I am trying to get 3454 onto the found var. Am I close?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assign Unknown Values to Variables

i have a program that spits out a certain number of values. i dont know the number of values. they can be 4, 10, 7, 20, no idea. but, i want to be able to assign each of the value returned by this program to a variable. in the latest instance, the program gave the following 6 values: 4... (8 Replies)
Discussion started by: SkySmart
8 Replies

2. Shell Programming and Scripting

A better way to assign values to variables - shell

so i've been used to doing it this way: SVAL=$(echo "7 3 2 38 3" | awk '{print $2}') 4VAL=$(echo "4:21:N:3" | awk -F":" '{print $4}') I know there's a way to do it by putting the value in an array and assigning it that way. but i'm not sure how to do it efficiently. any ideas? i dont... (9 Replies)
Discussion started by: SkySmart
9 Replies

3. Shell Programming and Scripting

Convert Columns in Rows delimited by a strings

Hi Gurus, I have a file that contain inventory information from someones computers: UserName domain\user1 DNSHostName machine1 Caption Microsoft Windows 7 Professional OSArchitecture 64 bits SerialNumber XXX Name HP EliteBook Revolve 810 G1 NumberOfProcessors 1 Name Intel(R)... (2 Replies)
Discussion started by: gilmore666
2 Replies

4. Shell Programming and Scripting

Need a script to convert comma delimited files to semi colon delimited

Hi All, I need a unix script to convert .csv files to .skv files (changing a comma delimited file to a semi colon delimited file). I am a unix newbie and so don't know where to start. The script will be scheduled using cron and needs to convert each .csv file in a particular folder to a .skv... (4 Replies)
Discussion started by: CarpKing
4 Replies

5. Shell Programming and Scripting

splitting tab delimited strings

hi i have a requirement to input a string to a shell script and to split the string to multiple fields, the string is copied from a row of three columns (name,age,address) in an excel sheet. the three columns (from excel) are seperated with a tab when pasted in the command prompt, but when the ... (2 Replies)
Discussion started by: midhun19
2 Replies

6. Shell Programming and Scripting

match and assign variables

Hi guys, I'm currently writing a script for automating a FreeBSD ZFS setup (ZFSonRooT). I got stuck at one point for raidz(1,2 a.k.a raid5,6) and am in need of assistance. This is what I need. example: #!/bin/sh <- must stay sh echo -n "hdd list: " read hdd_list echo -n "hdd label list:... (2 Replies)
Discussion started by: da1
2 Replies

7. Shell Programming and Scripting

How to read a delimited string and assign fields to incremented variables?

How can I read a string delimited on spaces and assign the fields to incremented variables. For example: Given $exts= txt dat mov I want to read in $exts and have "txt" "dat" and "mov" assigned to incremented variables like $ext1, $ext2, etc. I would like to do this in a loop so that I can... (4 Replies)
Discussion started by: runit
4 Replies

8. Shell Programming and Scripting

Assign value to external variables from awk

Hello I have a text file with the next pattern Name,Year,Grade1,Grade2,Grade3 Name,Year,Grade1,Grade2,Grade3 Name,Year,Grade1,Grade2,Grade3 I want to assign to external variables the grades using the awk method. After i read the file line by line in order to get the grades i use this ... (2 Replies)
Discussion started by: Spleshmen
2 Replies

9. Shell Programming and Scripting

Assign script parameters to variables

Hi, I have a bash script that accepts some parameters as input, like: sh script.sh first second third ..... I want to save these parameters as different variables, something like: VAR1=first VAR2=second etc I tried this, but apparently it didn't worked....... (16 Replies)
Discussion started by: giorgos193
16 Replies

10. Shell Programming and Scripting

Assign variables with cut

I need to read a file (a list) and assign the value to a variable (for each line), I'm looping until the end of the file. My problem is, I want to assign 2 separate variables from the list. The process I'm using is: awk '{print $3}' file1 > file2 awk '{print $4}' file1 > file3 cat file2... (2 Replies)
Discussion started by: douknownam
2 Replies
Login or Register to Ask a Question