I need to loop through and assign vars to the values:
var Fname = bob
var Lname = johnson
var email = email@email.com
If there is a middle initial I want to toss it and move on and get the last name from the next (3rd) field.
I have tried this:
#!/bin/bash
while read line
do
Fname=`cut -d: -f1`
Field2=`cut -d: -f2`
if [ "$Field2" = "##" ]; then
Lname=`cut -d: -f3`
else
Lname=$Field2
fi
echo -e "First Name is :$Fname\n"
echo -e "Last Name is :$Lname\n"
done < test
BTW this code is tabbed to be easier to read but this board is taking them out of it for some reason...
Needless to say it isn't working or I would not be posting this....
I get this as an output for example:
First Name is :Linda
Wendy
Esther
Pamela
Richard
William
Leona
Anne
Elaine
What I am doing is searching for any 2 characters (the .. does this in sed) with the third character a . (need to do \. in sed so it knows I want the period), and deletes those three if they exist.
I need to loop through and assign vars to each value in the line so I can do some testing on them.
Man this is getting complicated, I hate asking for all this help to do things I used to be so good at a long time ago, but I do really appreciate the refresher, so thanks a tun!
Big Picture:
I am going to have one file like the above format. Then another simular file with different colon delimited values. I need to pull lets say last name from file 1 and find it in file 2. Then build a file 3 with the values from the first two files but in a different order..
You follow me?
Just thought it may help if you knew the grand scope of what is going on here...
while read line
do
echo $line
Fname=`echo $line|cut -d: -f1`
Field2=`echo $line|cut -d: -f2`
len=`echo $Field2|awk '{print length($0)}'`
if [ $len -eq 2 ]; then
Lname=`echo $line|cut -d: -f3`
else
Lname=$Field2
fi
echo "First Name is =$Fname\n"
echo "Last Name is =$Lname\n"
done < 9.txt
Oh also the files are not ordered the same, so I have to pull the email from file 2, search for (ie grep) the correct line from file 1 off that, and compile file 3's line based on what I find.
How big are the two files?
If reasonable size, then a grep from one file to another might not be too bad.
The better approach would be with awk, and setting up a couple of arrays to help link the data.
The files are about 8000 records or so each. File 1 is 1.6 M, file 2 is 343 k.
FYI every tool you mentioned there are ones I am not too strong with, I can do loops, if thens, and geps pretty well but I never learned awk, and have relatively no experience with using arrays. I know I know it's shameful but I always have to do stuff the hard way... BUT I will take any help I can get, I am a fast learner.
Just so you know I just heard we are in the middle of a snow storm here and they are closing our building early... Sucks to work in a room with no windows, I had no idea! ANYWAY, I will be off the board for a few hours while I make my way home but I will continue on with this once I get there.
Let me know if you have any questions on the coding or commands. Some of them could have been combined or simpified, but I thought keeping in this layout would make it easier for you to follow the logic.
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:
I am going to try to script it to search for an email in each line of File2 (ie ":*@*:" or something like that).
Once I've got an email I will search in the other file for a line that has that email somewhere in it.
Once I have that line I will get the 7 digit number (which is the ID #) out of it.
Then I will also search this same line for the email that ends in the specified string.
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.
OK I was wrong I don't know all I need to know to do this...
I can use this:
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.
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...
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.