Skip to main content
added 193 characters in body
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60

Separating field selection/ordering (f=... below) from adding --argN (the loop) so that it's easy to modify fields and/or order and potentially use the same field multiple times as the OP said in a comment is required, using any awk and a POSIX xargs:

$ awk -F, -v f='2,1,3' '
    {
        n = split(f, flds)
        for (i = 1; i <= n; i++) {
            printf " --arg%d \"%s\"", i, $(flds[i])
        }
        print ""
    }
' file | xargs -L1 echo my_cmd
my_cmd --arg1 42 --arg2 foo --arg3 red
my_cmd --arg1 13 --arg2 bar --arg3 blue
my_cmd --arg1 27 --arg2 baz --arg3 green

Remove the echo when done testing.

Given that, changing the order and duplicating fields is as simple as changing f='...':

$ awk -F, -v f='3,1,3,2,1' '
    {
        n = split(f, flds)
        for (i = 1; i <= n; i++) {
            printf " --arg%d \"%s\"", i, $(flds[i])
        }
        print ""
    }
' file | xargs -L1 echo my_cmd
my_cmd --arg1 red --arg2 foo --arg3 red --arg4 42 --arg5 foo
my_cmd --arg1 blue --arg2 bar --arg3 blue --arg4 13 --arg5 bar
my_cmd --arg1 green --arg2 baz --arg3 green --arg4 27 --arg5 baz

The \"s around %s are to ensure xargs would handle fields that contain spaces correctly, otherwise a field like a b would be split into 2 separate arguments for my_cmd.

Separating field selection/ordering (f=... below) from adding --argN (the loop) so that it's easy to modify fields and/or order and potentially use the same field multiple times, using any awk and a POSIX xargs:

$ awk -F, -v f='2,1,3' '
    {
        n = split(f, flds)
        for (i = 1; i <= n; i++) {
            printf " --arg%d \"%s\"", i, $(flds[i])
        }
        print ""
    }
' file | xargs -L1 echo my_cmd
my_cmd --arg1 42 --arg2 foo --arg3 red
my_cmd --arg1 13 --arg2 bar --arg3 blue
my_cmd --arg1 27 --arg2 baz --arg3 green

Remove the echo when done testing.

Given that, changing the order and duplicating fields is as simple as changing f='...':

$ awk -F, -v f='3,1,3,2,1' '
    {
        n = split(f, flds)
        for (i = 1; i <= n; i++) {
            printf " --arg%d \"%s\"", i, $(flds[i])
        }
        print ""
    }
' file | xargs -L1 echo my_cmd
my_cmd --arg1 red --arg2 foo --arg3 red --arg4 42 --arg5 foo
my_cmd --arg1 blue --arg2 bar --arg3 blue --arg4 13 --arg5 bar
my_cmd --arg1 green --arg2 baz --arg3 green --arg4 27 --arg5 baz

The \"s around %s are to ensure xargs would handle fields that contain spaces correctly, otherwise a field like a b would be split into 2 separate arguments for my_cmd.

Separating field selection/ordering (f=... below) from adding --argN (the loop) so that it's easy to modify fields and/or order and potentially use the same field multiple times as the OP said in a comment is required, using any awk and a POSIX xargs:

$ awk -F, -v f='2,1,3' '
    {
        n = split(f, flds)
        for (i = 1; i <= n; i++) {
            printf " --arg%d \"%s\"", i, $(flds[i])
        }
        print ""
    }
' file | xargs -L1 echo my_cmd
my_cmd --arg1 42 --arg2 foo --arg3 red
my_cmd --arg1 13 --arg2 bar --arg3 blue
my_cmd --arg1 27 --arg2 baz --arg3 green

Remove the echo when done testing.

Given that, changing the order and duplicating fields is as simple as changing f='...':

$ awk -F, -v f='3,1,3,2,1' '
    {
        n = split(f, flds)
        for (i = 1; i <= n; i++) {
            printf " --arg%d \"%s\"", i, $(flds[i])
        }
        print ""
    }
' file | xargs -L1 echo my_cmd
my_cmd --arg1 red --arg2 foo --arg3 red --arg4 42 --arg5 foo
my_cmd --arg1 blue --arg2 bar --arg3 blue --arg4 13 --arg5 bar
my_cmd --arg1 green --arg2 baz --arg3 green --arg4 27 --arg5 baz

