programing

여러 인수가있는 xargs

kingscode 2021. 1. 17. 10:58
반응형

여러 인수가있는 xargs


소스 입력, input.txt가 있습니다.

a.txt
b.txt
c.txt

이 입력을 다음과 같이 프로그램에 공급하고 싶습니다.

my-program --file=a.txt --file=b.txt --file=c.txt

그래서 xargs 를 사용하려고 하지만 운이 없습니다.

cat input.txt | xargs -i echo "my-program --file"{}

그것은 준다

my-program --file=a.txt
my-program --file=b.txt
my-program --file=c.txt

하지만 나는 원한다

my-program --file=a.txt --file=b.txt --file=c.txt

어떤 생각?


지금까지 제공된 솔루션은 공백을 포함하는 파일 이름을 올바르게 처리하지 않습니다. 파일 이름에 '또는 "가 포함되어 있어도 일부는 실패합니다. 입력 파일이 사용자에 의해 생성 된 경우 놀라운 파일 이름에 대비해야합니다.

GNU Parallel 은 이러한 파일 이름을 잘 처리하고 (적어도) 3 가지 다른 솔루션을 제공합니다. 프로그램이 3 개의 인수 만 취하면 다음과 같이 작동합니다.

(echo a1.txt; echo b1.txt; echo c1.txt;
 echo a2.txt; echo b2.txt; echo c2.txt;) |
parallel -N 3 my-program --file={1} --file={2} --file={3}

또는:

(echo a1.txt; echo b1.txt; echo c1.txt;
 echo a2.txt; echo b2.txt; echo c2.txt;) |
parallel -X -N 3 my-program --file={}

그러나 프로그램이 명령 줄에 맞는만큼의 인수를 사용하는 경우 :

(echo a1.txt; echo b1.txt; echo c1.txt;
 echo d1.txt; echo e1.txt; echo f1.txt;) |
parallel -X my-program --file={}

자세히 알아 보려면 소개 동영상을 참조하세요 . http://www.youtube.com/watch?v=OpaiGYxkSuQ


그들 모두를 듣지 마십시오 :)이 예제를보십시오 :

echo argument1 argument2 argument3 | xargs -l bash -c 'echo this is first:$0 second:$1 third:$2' | xargs

출력은

this is first:argument1 second:argument2 third:argument3

어때 :

echo $'a.txt\nb.txt\nc.txt' | xargs -n 3 sh -c '
   echo my-program --file="$1" --file="$2" --file="$3"
' argv0

당신은 사용할 수 있습니다 sed접두사로 --file=전화 한 후 각 라인과 xargs:

sed -e 's/^/--file=/' input.txt | xargs my-program

다음은 세 개의 인수에 sed를 사용하는 솔루션이지만 각 인수에 동일한 변환을 적용한다는 점에서 제한적입니다.

cat input.txt | sed 's/^/--file=/g' | xargs -n3 my-program

다음은 두 개의 인수에 대해 작동하지만 더 많은 유연성을 허용하는 메서드입니다.

cat input.txt | xargs -n 2 | xargs -I{} sh -c 'V="{}"; my-program -file=${V% *} -file=${V#* }'

두 개의 xargs 호출을 사용하는 것이 더 간단합니다. 1st는 각 줄을 --file=..., 2nd는 실제로 xargs 작업을 수행합니다.->

$ cat input.txt | xargs -I@ echo --file=@ | xargs echo my-program
my-program --file=a.txt --file=b.txt --file=c.txt

나는 비슷한 문제를 발견하고 지금까지 제시된 것보다 더 좋고 깨끗한 해결책을 찾았습니다.

xargs내가 끝낸 구문은 (귀하의 예를 들어) 다음과 같습니다.

xargs -I X echo --file=X

전체 명령 줄은 다음과 같습니다.

my-program $(cat input.txt | xargs -I X echo --file=X)

마치

my-program --file=a.txt --file=b.txt --file=c.txt

완료되었습니다 (제공하는 경우 input.txt예제의 데이터가 포함됨).


Actually, in my case I needed to first find the files and also needed them sorted so my command line looks like this:

my-program $(find base/path -name "some*pattern" -print0 | sort -z | xargs -0 -I X echo --files=X)

Few details that might not be clear (they were not for me):

  • some*pattern must be quoted since otherwise shell would expand it before passing to find.
  • -print0, then -z and finally -0 use null-separation to ensure proper handling of files with spaces or other wired names.

Note however that I didn't test it deeply yet. Though it seems to be working.


It's because echo prints a newline. Try something like

echo my-program `xargs --arg-file input.txt -i echo -n " --file "{}`

xargs doesn't work that way. Try:

  myprogram $(sed -e 's/^/--file=/' input.txt)

I was looking for a solution for this exact problem and came to the conclution of coding a script in the midle.

to transform the standard output for the next example use the -n '\n' delimeter

example:

 user@mybox:~$ echo "file1.txt file2.txt" | xargs -n1 ScriptInTheMiddle.sh

 inside the ScriptInTheMidle.sh:
 !#/bin/bash
 var1=`echo $1 | cut -d ' ' -f1 `
 var2=`echo $1 | cut -d ' ' -f2 `
 myprogram  "--file1="$var1 "--file2="$var2 

For this solution to work you need to have a space between those arguments file1.txt and file2.txt, or whatever delimeter you choose, one more thing, inside the script make sure you check -f1 and -f2 as they mean "take the first word and take the second word" depending on the first delimeter's position found (delimeters could be ' ' ';' '.' whatever you wish between single quotes . Add as many parameters as you wish.

Problem solved using xargs, cut , and some bash scripting.

Cheers!

if you wanna pass by I have some useful tips http://hongouru.blogspot.com


Nobody has mentioned echoing out from a loop yet, so I'll put that in for completeness sake (it would be my second approach, the sed one being the first):

for line in $(< input.txt) ; do echo --file=$line ; done | xargs echo my-program

Actually, it's relatively easy:

... | sed 's/^/--prefix=/g' | xargs echo | xargs -I PARAMS your_cmd PARAMS

The sed 's/^/--prefix=/g' is optional, in case you need to prefix each param with some --prefix=.

The xargs echo turns the list of param lines (one param in each line) into a list of params in a single line and the xargs -I PARAMS your_cmd PARAMS allows you to run a command, placing the params where ever you want.

So cat input.txt | sed 's/^/--file=/g' | xargs echo | xargs -I PARAMS my-program PARAMS does what you need (assuming all lines within input.txt are simple and qualify as a single param value each).

ReferenceURL : https://stackoverflow.com/questions/3770432/xargs-with-multiple-arguments

반응형