assign colon delimited strings to variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting assign colon delimited strings to variables
# 1  
Old 01-27-2009
assign colon delimited strings to variables

Man it has been too long since I have had to do this type of stuff...

OK I have a file with lines in it looking like this:
bob:johnson:email@email.com (most lines)

john:F.:doe:email2@email.com (but some are like this)

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:
Code:
#!/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

Last Name is :



Any help is appreciated.

Last edited by joeyg; 01-27-2009 at 03:38 PM.. Reason: Applied codetags to make easier to read
# 2  
Old 01-27-2009
Hammer & Screwdriver Maybe this will get you started...

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.

Code:
> echo "john:F.:doe:email2@email.com"
john:F.:doe:email2@email.com
> echo "john:F.:doe:email2@email.com" | sed "s/..\.//g"
john:doe:email2@emacom

Now, your data is clean without the initial.

Note... as I think about this, the following might be better as it seaches for : then any character than period:

Code:
> echo "john:F.:doe:email2@email.com" | sed "s/:.\.//g"
john:doe:email2@email.com

# 3  
Old 01-27-2009
First again, thank you very much.

So now I will have:

bob:johnson:email1
jan:geters:email2
ricky:sticky:email3

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...
# 4  
Old 01-27-2009
Tools

Can you post samples of the two originating files?
And, what the data should look like in the final (third) file?

Comment and describe where appropriate.
# 5  
Old 01-27-2009
Will do, and thanks again for being willing to help me here.

I think I am on to something so please give me just a minute.
# 6  
Old 01-27-2009
Revised Code...

A slight modification in the sender's own code :

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
# 7  
Old 01-27-2009
Code:
/bin/awk -F: '!/^#/ {print $1, $2, $3}' somefile | \
while read Fname Lname email; do
 echo "Fname:$Fname - Lname:$Lname - Email:$email"
done

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