The \"s around %s are to ensure xargs would handle fields that contain spaces correctly, otherwise a field like a b would be split into 2 separate arguments for my_cmd.

added 124 characters in body
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60

Separating field selection/ordering (f=... below) from adding --argN (the loop) so that it's easy to modify fields and/or order and potentially use the same field multiple times, using any awk and a POSIX xargs:

$ awk -F, -v f='2,1,3' '
    {
        n = split(f, flds)
        for (i = 1; i <= n; i++) {
            printf " --arg%d \"%s\"", i, $(flds[i])
        }
        print ""
    }
' file | xargs -L1 echo my_cmd
my_cmd --arg1 42 --arg2 foo --arg3 red
my_cmd --arg1 13 --arg2 bar --arg3 blue
my_cmd --arg1 27 --arg2 baz --arg3 green

Remove the echo when done testing.

Given that, changing the order and duplicating fields is as simple as changing f='...':

$ awk -F, -v f='3,1,3,2,1' '
    {
        n = split(f, flds)
        for (i = 1; i <= n; i++) {
            printf " --arg%d \"%s\"", i, $(flds[i])
        }
        print ""
    }
' file | xargs -L1 echo my_cmd
my_cmd --arg1 red --arg2 foo --arg3 red --arg4 42 --arg5 foo
my_cmd --arg1 blue --arg2 bar --arg3 blue --arg4 13 --arg5 bar
my_cmd --arg1 green --arg2 baz --arg3 green --arg4 27 --arg5 baz

The \"s around %s are to ensure xargs would handle fields that contain spaces correctly, otherwise a field like a b would be split into 2 separate arguments for my_cmd.

Separating field selection/ordering (f=... below) from adding --argN (the loop) so that it's easy to modify fields and/or order and potentially use the same field multiple times, using any awk and a POSIX xargs:

$ awk -F, -v f='2,1,3' '
    {
        n = split(f, flds)
        for (i = 1; i <= n; i++) {
            printf " --arg%d \"%s\"", i, $(flds[i])
        }
        print ""
    }
' file | xargs -L1 echo my_cmd
my_cmd --arg1 42 --arg2 foo --arg3 red
my_cmd --arg1 13 --arg2 bar --arg3 blue
my_cmd --arg1 27 --arg2 baz --arg3 green

Given that, changing the order and duplicating fields is as simple as changing f='...':

$ awk -F, -v f='3,1,3,2,1' '
    {
        n = split(f, flds)
        for (i = 1; i <= n; i++) {
            printf " --arg%d \"%s\"", i, $(flds[i])
        }
        print ""
    }
' file | xargs -L1 echo my_cmd
my_cmd --arg1 red --arg2 foo --arg3 red --arg4 42 --arg5 foo
my_cmd --arg1 blue --arg2 bar --arg3 blue --arg4 13 --arg5 bar
my_cmd --arg1 green --arg2 baz --arg3 green --arg4 27 --arg5 baz

The \"s around %s are to ensure xargs would handle fields that contain spaces correctly.

Separating field selection/ordering (f=... below) from adding --argN (the loop) so that it's easy to modify fields and/or order and potentially use the same field multiple times, using any awk and a POSIX xargs:

$ awk -F, -v f='2,1,3' '
    {
        n = split(f, flds)
        for (i = 1; i <= n; i++) {
            printf " --arg%d \"%s\"", i, $(flds[i])
        }
        print ""
    }
' file | xargs -L1 echo my_cmd
my_cmd --arg1 42 --arg2 foo --arg3 red
my_cmd --arg1 13 --arg2 bar --arg3 blue
my_cmd --arg1 27 --arg2 baz --arg3 green

Remove the echo when done testing.

Given that, changing the order and duplicating fields is as simple as changing f='...':

$ awk -F, -v f='3,1,3,2,1' '
    {
        n = split(f, flds)
        for (i = 1; i <= n; i++) {
            printf " --arg%d \"%s\"", i, $(flds[i])
        }
        print ""
    }
' file | xargs -L1 echo my_cmd
my_cmd --arg1 red --arg2 foo --arg3 red --arg4 42 --arg5 foo
my_cmd --arg1 blue --arg2 bar --arg3 blue --arg4 13 --arg5 bar
my_cmd --arg1 green --arg2 baz --arg3 green --arg4 27 --arg5 baz

