1

I can extract the contents of all the \include statements from a latex file (and append ".tex" to each one) with

grep -P "\\\\include{" Thesis_master.tex |sed -n -e"s/\\\\include{/$1/" -e" s/}.*$/.tex/p"

(I couldn't get lookbehinds working in grep, hence the pipe through sed. That gives me a list of filenames, one per line. I'd now like to pass those files to aspell. But aspell only accepts one filename as an argument, so I can't just tack |xargs aspell -c on the end.

I've read this related question but that reads from a file line by line through xargs. So how do I get it to read from the output of sed line by line?

Community
  • 1
  • 1
Chris H
  • 331
  • 1
  • 2
  • 15

2 Answers2

1

I think xargs -L 1 should do what you need:

grep -P "\\\\include{" Thesis_master.tex | \
sed -n -e"s/\\\\include{/$1/" -e" s/}.*$/.tex/p" | \
xargs -L 1 aspell -c

(Backslash line continuation added for readability)

This will cause xargs to call aspell exactly once per line from the sed pipe.


Since your aspell commands appear to exit with a 255 code, this causes xargs to stop. You could trick xargs into not exiting by doing something like:

grep -P "\\\\include{" Thesis_master.tex | \
sed -n -e"s/\\\\include{/$1/" -e" s/}.*$/.tex/p" | \
xargs -L 1 -I % bash -c "aspell -c %; true"

This will run aspell in a subshell, followed by the true command which always return a 0 exit code to xargs.

Digital Trauma
  • 15,475
  • 3
  • 51
  • 83
  • I believe you're right - now trying to fix an issue with aspell closing with exit code 255 and no error message! – Chris H Mar 28 '14 at 19:59
  • You can debug `xargs` commands by changing your command to something like `xargs -L 1 echo aspell -c`. That way, xargs will effectively simply echo every command instead of executing it. You can then run the echoed commands manually and figure out why they're failing. – Digital Trauma Mar 28 '14 at 20:01
  • I was just trying that -- thanks -- I get `aspell -c chap_Theory/chap_Theory.tex` etc. Which works if I copy and paste or retype but not from xargs. Equally if I redirect the output of sed in my Q to a file tmp.txt, strip that to one line, and `cat tmp.txt | xargs aspell -c` it gives the same behaviour - I think I might have aspell related digging to do. – Chris H Mar 28 '14 at 20:05
  • From the `xargs` manpage: "The xargs utility exits immediately (without processing any further input) if [...], or an invocation of utility exits with a value of 255." – Digital Trauma Mar 28 '14 at 20:16
  • @ChrisH See my edit for a possible way to prevent xargs from quitting early – Digital Trauma Mar 28 '14 at 20:24
  • That didn't work either it echoes what I'd expect but with the echo taken out it gives me back the input line straight away, with some disk activity - I never see the aspell screen. But if I run `grep -P "\\\\include{" Thesis_master.tex |sed -n -e"s/\\\\include{//" -e" s/}.*$/.tex/p" |xargs -t -L 1 -I% xterm -e "aspell -c %; true"` (-t for debug) it works, just with aspell in an xterm window. Strange but I have something tat runs for now so thank you for all your help. – Chris H Mar 28 '14 at 20:39
  • @ChrisH Oops, I should have taken that `echo` out. Edited. – Digital Trauma Mar 28 '14 at 20:44
  • I thought it was there for debugging! But I think aspell is fussy about screen or I/O or something - calling xterm as in my previous comment turns out not to need the `; true`. – Chris H Mar 28 '14 at 20:53
1

The grep recipe is:

grep -oP '\\include{\K.+?(?=})' latex.file | xargs aspell ...
glenn jackman
  • 238,783
  • 38
  • 220
  • 352