How to extract data and for loop it into an array? (shell)
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.
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?
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
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.
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 ...
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.