The \"s around %s are to ensure xargs would handle fields that contain spaces correctly, otherwise a field like a b would be split into 2 separate arguments for my_cmd.

added 101 characters in body
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60

Separating field selection/ordering (f=... below) from adding --argN (the loop) so that it's easy to modify fields and/or order and potentially use the same field multiple times, using any awk and a POSIX xargs:

$ awk -F, -v f='2,1,3' '
    {
        n = split(f, flds)
        for (i = 1; i <= n; i++) {
            printf " --arg%d %s"\"%s\"", i, $(flds[i])
        }
        print ""
    }
' file | xargs -L1 echo my_cmd
my_cmd --arg1 42 --arg2 foo --arg3 red
my_cmd --arg1 13 --arg2 bar --arg3 blue
my_cmd --arg1 27 --arg2 baz --arg3 green

Given that, changing the order and duplicating fields is as simple as changing f='...':

$ awk -F, -v f='3,1,3,2,1' '
    {
        n = split(f, flds)
        for (i = 1; i <= n; i++) {
            printf " --arg%d %s"\"%s\"", i, $(flds[i])
        }
        print ""
    }
' file | xargs -L1 echo my_cmd
my_cmd --arg1 red --arg2 foo --arg3 red --arg4 42 --arg5 foo
my_cmd --arg1 blue --arg2 bar --arg3 blue --arg4 13 --arg5 bar
my_cmd --arg1 green --arg2 baz --arg3 green --arg4 27 --arg5 baz

The \"s around %s are to ensure xargs would handle fields that contain spaces correctly.

Separating field selection/ordering (f=... below) from adding --argN (the loop) so that it's easy to modify fields and/or order and potentially use the same field multiple times, using any awk and a POSIX xargs:

$ awk -F, -v f='2,1,3' '
    {
        n = split(f, flds)
        for (i = 1; i <= n; i++) {
            printf " --arg%d %s", i, $(flds[i])
        }
        print ""
    }
' file | xargs -L1 echo my_cmd
my_cmd --arg1 42 --arg2 foo --arg3 red
my_cmd --arg1 13 --arg2 bar --arg3 blue
my_cmd --arg1 27 --arg2 baz --arg3 green

Given that, changing the order and duplicating fields is as simple as changing f='...':

$ awk -F, -v f='3,1,3,2,1' '
    {
        n = split(f, flds)
        for (i = 1; i <= n; i++) {
            printf " --arg%d %s", i, $(flds[i])
        }
        print ""
    }
' file | xargs -L1 echo my_cmd
my_cmd --arg1 red --arg2 foo --arg3 red --arg4 42 --arg5 foo
my_cmd --arg1 blue --arg2 bar --arg3 blue --arg4 13 --arg5 bar
my_cmd --arg1 green --arg2 baz --arg3 green --arg4 27 --arg5 baz

Separating field selection/ordering (f=... below) from adding --argN (the loop) so that it's easy to modify fields and/or order and potentially use the same field multiple times, using any awk and a POSIX xargs:

$ awk -F, -v f='2,1,3' '
    {
        n = split(f, flds)
        for (i = 1; i <= n; i++) {
            printf " --arg%d \"%s\"", i, $(flds[i])
        }
        print ""
    }
' file | xargs -L1 echo my_cmd
my_cmd --arg1 42 --arg2 foo --arg3 red
my_cmd --arg1 13 --arg2 bar --arg3 blue
my_cmd --arg1 27 --arg2 baz --arg3 green

Given that, changing the order and duplicating fields is as simple as changing f='...':

$ awk -F, -v f='3,1,3,2,1' '
    {
        n = split(f, flds)
        for (i = 1; i <= n; i++) {
            printf " --arg%d \"%s\"", i, $(flds[i])
        }
        print ""
    }
' file | xargs -L1 echo my_cmd
my_cmd --arg1 red --arg2 foo --arg3 red --arg4 42 --arg5 foo
my_cmd --arg1 blue --arg2 bar --arg3 blue --arg4 13 --arg5 bar
my_cmd --arg1 green --arg2 baz --arg3 green --arg4 27 --arg5 baz

The \"s around %s are to ensure xargs would handle fields that contain spaces correctly.

added 34 characters in body
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60
Loading
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60
Loading