0

I am trying to pipe a few commands in a row; it works with a single file, but gives me an error once I try it on multiple files at once.

On a single file in my working folder:

find . -type f -iname "summary.5runs.*" -print0 | xargs -0 cut -f1-2 | head -n 2
#It works

Now I want to scan all files with a certain prefix/suffix in the name in all subdirectories of my working folder, then write the results to text file

find . -type f -iname "ww.*.out.txt" -print0 | xargs -0 cut -f3-5 | head -n 42 > summary.5runs.txt
#Error: xargs: cut: terminated by signal 13

I guess my problem is to reiterate through multiple files, but I am not sure how to do it.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Max_IT
  • 602
  • 5
  • 15
  • 1
    I can't reproduce yourr error, but seems like you should not care about it: [signal 13...](https://stackoverflow.com/questions/27800726/ls-terminated-by-signal-13-when-using-xargs) Don't you get the results you expect in your output? – eraenderer Aug 29 '18 at 14:07
  • 1
    it means xargs is trying to pipe output to a command which is no longer reading input. See https://stackoverflow.com/a/27903078/8710344 – HardcoreHenry Aug 29 '18 at 14:09
  • Wow, I didn't check for the output. Unfortunately, it olny gives me the result of one file – Max_IT Aug 29 '18 at 14:11

1 Answers1

1

Your final head stops after 42 lines of total output, but you want it to operate per file. You could fudge around with a subshell in xargs:

xargs -0 -I{} bash -c 'cut -f3-5 "$1" | head -n 42' _ {} > summary.5runs.txt

or you could make it part of an -exec action:

find . -type f -iname "ww.*.out.txt" \
    -exec bash -c 'cut -f3-5 "$1" | head -n 42' _ {} \; > summary.5runs.txt

Alternatively, you could loop over all the files in the subshell so you have to spawn just one:

find . -type f -iname "ww.*.out.txt" \
    -exec bash -c 'for f; do cut -f3-5 "$f" | head -n 42; done' _ {} + \
    > summary.5runs.txt

Notice the {} + instead of {} \;.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116