Kevin
Kevin

Reputation: 794

Separate arguments with a custom separator in GNU Parallel

I would like to separate the arguments in GNU parallel using a custom separator.

For example:

$ seq 19 30  | parallel -n 4 --dry-run 'my-command {}'
my-command 19 20 21 22
my-command 23 24 25 26
my-command 27 28 29 30

What I want to achieve:

$ seq 19 30  | parallel --MAGIC-OPTION ',' -n 4 --dry-run 'my-command {}'
my-command 19,20,21,22
my-command 23,24,25,26
my-command 27,28,29,30

Does such a --MAGIC-OPTION exist? I did not find it in the man pages.

Upvotes: 1

Views: 50

Answers (1)

Ole Tange
Ole Tange

Reputation: 33740

There is no such option. If you only have 4 args, you can do:

$ seq 19 30  | parallel -n 4 --dry-run 'my-command {1},{2},{3},{4}'

Otherwise, you will have to do something like:

seq 19 30  | parallel -n 4 --dry-run my-command '{=-2 shift @arg;$_=join",",@arg =}'

or:

seq 19 30  | parallel --rpl '{,} -2 shift @arg;$_=join",",@arg' -n 4 --dry-run my-command '{,}'

or:

function run_on_args {
  # Define your own Output Field Separator
  local OFS=","
  
  # Initialize a variable to hold the concatenated arguments
  local args_str=""
  
  # Loop through all passed arguments
  for arg in "$@"; do
    # Check if args_str is empty to avoid adding the separator at the beginning
    if [[ -z $args_str ]]; then
      args_str="$arg"
    else
      args_str="$args_str$OFS$arg"
    fi
  done


  # Echo the concatenated arguments
  echo $args_str
}

Upvotes: 1

Related Questions