Problem in tokenizing the string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem in tokenizing the string
# 1  
Old 04-27-2012
Problem in tokenizing the string

Supposed I have a string in the format:

Code:
<service_name> = <ldap user FDN> : <password>

like

Code:
DNS = cn=user1,o=company : pwd

I want to tokenize like:

Code:
service name:DNS
UserName: cn=user1,o=company
Password: pwd.

Because of the '=' sign between Service Name and LDAP Name I not struggling to parse it. Please help.

Last edited by Scrutinizer; 04-27-2012 at 03:32 AM..
# 2  
Old 04-27-2012
Code:
$ echo "DNS = cn=user1,o=company : pwd" | nawk -F"[\=:]" '{print "Service Name : " $1; print "Password : " $NF}{sub($1"=","");sub(":"$NF,"");print "UserName : " $0}'
Service Name : DNS 
Password :  pwd
UserName :  cn=user1,o=company

# 3  
Old 04-27-2012
Actually I don't want to print in that format. I want the corresponding variables variables assigned to the corresponding value. As I will need the variables further. Like I want servicename variable assigned to DNS. So, if I do echo $servicename, it will print DNS. If I do echo $password it will print pwd, if I do echo $username it will print cn=user1,o=company.

So, I did something like this:

Code:
echo "DNS = cn=user1,o=company : pwd" | awk -F"[=:]" '{servicename="$1"; password="$NF"}{sub($1"=","");sub(":"$NF,"");Username="$0"}'

And then tried echo. But it didn't print anything. Please correct me where I am wrong.

Moderator's Comments:
Mod Comment Please use [code][/code] tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-27-2012 at 05:14 AM..
# 4  
Old 04-28-2012
Any response on this?
# 5  
Old 04-28-2012
Hi.

Edit -- I didn't notice the embedded "=" sign, so the solution from neutronscott, q.v., is the correct one for using bash built-ins.

With bash shell built-ins:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate parse line into variables, bash built-in read.

# List environment for demonstration computation.
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C

line="DNS = cn=user1,o=company : pwd"
pl " Line to be parsed into variables:"
pe "$line"

oldifs="$IFS"
IFS="=:" read s u p junk <<< "$line"

pl " Results of setting variables from read:"
pe "service name: $s"
pe "UserName: $u"
pe "Password: $p"

pl " Results with all spaces eliminated with \"Parameter Expansion\":"
t1=${line// /}
pe " t1 is \"$t1\""
IFS="=:" read s u p junk <<< "$t1"
pe "service name: $s"
pe "UserName: $u"
pe "Password: $p"

IFS="$oldifs"

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39

-----
 Line to be parsed into variables:
DNS = cn=user1,o=company : pwd

-----
 Results of setting variables from read:
service name: DNS 
UserName:  cn
Password: user1,o

-----
 Results with all spaces eliminated with "Parameter Expansion":
 t1 is "DNS=cn=user1,o=company:pwd"
service name: DNS
UserName: cn
Password: user1,o

See man bash for details.

Best wishes ... cheers, drl

Last edited by drl; 04-28-2012 at 03:36 PM..
# 6  
Old 04-28-2012
Code:
[mute@geek ~]$ bash ./test
[DNS] [cn=user1,o=company] [pwd]
[mute@geek ~]$ cat test
#!/bin/bash

var='DNS = cn=user1,o=company : pwd'

service=${var%% *}
user=${var#* = }
pass=${user#* : }
user=${user% : *}

echo "[$service] [$user] [$pass]"

This User Gave Thanks to neutronscott For This Post:
# 7  
Old 04-28-2012
Can you please explain the logic?

Also if I miss the blank between DNS and username then it doesn't work.

Code:
wgp-fap-109:~ # ./srk.sh
DNS=cn=afpuser,o=company
DNS=cn=afpuser,o=company
pwd
wgp-fap-109:~ # cat srk.sh
#!/bin/bash

var='DNS=cn=afpuser,o=company : pwd'

service=${var%% *}
user=${var#* = }
pass=${user#* : }
user=${user% : *}

echo $service
echo $user
echo $pass

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing a long string string problem for procmail

Hi everyone, I am working on fetchmail + procmail to filter mails and I am having problem with parsing a long line in the body of the email. Could anyone help me construct a reg exp for this string below. It needs to match exactly as this string. GetRyt... (4 Replies)
Discussion started by: cwiggler
4 Replies

2. Shell Programming and Scripting

Problem in comparing 2 files string by string

Hi Champs, I am a newbie to unix world, and I am trying to built a script which seems to be far tough to be done alone by me..... " I am having a raw csv file which contains around 50 fields..." From that file I have to grep 2 fields "A" and "B"....field "A" is to be aligned vertically... (11 Replies)
Discussion started by: jitendra.pat04
11 Replies

3. Shell Programming and Scripting

if PATH contains a certain string problem!

Hi I am using MKS Toolkit c shell. I want to basically check if my PATH variable already contains a certain path directory so I tried this (it didnt work!): if: Expression Syntax if ( echo $path |grep -c c:/PROGRA~1/blah/blah ) then please help me get this little statement to work. ... (3 Replies)
Discussion started by: vas28r13
3 Replies

4. Shell Programming and Scripting

string tokenizing

I have a string that looks like this: blahblahblah_^substring^_blahblahblah I need to extract substring, the bit between the ^ characters, into another string variable. This will be in a bash shell script. Thanks. (2 Replies)
Discussion started by: daflore
2 Replies

5. Shell Programming and Scripting

Runaway String Problem

Database.txt John:30:40 echo -n "New Title Please :" read NewTitle awk -F":" 'OFS = ":"{ $1 = "'$NewTitle'" ; print $0 } ' Database.txt> Database2.txt mv Database2.txt Database.txt what this does, is that when i input something into $NewTitle, it will update $1 which is "John" into... (3 Replies)
Discussion started by: gregarion
3 Replies

6. Shell Programming and Scripting

Tokenizing a String in unix

Hi All, I have String i want to tokenize based on one delimiter. Original String is -pComments.properties,-iPELF4 i want to tokenize the original string based on ',' (comma) as delimiter and collect them individually like string1=-pComments.properties string2=-iPELF4 ... (1 Reply)
Discussion started by: rajeshorpu
1 Replies

7. Programming

Problem returning string

Hi all, I have been trying with RSA encryption and decryption. I was able to write a code which worked fine. Then I split it into functions as part of a requirement. However I am not getting the expected result everytime. I know it has something to do with how I am returning but it just does not... (1 Reply)
Discussion started by: Treasa
1 Replies

8. UNIX for Dummies Questions & Answers

problem with if -z string

someone please help me out here. i am not a newbie. i just haven't posted on this board in years. i'm talking at least two and a half years. My last user name was TRUEST. i couldn't log into this name because i forgot my password and my aol email address has long been deleted. anyway, i'm... (1 Reply)
Discussion started by: Terrible
1 Replies

9. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies

10. Shell Programming and Scripting

An Idea for Tokenizing

One of the monitoring tools in Java is called `jps`, and it monitors all Java processes that are run by the user, an example output would be like this: 3459 Jps 2348 test 2311 Util where the first column represents Process IDs and the second column represents Java processes names.... (8 Replies)
Discussion started by: neked
8 Replies
Login or Register to Ask a Question