LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 11-16-2006, 08:50 PM   #1
WeiSomething
LQ Newbie
 
Registered: Nov 2006
Posts: 10

Rep: Reputation: 0
How to extract data and for loop it into an array? (shell)


Hi everyone!


How do I do a split in UNIX? Like what a Java can do where it read in a file and split the data with the delimiter the user specified and put all the data it split into an array.


And how do I read a specific line in a file?

For example:

Data1

I love apple
I love orange
I love mango

How do i extract the second line "I love orange" out?
 
Old 11-16-2006, 10:10 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,693
Blog Entries: 54

Rep: Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961Reputation: 2961
How do I do a split in UNIX? Like what a Java can do where it read in a file and split the data with the delimiter the user specified and put all the data it split into an array.
#Watch your IFS (delimiters)
Code:
ARRAY=(`output something`); 
echo ARRAY has ${#ARRAY[@]} elements.

And how do I read a specific line in a file?
Code:
cat file | while read line; do case "${line}" in *orange) doSomething;; esac; done
or
Code:
n=1; cat file | while read line; do [ $n -eq 2 ] && doSomething; ((n++)); done

Code:
function help() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html 
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html 
http://www.tldp.org/LDP/abs/html/"; }
 
Old 11-17-2006, 12:56 AM   #3
WeiSomething
LQ Newbie
 
Registered: Nov 2006
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn
How do I do a split in UNIX? Like what a Java can do where it read in a file and split the data with the delimiter the user specified and put all the data it split into an array.
#Watch your IFS (delimiters)
Code:
ARRAY=(`output something`); 
echo ARRAY has ${#ARRAY[@]} elements.
How do I set my IFS (delimiters) to an empty space?

file1
I love apple
I love orange
I love mango

Code:
ARRAY=(`more file1`); 
echo ARRAY has ${#ARRAY[@]} elements.
Does this print out "ARRAY has 9 elements."?

What I got is 1 element.
 
Old 11-17-2006, 01:39 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,004
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
IFS='<something>'
It's a space by default, hence 9 elements. If you want one line = one element,
use IFS="\n"... if you see 1 element there's something seriously odd with your
system ...


Cheers,
Tink
 
Old 11-17-2006, 02:41 AM   #5
WeiSomething
LQ Newbie
 
Registered: Nov 2006
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Tinkster
IFS='<something>'
It's a space by default, hence 9 elements. If you want one line = one element,
use IFS="\n"... if you see 1 element there's something seriously odd with your
system ...


Cheers,
Tink
hi. I know why I got only 1 element. Because I did not put any bracket around it. But after I put, it gave mi error like:

New1.sh[4]: syntax error at line 4 : `(' unexpected

ARRAY=(`more file1`); <-- this is the line that it prompt me with the error.
 
Old 11-17-2006, 03:45 AM   #6
WeiSomething
LQ Newbie
 
Registered: Nov 2006
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by WeiSomething
hi. I know why I got only 1 element. Because I did not put any bracket around it. But after I put, it gave mi error like:

New1.sh[4]: syntax error at line 4 : `(' unexpected

ARRAY=(`more file1`); <-- this is the line that it prompt me with the error.
I found out that the method is not correct in fact it should be:

Code:
set -A array1 `more test_file`
But now i had problem with the LFS because in my file there is space and new line. How to define 2 delimiters?
 
Old 11-17-2006, 04:27 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
you can't have 2 delimiters. (not in ksh, which it looks like)

If you want to ignore blank lines
you can set -A array1 `grep . test_file`

you probably shouldn't really use 'more' as it's designed for interactive use
use 'cat' instead.

Last edited by bigearsbilly; 11-17-2006 at 04:28 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
BASH: Reading long filenames into an array using a loop DaneM Programming 12 09-11-2009 08:24 AM
Do/while loop and if statements not functioning properly w/ array... ohfaney Programming 5 05-02-2006 03:22 AM
LXer: Extract data from the Internet with Web scraping LXer Syndicated Linux News 0 03-29-2006 01:55 PM
Extract data ust Linux - General 1 10-23-2003 06:45 AM
Extract data from disk, without partition table... mandatory Linux - General 1 10-20-2003 07:35 AM


All times are GMT -5. The time now is 04:29 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration