ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
$ 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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.