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`); 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 Code:
n=1; cat file | while read line; do [ $n -eq 2 ] && doSomething; ((n++)); done Code:
function help() { echo "Bash scripting guides: |
Quote:
file1 I love apple I love orange I love mango Code:
ARRAY=(`more file1`); What I got is 1 element. |
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 |
Quote:
New1.sh[4]: syntax error at line 4 : `(' unexpected ARRAY=(`more file1`); <-- this is the line that it prompt me with the error. |
Quote:
Code:
set -A array1 `more test_file` |
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. |