LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-10-2008, 10:32 AM   #1
tugce_zehra
LQ Newbie
 
Registered: Dec 2008
Posts: 5

Rep: Reputation: 0
Exclamation How To Assign an Appropriate Seperator


Hi everybody;
I have made a little program in bash.It's simple table evaluator program.
This table contains three columns: "name", "country" and "year".
here are the example input:

BMW Germany 1995
Ferrari Italy 2000
Citroen France 2003
Honda Japan 2005

I had used the whitespace character in order to seperate fields as you see.But this appoaches leads some problem, so I had change it with "|".

But later I had encounter another problem.I must get word by word one line, after this change I can not get word by word.This line:

for WORD in `cat $TABLE_NAME`
do

returns whole line ,say "BMW|Germany|1995". I tried changing string seperator by using IFS=| but it doesn't work.

Is there any suggestions for solve that problem, how can I get one word from a line even every word seperated by "|"?

Last edited by tugce_zehra; 12-10-2008 at 10:36 AM. Reason: Recover Problem
 
Old 12-10-2008, 01:39 PM   #2
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
Code:
$ cat cars.db
BMW|Germany|1995
Ferrari Italy 2000
Citroen France 2003
Honda Japan 2005
Ford    USA 2001
Code:
$ cat cars.sh
#!/usr/bin/env bash

OLDIFS="$IFS"
IFS=$'\x20'$'\x09'$'\x0A'$'\x0D'$'\x7C'

parse_entry() {
    for ENTRY in "$*"
    do
        echo $ENTRY
    done
}

for LINE in $(cat cars.db)
do
    parse_entry $LINE
done

IFS="$OLDIFS"
Code:
$ ./cars.sh
BMW
Germany
1995
Ferrari
Italy
2000
Citroen
France
2003
Honda
Japan
2005
Ford
USA
2001

Last edited by jcookeman; 12-10-2008 at 01:47 PM. Reason: IFS fubar
 
Old 12-10-2008, 01:47 PM   #3
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
To define an arbitrary separator you do need to change the IFS variable:
Code:
#!/bin/bash
# save the original IFS so we can reset it
OLD_IFS=$IFS
# the original IFS is equal to [space][TAB][EOL]
# so use the original IFS to go over each line:
for LINE in $(cat $TABLE_NAME) ;do
 # then change the separator to your in-the-line separator
 IFS=|
 for ITEM in $(echo $LINE) ; do
  
 done
 # now change the separator back to retrieve the next line
 IFS=$OLD_IFS
done
However, you should probably choose some other separator besides the 'pipe' symbol as bash will try to treat that as a reserved word. Avoid the '|', ':', '<' and '>' at least. You may not always be able to use the same separator -for instance you can create CSV entries by using the ',', but that may be part of some of your entries. It's bets to choose some character you can be sure will not be a part of your data fields, like: '§', for example.
 
Old 12-10-2008, 02:00 PM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
A tab makes a good separator. The data can be printed with the fields aligned. CSV files will place quotes around text fields. Handling regularly structured text databases is what awk was created for. You might want to learn about using AWK.

The source package for gawk contains a book called "GAWK: Effective Awk Programming". It is what is used to produce the info manual. However if you use the source package and produce a print worthy version with the "make pdf" or "make ps" or "make dvi" targets, the results are excellent.

You would use ./configure && make && make install to produce the binary package from source. Pdf & ps & dvi are other targets besides the install target that normally don't get executed. Two other packages that have excellent manuals are coreutils and bash (bashref.ps). The bashref manual is usually supplied with the bash package and you probably have print worthy versions already in /usr/share/doc, or from a separate bashref package.

Good Luck!
 
Old 12-10-2008, 03:16 PM   #5
tugce_zehra
LQ Newbie
 
Registered: Dec 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks for the replies.I will check them out.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I am getting this error "Missing Seperator" when I try make. NewToLinux2005 Linux - Software 5 08-11-2011 08:06 PM
is there any way to use a word for seperator qrshat Solaris / OpenSolaris 11 02-13-2006 04:13 PM
Re: Missing Seperator Error? dt23 Mandriva 1 11-30-2004 11:18 PM
KDE command seperator a_priebe47 Slackware 1 08-06-2004 07:48 PM
Firefox Bookmark Manager - What are bookmark seperator names for? SBing Linux - Software 2 06-21-2004 02:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration