开发者

Finding a line in a text file and replacing it with the contents of an existing text file

I have a latex project that I want to build into an epub file using pandoc. I'm using the \input{FILE} command to keep chapters in separate latex files. Unfortunately pandoc doesn't understand the \input command, so my chapters never get inserted.

I need to run a script or something to process my main.tex file to find the lines that look like

\input{ch1.tex}
\input{ch2.tex}
...

and at each line, replace it with it with the contents of that file, then remove the original \input command, then input the results into a temporary file or pipe the result directly to the pandoc command to be build.

This project gets built using MikTex and TexLive on both WinXP and Debian respectively. A solution that works on Linux is preferre开发者_StackOverflow中文版d.


If you have gawk:

gawk '
    match($0, /\\input\{([^\}]+)/, a) {
        system("cat " a[1])
        next
    }
    {print}
'


Using Perl:

$ cat file1.txt
foo
bar
qwe\input{file2.txt}asd\input{file3.txt}zxc
baz
$ cat file2.txt
qux
quux
quuux
$ cat file3.txt
xyzzy
xyzzzy

$ <file1.txt  perl -pe 's/\\input{([^}]*)}/open $1,"<$1";join("",<$1>)/ge'
foo
bar
qwequx
quux
quuux
asdxyzzy
xyzzzy
zxc
baz
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