Problem in tokenizing the string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem in tokenizing the string
# 8  
Old 04-28-2012
# will remove string from beginning. % from the end. Double them, ## or %% to strip longest match..

So if your spaces are optional you need more work. If spaces won't be there, remove them from the right side.

Look at "Parameter Expansion" in shell manual
# 9  
Old 04-28-2012
Hi.

Here is an amended script that follows the idea of using built-in read to parse the tokens. The difference is that spaces are deleted, then characters that separate the tokens are converted to underlines, "_", and then that is used as the separator.

Here are two ways of doing that, one with sed, the other with parameter expansion:
Code:
#!/usr/bin/env bash

# @(#) s2	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 sed

line="DNS = cn=user1,o=company : pwd"
pl " Line to be parsed into variables:"
pe "$line"
line=$( echo "$line" | sed -e 's/ //g' -e 's/=/_/' -e 's/:/_/')
pe " Line prepared by sed:"
pe "$line"

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

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

unset s u p
pl " Sequence of \"Parameter Expansions\":"
line="DNS = cn=user1,o=company : pwd"
pe "$line"
line=${line// /}
pe "$line"
line=${line/=/_}
pe "$line"
line=${line/:/_}
pe "$line"
IFS="_" read s u p junk <<< "$line"
pl " Results of setting variables from expansion, read:"
pe "service name: $s"
pe "UserName: $u"
pe "Password: $p"

IFS="$oldifs"

exit 0

producing:
Code:
% ./s2

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
sed GNU sed version 4.1.5

-----
 Line to be parsed into variables:
DNS = cn=user1,o=company : pwd
 Line prepared by sed:
DNS_cn=user1,o=company_pwd

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

-----
 Sequence of "Parameter Expansions":
DNS = cn=user1,o=company : pwd
DNS=cn=user1,o=company:pwd
DNS_cn=user1,o=company:pwd
DNS_cn=user1,o=company_pwd

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

Best wishes ... cheers, drl
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