LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to extract data and for loop it into an array? (shell) (https://www.linuxquestions.org/questions/programming-9/how-to-extract-data-and-for-loop-it-into-an-array-shell-502357/)

WeiSomething 11-16-2006 07:50 PM

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?

unSpawn 11-16-2006 09:10 PM

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/"; }


WeiSomething 11-16-2006 11:56 PM

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.

Tinkster 11-17-2006 12:39 AM

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

WeiSomething 11-17-2006 01:41 AM

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.

WeiSomething 11-17-2006 02:45 AM

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?

bigearsbilly 11-17-2006 03:27 AM

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.


All times are GMT -5. The time now is 07:49 PM.