assign colon delimited strings to variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting assign colon delimited strings to variables
# 22  
Old 01-29-2009
Never mind I got it:
Code:
#!/bin/bash
var="bob:johnson:3454:jb@email.edu:test:fu.org::"
echo $var
found=`echo ${var} | tr ":" "\n" | grep "\#\#\#\#"`
found=`echo ${var} | tr ":" "\n" | grep "[0-9][0-9][0-9][0-9]"`
echo $found

Thanks anyway!
# 23  
Old 01-30-2009
Wow. Won't it be simpler to do:
Code:
var=`echo $var |awk -F: '{print $3 }'`


Last edited by otheus; 01-30-2009 at 06:52 AM.. Reason: forgot input stream
# 24  
Old 01-30-2009
Hammer & Screwdriver Or try

If you know it will always be four digits, but not sure where in the string

Code:
> echo "bob:johnson:3454:jb@email.edu:test:fu.org::" | tr ":" "\n" | grep "[0-9]\{4\}"
3454

# 25  
Old 02-03-2009
Quote:
Originally Posted by otheus
Wow. Won't it be simpler to do:
Code:
var=`echo $var |awk -F: '{print $3 }'`

That would be much cleaner but it is not always field 3.
# 26  
Old 02-03-2009
Well guys this is what I ended up with:
Code:
#! /bin/bash
#set vars to file names
FileFC="FCformated.txt"
FileWHD="WHDformated.txt"
FileOut="NewFile.txt"

#set email ending to search for
MATCH_EM="@antioch.edu"

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

#loop through file line by line
while read FileWHDline
   do
   #set vars to empty string
   EmailWHD=""
   FileFCline=""
   ID=""
   AntEmailField=""
   AntEmail=""
   EmailWHDwCom=""
   EmailWHDwCol=""
	#check for colon at end, if not add one
	case $FileWHDline in
        	*:) ;; 
        	*) FileWHDline="${FileWHDline}:" #no colon at end so put one in;;
	esac	
   #get email from file to search other file with
   EmailWHD=`echo ${FileWHDline} | tr ":" "\n" | grep "@"`
	#if blank write out original line and continue with next while loop
	if [ "$EmailWHD" == "" ]; then
   		echo "${FileWHDline}" >> ${FileOut}
		continue
	fi
	EmailWHDwCom="\,${EmailWHD}"
	EmailWHDwCol="\:${EmailWHD}"
   #Find line in FC file by looking for comma email
   FileFCline=`grep -i "${EmailWHDwCom}" ${FileFC}` 
	#if blank try with colon email
	if [ "$FileFCline" == "" ]; then
		#look for line with colon email
   		FileFCline=`grep -i "${EmailWHDwCol}" ${FileFC}` 
			#if blank write out original line and continue with next while loop
			if [ "$FileFCline" == "" ]; then
   				echo "${FileWHDline}" >> ${FileOut}
				continue
			fi
	fi
   #look for 7 digit num in string it will be the ID
   ID=`echo ${FileFCline} | tr ":" "\n" | grep "^[0-9][0-9][0-9][0-9][0-9][0-9][0-9]$"`
   #find coma seperated email field within line
   AntEmailField=`echo ${FileFCline} | tr ":" "\n" | grep ","`
	#if blank test for ID then write out and continue with next while loop
	if [ "$AntEmailField" == "" ]; then
		if [ "$ID" == "" ]; then
   			echo "${FileWHDline}" >> ${FileOut}
			continue
		fi
   		echo "${ID}:${FileWHDline}" >> ${FileOut}
		continue
	fi
   #get email out of email that ends with ARG string set above
   AntEmail=`echo ${AntEmailField} | tr "," "\n" | grep -m 1 "${MATCH_EM}"`
	#if blank test for ID then write out and continue with next while loop
	if [ "$AntEmail" == "" ]; then
		if [ "$ID" == "" ]; then
   			echo "${FileWHDline}" >> ${FileOut}
			continue
		fi
   		echo "${ID}:${FileWHDline}" >> ${FileOut}
		continue
	fi
   #if you made it to here you have both ID and email so wrap the orig line with them and write it out
   echo "${ID}:${FileWHDline}${AntEmail}" >> ${FileOut}
done<${FileWHD}

I had a hell of a time dealing with all the different inconsistencies in the files I was given.

Thank you very much for the help, this would have taken another week or so to get done on my own, maybe more...

I hope someone finds this useful. If anyone has any questions ask away. BUT do it quick, I will probably forget why I did what pretty quickly Smilie
# 27  
Old 02-03-2009
Wow, call me stupid. I somehow missed the entire first page of posts.

Okay, this is too complex for awk or sed, so I'd approach this with perl.
Code:
perl -ne '
while (@a=split(":"), $a[2] !~ /@/) { 
     if ($a[2] =~ /^[0-9]+$/) { 
       # delete the 3rd field
       splice(@a,2,1); 
     } 
     else { 
       # delete the 2nd field (middle name)
       splice(@a,1,1); 
     } 
     $_=join(":",@a); 
} 
# strip all but the first email address.
$a[2]=s/,.*//;
# Report results (separated by spaces -- see $\)
$\=" "; print "@a[0,1,2]\n"; '
'

Keep in mind, perl (normally) starts arrays with 0, whereas awk starts with 1. So when I say 3rd, I mean, $a[2].

So basically, this keeps deleting the second field (or 3rd if the 3rd is a number) UNTIL the third field is an email address (has a '@' in it).

Last edited by otheus; 02-03-2009 at 10:30 AM.. Reason: strip other email addresses
# 28  
Old 02-03-2009
OK one more easy question. I have this a file with a bunch of lines like:

bob:johnson:email.com:
jim:carey:email.com:
bill:withers:email.com
saly:jims:email.com

Some have ":" at the end some don't.

and I want to get rid of the ":" at the end but leave the ones that don't alone.

I tried:
sed -e 's/:\n/\n/g' WHDTest > fix

and

sed -e 's/:\r/\r/g' WHDTest > fix

but it isn't working???

Any ideas?
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