우선..

cat 으로 멀티라인을 읽어서 xargs  로 처리하고 싶은데 멀티라인이 아니라 하나만 처리될경우.... (사실 전체가 하나로 넘어가는거겠지만)

이때 하나하나 라인마다 실행하고 싶으면



from https://unix.stackexchange.com/questions/7558/execute-a-command-once-per-line-of-piped-input


The accepted answer has the right idea, but the key is to pass xargs the -n1 switch, which means "Execute the command once per line of output:"

cat file... | xargs -n1 command

Or, for a single input file you can avoid the pipe from cat entirely and just go with:

<file xargs -n1 command
  • 1
    Also of interest is the ability of xargs to not run if stdin is empty:  --no-run-if-empty  -r : If the standard input does not contain any nonblanks, do not run the command. Normally, the command is run once even if there is no input. This option is a GNU extension. – Ronan Jouchet Oct 24 '15 at 19:58 
  • 4
    How dyou access the line inside command ? – B T Apr 14 '16 at 1:21
  • This is the correct use of xargs. Without -n1, it only works on commands that treat lists of parameters as multiple invocations which not all do. – masterxilo Mar 15 at 9:43
  • printf "foo bar\nbaz bat" | xargs -n1 echo whee splits by words and not by lines – Gismo Ranas Oct 24 at 8:32



와!! 그래 -n1  이다 ..  아주 좋은 명령이네;



+ Recent posts