assign colon delimited strings to variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting assign colon delimited strings to variables
# 8  
Old 01-27-2009
OK file 1 format:
Fname:Lname:ID_number:multiple emails separated by ,'s
EX:
bob:johnson:3343:bjohn@email_SB.org,bjohn@emailARG.org,3343@email.org

File 2 format:
Fname:Lname:just one email
EX:
bob:johnson:bjohn@email_SB.org

And I need file 3 to look like:
ID_Number:Fname:Lname:email from file 2:whatever email ends in @emailARG.org from file 1

SO with this EX it would be:

3343:bob.johnson:bjohn@email_SB.org:bjohn@emailARG.org

I think I have figuread out how to run through and assign vars to the values using:
Code:
while read line
do
        #echo $line
        Fname=`echo $line | cut -d: -f1`
        echo "Fname = $Fname"
        Lname=`echo $line | cut -d: -f2`
        echo "Lname = $Lname"
        ID=`echo $line | cut -d: -f3`
        echo "ID = $ID"

done < test

test being the name of file 1 from above.

That is as far as I have gotten...
# 9  
Old 01-27-2009
I am looking at the other responses I got while typing that up now, so thanks to all in the mean time...
# 10  
Old 01-27-2009
Oh and BTW I got the DB people to toss the middle initials out so that section can be omitted.
# 11  
Old 01-27-2009
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.
# 12  
Old 01-27-2009
Hammer & Screwdriver

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.
# 13  
Old 01-27-2009
Quote:
Originally Posted by joeyg
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... Smilie 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.

Thanks SO MUCH again for all of the help.

Back soon!
# 14  
Old 01-27-2009
Hammer & Screwdriver Here is a try...

Two raw files
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

Script to do what you want.
It uses variables for the filenames, to make easier to modify.

Code:
> cat make153.sh
#! /usr/bin/bash

FILE1="file151"
FILE2="file152"
FILE3="file153"

MATCH_EM="@emailARG.org"
savifs=$IFS
IFS=":"

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

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

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

The output file created:
Code:
> cat file153
3343:bob:johnson:bjohn@email_SB.org:bjohn@emailARG.org
3344:sol:admin:3344@email.org:sadmin@emailARG.org
3345:joe:sample:jsample@email_SB.org:jsample@emailARG.org

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.
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