Sample Header Ad - 728x90

Iterating over array elements with gnu parallel

3 votes
2 answers
690 views
I have an input file, *names.txt*, with the 1 word per line: apple abble aplle With my bash script I am trying to achieve the following output: apple and apple apple and abble apple and aplle abble and apple abble and abble abble and aplle aplle and apple aplle and abble aplle and aplle Here is my bash script
#!/usr/bin bash
readarray -t seqcol < names.txt

joiner () {
           val1=$1
           val2=$2
           echo "$val1 and $val2"
}

export -f joiner

parallel -j 20 '
	line=($(echo {}))
	for word in "${line[@]}"; do
    		joiner "${line}" "${word}"
	done
' ::: "${seqcol[@]}"
but it is only outputting the following 3 lines comparing identical elements from the array apple and apple abble and abble aplle and aplle I have the script that uses while read line loop, but it is too slow (my actual datafile is has about 200k lines). That is why I want to use array elements and gnu parallel at the same to speed the process up. I have tried different ways of accessing the array elements within the parallel ' ' command (by mainly modifying this loop - for word in "${line[@]}", or by supplying the array to parallel via printf '%s\n' "${seqcol[@]}") but they are either leading to errors or output blank lines. I would appreciate any help!
Asked by duda13 (33 rep)
Apr 28, 2023, 03:42 PM
Last activity: May 2, 2023, 07:37 AM