An Idea for Tokenizing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting An Idea for Tokenizing
# 8  
Old 06-07-2005
Thanks bakunin and reborg for your ideas.

Bakunin's script captures the idea I originally intended, but it fails it work, I don't know why. Specifically, this line does not work properly:

Code:
print - "$line" | read word, line

I don't know if this is because of difference in systems (I am using Fedora Core 3), but I don't think this should be an issue, since we are using ksh, which is common among all Linux distributions and UNIX flavors.

On command shell I tried to run the following command, but it would not work as well:

Code:
echo 2224 test | read word line

when I echoed $word and $line they would be empty variables. Therefore, the script bakunin wrote would result in an infinite loop, although $line is read properly by the first 'read line' statement, piping doesn't seem to work. Furthermore, if it had worked I think the output would have been:

2224
test

rather than just:

2224

this is because $line becomes 'test' after the first run on the while loop, which would pass the inner loop condition again, causing $word this time to become 'test' and printing it. This is of course a simple programming problem, and can be fixed easily:

Code:
tempVar = ""
while [ -z "$tempVar" ] ; do
print - "$line" | read word, tempVar
line = ""
print "$word"
done

Any suggestions regarding the piping problem?
# 9  
Old 06-08-2005
Sorry, I had a typo in my solution. You have already found the offending line, the comma is the error. It should read:

Code:
print - "$line" | read word line

and, when you call the whole script "tokenize.ksh", you could use it like:

Code:
cat anyfile | tokenize.ksh | some_script_using_tokens.ksh

If you meant by "tokenizing" that you want only the first word of each line going to the output (sorry, but that wasn't clear from the beginning, at least not for me), replace the while...done-loop by a sinqle

Code:
print - $line | read word junk
print - "$word"

The way the "read"-statement works is: put the first word encountered to the first variable, the second word to the second variable, etc. If you run out of variables put the rest of the line to the last variable. This means "read line" will read the whole line, regardless of how many word are in it, "read word line" will put the first word to $word, everything else to $line.

Since you mentioned that "echo 111 222 | read word line" doesn't work either I suppose your Kornshell implementation is as faulty as was my script. On my system (various AIX 5.x versions) it works perfectly.

bakunin

Last edited by bakunin; 06-08-2005 at 04:02 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I have no idea how to solve this :/

I am trying to make a script that will execute different things by the input Input: #/bin/bash echo "Test script" read cake if then echo "cake it is" fi Output: 1 : bad variable nameake test.sh: 8: test.sh: Syntax error: end of file unexpected (expecting "then") (0 Replies)
Discussion started by: kitay
0 Replies

2. Shell Programming and Scripting

Problem in tokenizing the string

Supposed I have a string in the format: <service_name> = <ldap user FDN> : <password> like DNS = cn=user1,o=company : pwd I want to tokenize like: service name:DNS UserName: cn=user1,o=company Password: pwd. Because of the '=' sign between Service Name and LDAP Name I not... (8 Replies)
Discussion started by: saurabhkoar
8 Replies

3. Shell Programming and Scripting

need a new idea for a script

Hi all, I now have project in UNIX Solaris and I want to have some new ideas to execute it, so I hope you help me finding new ideas in scripting or some infrastructure .bye (1 Reply)
Discussion started by: hard_revenge
1 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

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

6. Shell Programming and Scripting

help... no idea what to use

my issue now is i have a txt file containing a list like below i want to create a script that will add a constant text "Find this name" at the start and "at your directory" at the end. every line should be added by phrase at the start and end. Each line of the file should look like "Find... (4 Replies)
Discussion started by: dakid
4 Replies

7. Shell Programming and Scripting

Errors-- Any Idea

Getting some errors when i run this script.. ./xml_load_process.txt: -jar: not found ./xml_load_process.txt: syntax error at line 31 : `then' unmatched any ideas...really miffed at my inability to spot the error.... #!/bin/ksh # set -o xtrace # set environment variables export... (3 Replies)
Discussion started by: jazz21
3 Replies

8. Shell Programming and Scripting

b-shell: any better idea for this one?Thanks!!

I'm new to the script programming and I have this piece of code (repeatedly used) in my program: while : do ................ ans=`ckyorn -p "Do you want to continue?"` if || || || ; then break elif || ; then echo "Aborting..." exit... (2 Replies)
Discussion started by: bluemoon1
2 Replies

9. Shell Programming and Scripting

Here is an interesting idea...

Does anyone know how to test if an ethernet interface is alive, or accepting connections? Here is the scenario - I have rsc and sc console interfaces on some Suns. There are some sporadic vulnerability scans that send them out to lunch when they run the scans. I have to login to the host and reset... (0 Replies)
Discussion started by: lm_admin_dh
0 Replies

10. Shell Programming and Scripting

Limitations of awk? Good idea? Bad idea?

Keeping in mind that I'm relatively comfortable with programming in general but very new to unix and korn/bourne shell scripts.. I'm using awk on a CSV file, and then performing calculations and operations on specific fields within specific records. The CSV file I'm working with has about 600... (2 Replies)
Discussion started by: yongho
2 Replies
Login or Register to Ask a Question