0

I have a properties file and wanted to read line by line and split each line into a array. I referenced Split string into an array in Bash and the awk statement looks complicated. In the same time I wanted to follow suggestion provided in the link. When I try the command in bash shell:

export $line1="table1;/users/user_name/dir1/dir2;/users/user_name/dirA/dirB" 
readarray -td; a <<<"$line1,"; unset 'a[-1]'; declare -p a;

throws error.

bash: readarray: -d: invalid option
readarray: usage: readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]

Couldn't under the below awk statements:

array=()
while read -r -d $'\0' each; do   # use a NUL terminated field separator 
    array+=("$each")
done < <(printf "%s" "$str" | awk '{ gsub(/,[ ]+|$/,"\0"); print }')
declare -p array

to translate to my requirement.

This is how I read the file:

while read record_line; do
    if [ ! -z "$record_line" -a "$record_line" != " " ]; then
    readarray -td; a <<<"$record_line,"; unset 'a[-1]'; declare -p a;
    fi
done<${PPROPERTIES_FILE} 

Any help. Thanks.

Aavik
  • 967
  • 19
  • 48
  • Why does it say "ksh" in your title? – melpomene Apr 16 '18 at 15:53
  • program runs as !/bin/ksh – Aavik Apr 16 '18 at 15:54
  • Then why are you linking to a bash solution and why does your error say `bash:`? – melpomene Apr 16 '18 at 15:55
  • I was trying in both ksh and bash – Aavik Apr 16 '18 at 15:57
  • Which one are you actually asking about, bash or ksh? – melpomene Apr 16 '18 at 15:58
  • I am looking for ksh please – Aavik Apr 16 '18 at 16:06
  • `export $line1=` should, to start with, just be `line1=`. No `$`, no `export`. (The `export` is mostly -- but not entirely -- harmless, the `$` actually breaks your code). To explain how `export` is harmful -- environment variables live in the same reserved memory space as command line arguments, so the more you export, the shortest your maximum command line length can be; thus, you shouldn't export variables unless you really need them in the environment, rather than just defined in regular heap memory. – Charles Duffy Apr 16 '18 at 16:06
  • 1
    Also -- **which** version of ksh are you using? The differences between ksh88 and ksh93 are huge; the differences between mksh or pdksh and *real* David Korn ksh, even moreso. – Charles Duffy Apr 16 '18 at 16:08
  • version sh (AT&T Research) 93u+ 2012-08-01 – Aavik Apr 16 '18 at 16:15
  • Oh, good; that's easy. Just to be clear -- what's your desired result? (Enough of a [mcve] to let someone test that their answer is correct in light of what you actually want to accomplish would be ideal). – Charles Duffy Apr 16 '18 at 16:19
  • Read 1st line, split into array; process 1st element of array(call to a function create table); process rest of the elements of array(call to a function to create directories); Read the next line – Aavik Apr 16 '18 at 16:22

1 Answers1

1

Consider the following

#!/usr/bin/env ksh
line=0
while IFS=';' read -rA pieces; do
  line=$(( line + 1 ))
  unset "pieces[0]"    # ignore first
  echo "Found pieces on line $line:"
  printf ' - %s\n' "${all_pieces[@]}"
done <in.txt

Note that unlike in bash, reading to an array in ksh is done with -A, not -a.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Googling says IFS is not a safe option – Aavik Apr 16 '18 at 16:24
  • 1
    "Googling" is wrong. `IFS` is entirely safe in this context, where it's set only for the duration of the `read` command. – Charles Duffy Apr 16 '18 at 16:24
  • BTW, if you want to find authoritative information about shell programming, two (bash-focused) resources I can recommend are [the Bash Hackers' Wiki](http://wiki.bash-hackers.org/) and [the Wooledge BashGuide](http://mywiki.wooledge.org/BashGuide). There's a lot of junk out there, and Google tends to have all of it indexed. (TLDP's ABS is particularly notable as a resource to avoid). – Charles Duffy Apr 16 '18 at 16:28
  • ...to be clear, it's not safe to use `IFS` to try to allow unquoted expansion (`$foo` instead of `"$foo"`), as doing so fails to suppress glob expansion: `s='*.txt'; echo "$s"` will echo exactly `*.txt`, whereas if any files matching the glob exist, `s='*.txt'; IFS=; echo $s` will name those files -- presumably, that's what the resource you found was trying to get at. (Similar issues apply for `IFS=';'; array=( $s )`, being likewise an unquoted expansion). – Charles Duffy Apr 16 '18 at 16:55