LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 11-16-2006, 07: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, 09:10 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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-16-2006, 11:56 PM   #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, 12:39 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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, 01: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, 02: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, 03:27 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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 03:28 AM.
 
  


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

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

All times are GMT -5. The time now is 01:47 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