Fixed last argument with xargs
up vote
2
down vote
favorite
Is it possible to use xargs to invoke a command so that the last argument of the command is fixed?
My attempt:
printf '%sn' a b c d | xargs -I{} echo {} LAST
ends up doing
echo a LAST
echo b LAST
echo c LAST
echo d LAST
I want for xargs to invoke
echo a b c d LAST
#fit as many as you can but always finish wiht LAST
Is this possible to do, preferably in a portable way?
xargs
|
show 2 more comments
up vote
2
down vote
favorite
Is it possible to use xargs to invoke a command so that the last argument of the command is fixed?
My attempt:
printf '%sn' a b c d | xargs -I{} echo {} LAST
ends up doing
echo a LAST
echo b LAST
echo c LAST
echo d LAST
I want for xargs to invoke
echo a b c d LAST
#fit as many as you can but always finish wiht LAST
Is this possible to do, preferably in a portable way?
xargs
1
not as far as I known but at leastcp
,mv
andln
commands havs a-t|--target-directory
parameter that replace the last arg...
– xenoid
Sep 1 at 13:29
1
Would you please shed more light on what you want. i mean revise the question and add more clarifications. In the current shape it is not clear!
– user88036
Sep 1 at 13:51
1
Also, the output of your command is empty lines.
– user88036
Sep 1 at 13:53
1
Your command still doesn't work! "xargs: LAST: No such file or directory"
– user88036
Sep 1 at 14:00
1
Yes! it didn't work . I use Centos 7
– user88036
Sep 1 at 14:09
|
show 2 more comments
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Is it possible to use xargs to invoke a command so that the last argument of the command is fixed?
My attempt:
printf '%sn' a b c d | xargs -I{} echo {} LAST
ends up doing
echo a LAST
echo b LAST
echo c LAST
echo d LAST
I want for xargs to invoke
echo a b c d LAST
#fit as many as you can but always finish wiht LAST
Is this possible to do, preferably in a portable way?
xargs
Is it possible to use xargs to invoke a command so that the last argument of the command is fixed?
My attempt:
printf '%sn' a b c d | xargs -I{} echo {} LAST
ends up doing
echo a LAST
echo b LAST
echo c LAST
echo d LAST
I want for xargs to invoke
echo a b c d LAST
#fit as many as you can but always finish wiht LAST
Is this possible to do, preferably in a portable way?
xargs
xargs
edited Sep 1 at 13:55
asked Sep 1 at 13:21
PSkocik
17.6k44993
17.6k44993
1
not as far as I known but at leastcp
,mv
andln
commands havs a-t|--target-directory
parameter that replace the last arg...
– xenoid
Sep 1 at 13:29
1
Would you please shed more light on what you want. i mean revise the question and add more clarifications. In the current shape it is not clear!
– user88036
Sep 1 at 13:51
1
Also, the output of your command is empty lines.
– user88036
Sep 1 at 13:53
1
Your command still doesn't work! "xargs: LAST: No such file or directory"
– user88036
Sep 1 at 14:00
1
Yes! it didn't work . I use Centos 7
– user88036
Sep 1 at 14:09
|
show 2 more comments
1
not as far as I known but at leastcp
,mv
andln
commands havs a-t|--target-directory
parameter that replace the last arg...
– xenoid
Sep 1 at 13:29
1
Would you please shed more light on what you want. i mean revise the question and add more clarifications. In the current shape it is not clear!
– user88036
Sep 1 at 13:51
1
Also, the output of your command is empty lines.
– user88036
Sep 1 at 13:53
1
Your command still doesn't work! "xargs: LAST: No such file or directory"
– user88036
Sep 1 at 14:00
1
Yes! it didn't work . I use Centos 7
– user88036
Sep 1 at 14:09
1
1
not as far as I known but at least
cp
, mv
and ln
commands havs a -t|--target-directory
parameter that replace the last arg...– xenoid
Sep 1 at 13:29
not as far as I known but at least
cp
, mv
and ln
commands havs a -t|--target-directory
parameter that replace the last arg...– xenoid
Sep 1 at 13:29
1
1
Would you please shed more light on what you want. i mean revise the question and add more clarifications. In the current shape it is not clear!
– user88036
Sep 1 at 13:51
Would you please shed more light on what you want. i mean revise the question and add more clarifications. In the current shape it is not clear!
– user88036
Sep 1 at 13:51
1
1
Also, the output of your command is empty lines.
– user88036
Sep 1 at 13:53
Also, the output of your command is empty lines.
– user88036
Sep 1 at 13:53
1
1
Your command still doesn't work! "xargs: LAST: No such file or directory"
– user88036
Sep 1 at 14:00
Your command still doesn't work! "xargs: LAST: No such file or directory"
– user88036
Sep 1 at 14:00
1
1
Yes! it didn't work . I use Centos 7
– user88036
Sep 1 at 14:09
Yes! it didn't work . I use Centos 7
– user88036
Sep 1 at 14:09
|
show 2 more comments
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
tl;dr; this is how you could do it portably, without -I and other broken fancy options:
$ echo a b c d f g | xargs -n 2 sh -c 'echo "$@" LAST' sh
a b LAST
c d LAST
f g LAST
$ seq 1 100000 | xargs sh -c 'echo "$#" LAST' sh
23692 LAST
21841 LAST
21841 LAST
21841 LAST
10785 LAST
The problem with the -I option is that it's broken by design, and there is no way around it:
$ echo a b c d f g | xargs -I {} -n 1 echo {} LAST
a b c d f g LAST
$ echo a b c d f g | xargs -I {} -n 2 echo {} LAST
{} LAST a b
{} LAST c d
{} LAST f g
But they're probably covered, because that's what the standard says:
-I replstr
^[XSI] [Option Start] Insert mode: utility is executed for each
line from standard input, taking the entire line as a single
argument, inserting it in arguments for each occurrence of
replstr.
And it doesn't say anything about the interaction with the -n and -d options, so they're free to do whatever they please.
This is how it is on an (older) FreeBSD, less unexpected but non-standard:
fzu$ echo a b c d f g | xargs -I {} -n 2 echo {} LAST
a b LAST
c d LAST
f g LAST
fzu$ echo a b c d f g | xargs -I {} -n 1 echo {} LAST
a LAST
b LAST
c LAST
d LAST
f LAST
g LAST
2
I can't help thinking there could be a more neutral way of phrasing this answer. It also feels odd to point out one OS in particular (Linux), when you seem to be of the opinion that it's the behaviour required by the standard that's at fault here.
– ilkkachu
Sep 1 at 15:34
add a comment |
up vote
-2
down vote
Not with xargs
(alone). If you have an item list of unpredictable length, how should xargs
know from the beginning (= first element) which element would be the last one?
You'll need some additional logics around it to separate the desired element from the others.
@ilkkachu: Then, please show how from the lista b c d LAST
to get to four parameter listsa LAST
,b LAST
,c LAST
, andd LAST
by usingxargs
alone without the constantLAST
being supplied by other means (as wrongly done above). If that is what the requestor wanted.
– RudiC
Sep 1 at 21:23
I thought they wanted to go froma b c d
toa b ... LAST
, or such, with more than one ofa
,b
, etc used byxargs
for each command invocation. (one is easy, that's what-I
does.) They did specify theLAST
part in thexargs
command (not in theprintf
feedingxargs
), it's just that it doesn't work like that with-I
(but does in FreeBSDxargs -J
). But yeah, you're right in that I misread your intent, though technicallyxargs
could read the whole input before processing, but that wouldn't be very efficient.
– ilkkachu
Sep 1 at 22:29
Now that I reread the original request, I admit I may have misread it, so the base of my approach is gone...
– RudiC
Sep 1 at 22:37
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
tl;dr; this is how you could do it portably, without -I and other broken fancy options:
$ echo a b c d f g | xargs -n 2 sh -c 'echo "$@" LAST' sh
a b LAST
c d LAST
f g LAST
$ seq 1 100000 | xargs sh -c 'echo "$#" LAST' sh
23692 LAST
21841 LAST
21841 LAST
21841 LAST
10785 LAST
The problem with the -I option is that it's broken by design, and there is no way around it:
$ echo a b c d f g | xargs -I {} -n 1 echo {} LAST
a b c d f g LAST
$ echo a b c d f g | xargs -I {} -n 2 echo {} LAST
{} LAST a b
{} LAST c d
{} LAST f g
But they're probably covered, because that's what the standard says:
-I replstr
^[XSI] [Option Start] Insert mode: utility is executed for each
line from standard input, taking the entire line as a single
argument, inserting it in arguments for each occurrence of
replstr.
And it doesn't say anything about the interaction with the -n and -d options, so they're free to do whatever they please.
This is how it is on an (older) FreeBSD, less unexpected but non-standard:
fzu$ echo a b c d f g | xargs -I {} -n 2 echo {} LAST
a b LAST
c d LAST
f g LAST
fzu$ echo a b c d f g | xargs -I {} -n 1 echo {} LAST
a LAST
b LAST
c LAST
d LAST
f LAST
g LAST
2
I can't help thinking there could be a more neutral way of phrasing this answer. It also feels odd to point out one OS in particular (Linux), when you seem to be of the opinion that it's the behaviour required by the standard that's at fault here.
– ilkkachu
Sep 1 at 15:34
add a comment |
up vote
4
down vote
accepted
tl;dr; this is how you could do it portably, without -I and other broken fancy options:
$ echo a b c d f g | xargs -n 2 sh -c 'echo "$@" LAST' sh
a b LAST
c d LAST
f g LAST
$ seq 1 100000 | xargs sh -c 'echo "$#" LAST' sh
23692 LAST
21841 LAST
21841 LAST
21841 LAST
10785 LAST
The problem with the -I option is that it's broken by design, and there is no way around it:
$ echo a b c d f g | xargs -I {} -n 1 echo {} LAST
a b c d f g LAST
$ echo a b c d f g | xargs -I {} -n 2 echo {} LAST
{} LAST a b
{} LAST c d
{} LAST f g
But they're probably covered, because that's what the standard says:
-I replstr
^[XSI] [Option Start] Insert mode: utility is executed for each
line from standard input, taking the entire line as a single
argument, inserting it in arguments for each occurrence of
replstr.
And it doesn't say anything about the interaction with the -n and -d options, so they're free to do whatever they please.
This is how it is on an (older) FreeBSD, less unexpected but non-standard:
fzu$ echo a b c d f g | xargs -I {} -n 2 echo {} LAST
a b LAST
c d LAST
f g LAST
fzu$ echo a b c d f g | xargs -I {} -n 1 echo {} LAST
a LAST
b LAST
c LAST
d LAST
f LAST
g LAST
2
I can't help thinking there could be a more neutral way of phrasing this answer. It also feels odd to point out one OS in particular (Linux), when you seem to be of the opinion that it's the behaviour required by the standard that's at fault here.
– ilkkachu
Sep 1 at 15:34
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
tl;dr; this is how you could do it portably, without -I and other broken fancy options:
$ echo a b c d f g | xargs -n 2 sh -c 'echo "$@" LAST' sh
a b LAST
c d LAST
f g LAST
$ seq 1 100000 | xargs sh -c 'echo "$#" LAST' sh
23692 LAST
21841 LAST
21841 LAST
21841 LAST
10785 LAST
The problem with the -I option is that it's broken by design, and there is no way around it:
$ echo a b c d f g | xargs -I {} -n 1 echo {} LAST
a b c d f g LAST
$ echo a b c d f g | xargs -I {} -n 2 echo {} LAST
{} LAST a b
{} LAST c d
{} LAST f g
But they're probably covered, because that's what the standard says:
-I replstr
^[XSI] [Option Start] Insert mode: utility is executed for each
line from standard input, taking the entire line as a single
argument, inserting it in arguments for each occurrence of
replstr.
And it doesn't say anything about the interaction with the -n and -d options, so they're free to do whatever they please.
This is how it is on an (older) FreeBSD, less unexpected but non-standard:
fzu$ echo a b c d f g | xargs -I {} -n 2 echo {} LAST
a b LAST
c d LAST
f g LAST
fzu$ echo a b c d f g | xargs -I {} -n 1 echo {} LAST
a LAST
b LAST
c LAST
d LAST
f LAST
g LAST
tl;dr; this is how you could do it portably, without -I and other broken fancy options:
$ echo a b c d f g | xargs -n 2 sh -c 'echo "$@" LAST' sh
a b LAST
c d LAST
f g LAST
$ seq 1 100000 | xargs sh -c 'echo "$#" LAST' sh
23692 LAST
21841 LAST
21841 LAST
21841 LAST
10785 LAST
The problem with the -I option is that it's broken by design, and there is no way around it:
$ echo a b c d f g | xargs -I {} -n 1 echo {} LAST
a b c d f g LAST
$ echo a b c d f g | xargs -I {} -n 2 echo {} LAST
{} LAST a b
{} LAST c d
{} LAST f g
But they're probably covered, because that's what the standard says:
-I replstr
^[XSI] [Option Start] Insert mode: utility is executed for each
line from standard input, taking the entire line as a single
argument, inserting it in arguments for each occurrence of
replstr.
And it doesn't say anything about the interaction with the -n and -d options, so they're free to do whatever they please.
This is how it is on an (older) FreeBSD, less unexpected but non-standard:
fzu$ echo a b c d f g | xargs -I {} -n 2 echo {} LAST
a b LAST
c d LAST
f g LAST
fzu$ echo a b c d f g | xargs -I {} -n 1 echo {} LAST
a LAST
b LAST
c LAST
d LAST
f LAST
g LAST
edited Nov 30 at 6:56
answered Sep 1 at 15:12
mosvy
5,1791323
5,1791323
2
I can't help thinking there could be a more neutral way of phrasing this answer. It also feels odd to point out one OS in particular (Linux), when you seem to be of the opinion that it's the behaviour required by the standard that's at fault here.
– ilkkachu
Sep 1 at 15:34
add a comment |
2
I can't help thinking there could be a more neutral way of phrasing this answer. It also feels odd to point out one OS in particular (Linux), when you seem to be of the opinion that it's the behaviour required by the standard that's at fault here.
– ilkkachu
Sep 1 at 15:34
2
2
I can't help thinking there could be a more neutral way of phrasing this answer. It also feels odd to point out one OS in particular (Linux), when you seem to be of the opinion that it's the behaviour required by the standard that's at fault here.
– ilkkachu
Sep 1 at 15:34
I can't help thinking there could be a more neutral way of phrasing this answer. It also feels odd to point out one OS in particular (Linux), when you seem to be of the opinion that it's the behaviour required by the standard that's at fault here.
– ilkkachu
Sep 1 at 15:34
add a comment |
up vote
-2
down vote
Not with xargs
(alone). If you have an item list of unpredictable length, how should xargs
know from the beginning (= first element) which element would be the last one?
You'll need some additional logics around it to separate the desired element from the others.
@ilkkachu: Then, please show how from the lista b c d LAST
to get to four parameter listsa LAST
,b LAST
,c LAST
, andd LAST
by usingxargs
alone without the constantLAST
being supplied by other means (as wrongly done above). If that is what the requestor wanted.
– RudiC
Sep 1 at 21:23
I thought they wanted to go froma b c d
toa b ... LAST
, or such, with more than one ofa
,b
, etc used byxargs
for each command invocation. (one is easy, that's what-I
does.) They did specify theLAST
part in thexargs
command (not in theprintf
feedingxargs
), it's just that it doesn't work like that with-I
(but does in FreeBSDxargs -J
). But yeah, you're right in that I misread your intent, though technicallyxargs
could read the whole input before processing, but that wouldn't be very efficient.
– ilkkachu
Sep 1 at 22:29
Now that I reread the original request, I admit I may have misread it, so the base of my approach is gone...
– RudiC
Sep 1 at 22:37
add a comment |
up vote
-2
down vote
Not with xargs
(alone). If you have an item list of unpredictable length, how should xargs
know from the beginning (= first element) which element would be the last one?
You'll need some additional logics around it to separate the desired element from the others.
@ilkkachu: Then, please show how from the lista b c d LAST
to get to four parameter listsa LAST
,b LAST
,c LAST
, andd LAST
by usingxargs
alone without the constantLAST
being supplied by other means (as wrongly done above). If that is what the requestor wanted.
– RudiC
Sep 1 at 21:23
I thought they wanted to go froma b c d
toa b ... LAST
, or such, with more than one ofa
,b
, etc used byxargs
for each command invocation. (one is easy, that's what-I
does.) They did specify theLAST
part in thexargs
command (not in theprintf
feedingxargs
), it's just that it doesn't work like that with-I
(but does in FreeBSDxargs -J
). But yeah, you're right in that I misread your intent, though technicallyxargs
could read the whole input before processing, but that wouldn't be very efficient.
– ilkkachu
Sep 1 at 22:29
Now that I reread the original request, I admit I may have misread it, so the base of my approach is gone...
– RudiC
Sep 1 at 22:37
add a comment |
up vote
-2
down vote
up vote
-2
down vote
Not with xargs
(alone). If you have an item list of unpredictable length, how should xargs
know from the beginning (= first element) which element would be the last one?
You'll need some additional logics around it to separate the desired element from the others.
Not with xargs
(alone). If you have an item list of unpredictable length, how should xargs
know from the beginning (= first element) which element would be the last one?
You'll need some additional logics around it to separate the desired element from the others.
answered Sep 1 at 16:15
RudiC
3,7351312
3,7351312
@ilkkachu: Then, please show how from the lista b c d LAST
to get to four parameter listsa LAST
,b LAST
,c LAST
, andd LAST
by usingxargs
alone without the constantLAST
being supplied by other means (as wrongly done above). If that is what the requestor wanted.
– RudiC
Sep 1 at 21:23
I thought they wanted to go froma b c d
toa b ... LAST
, or such, with more than one ofa
,b
, etc used byxargs
for each command invocation. (one is easy, that's what-I
does.) They did specify theLAST
part in thexargs
command (not in theprintf
feedingxargs
), it's just that it doesn't work like that with-I
(but does in FreeBSDxargs -J
). But yeah, you're right in that I misread your intent, though technicallyxargs
could read the whole input before processing, but that wouldn't be very efficient.
– ilkkachu
Sep 1 at 22:29
Now that I reread the original request, I admit I may have misread it, so the base of my approach is gone...
– RudiC
Sep 1 at 22:37
add a comment |
@ilkkachu: Then, please show how from the lista b c d LAST
to get to four parameter listsa LAST
,b LAST
,c LAST
, andd LAST
by usingxargs
alone without the constantLAST
being supplied by other means (as wrongly done above). If that is what the requestor wanted.
– RudiC
Sep 1 at 21:23
I thought they wanted to go froma b c d
toa b ... LAST
, or such, with more than one ofa
,b
, etc used byxargs
for each command invocation. (one is easy, that's what-I
does.) They did specify theLAST
part in thexargs
command (not in theprintf
feedingxargs
), it's just that it doesn't work like that with-I
(but does in FreeBSDxargs -J
). But yeah, you're right in that I misread your intent, though technicallyxargs
could read the whole input before processing, but that wouldn't be very efficient.
– ilkkachu
Sep 1 at 22:29
Now that I reread the original request, I admit I may have misread it, so the base of my approach is gone...
– RudiC
Sep 1 at 22:37
@ilkkachu: Then, please show how from the list
a b c d LAST
to get to four parameter lists a LAST
, b LAST
, c LAST
, and d LAST
by using xargs
alone without the constant LAST
being supplied by other means (as wrongly done above). If that is what the requestor wanted.– RudiC
Sep 1 at 21:23
@ilkkachu: Then, please show how from the list
a b c d LAST
to get to four parameter lists a LAST
, b LAST
, c LAST
, and d LAST
by using xargs
alone without the constant LAST
being supplied by other means (as wrongly done above). If that is what the requestor wanted.– RudiC
Sep 1 at 21:23
I thought they wanted to go from
a b c d
to a b ... LAST
, or such, with more than one of a
, b
, etc used by xargs
for each command invocation. (one is easy, that's what -I
does.) They did specify the LAST
part in the xargs
command (not in the printf
feeding xargs
), it's just that it doesn't work like that with -I
(but does in FreeBSD xargs -J
). But yeah, you're right in that I misread your intent, though technicallyxargs
could read the whole input before processing, but that wouldn't be very efficient.– ilkkachu
Sep 1 at 22:29
I thought they wanted to go from
a b c d
to a b ... LAST
, or such, with more than one of a
, b
, etc used by xargs
for each command invocation. (one is easy, that's what -I
does.) They did specify the LAST
part in the xargs
command (not in the printf
feeding xargs
), it's just that it doesn't work like that with -I
(but does in FreeBSD xargs -J
). But yeah, you're right in that I misread your intent, though technicallyxargs
could read the whole input before processing, but that wouldn't be very efficient.– ilkkachu
Sep 1 at 22:29
Now that I reread the original request, I admit I may have misread it, so the base of my approach is gone...
– RudiC
Sep 1 at 22:37
Now that I reread the original request, I admit I may have misread it, so the base of my approach is gone...
– RudiC
Sep 1 at 22:37
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f466242%2ffixed-last-argument-with-xargs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
not as far as I known but at least
cp
,mv
andln
commands havs a-t|--target-directory
parameter that replace the last arg...– xenoid
Sep 1 at 13:29
1
Would you please shed more light on what you want. i mean revise the question and add more clarifications. In the current shape it is not clear!
– user88036
Sep 1 at 13:51
1
Also, the output of your command is empty lines.
– user88036
Sep 1 at 13:53
1
Your command still doesn't work! "xargs: LAST: No such file or directory"
– user88036
Sep 1 at 14:00
1
Yes! it didn't work . I use Centos 7
– user88036
Sep 1 at 14:09