INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

loop through file - parse lines based on field delimiter

loop through file - parse lines based on field delimiter

loop through file - parse lines based on field delimiter

(OP)
I have this file:

CODE

master::XXX887YY::ACCOUNT NAME ONE:some.domain.com::eof
::ABC123ABC::ACCOUNT NAME TWO:some.domain.com::eof
master::SXYZZ823::ACCOUNT NAME THREE:some.domain.com::eof
::CCD0932YFD::ACCOUNT NAME FOUR:some.domain.com::eof
foo:bar:foobar:foo:ACCOUNT NAME FIVE:some.domain.com:some.domain.com:eof
test:test:testbar:testing:TEST NAME:test.domain.com:test.domain.com:eof 

I can be of any number o lines but same number of columns. I need to
1. Read through each line
2. explode the lines based on field delimiter

Notice that the field delimiter is :

How can I do this?

Thank you all for your assistance!


--
SouthBeach
http://www.fpgroups.com
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.

RE: loop through file - parse lines based on field delimiter

I find this is the easiest for something like that...

CODE

#!/bin/ksh

# Set delimiter (IFS = Inter Field Separator)
IFS=:

while read FIELD1 FIELD2 FIELD3 FIELD4 FIELD5 REMAINDER
do
# Each field in the file has been assigned the variable names after the "read" print "FIELD1 = ${FIELD1}" print "FIELD2 = ${FIELD2}" print "FIELD3 = ${FIELD3}" print "FIELD4 = ${FIELD4}" print "FIELD5 = ${FIELD5}" print
done

Apologies for any typos, I didn't test it. bigsmile

RE: loop through file - parse lines based on field delimiter


Or you can check out the script I posted in the article: Quick and Dirty Unix CSV File Browser
3eyes

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

RE: loop through file - parse lines based on field delimiter

Oops, I did have a code error. The final done should be like this...

CODE

#!/bin/ksh

# Set delimiter (IFS = Inter Field Separator)
IFS=:

while read FIELD1 FIELD2 FIELD3 FIELD4 FIELD5 REMAINDER
do
# Each field in the file has been assigned the variable names after the "read" print "FIELD1 = ${FIELD1}" print "FIELD2 = ${FIELD2}" print "FIELD3 = ${FIELD3}" print "FIELD4 = ${FIELD4}" print "FIELD5 = ${FIELD5}" print
done < file.txt


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close