1

I have a LaTeX-File like this:

\usepackage[colorlinks,
citecolor=black,
          urlcolor=black
]
{hyperref}
\usepackage{ngerman}

and i have to output it like this:

hyperref:colorlinks,citecolor=black,urlcolor=black
ngerman:

I may only use sed and egrep, not awk and perl. How do I do this?

devnull
  • 118,548
  • 33
  • 236
  • 227
BadJoke
  • 147
  • 1
  • 1
  • 10
  • 1
    why can you only use sed and egrep? Is this a homework problem? – evil otto Dec 02 '13 at 19:57
  • I tried this one: sed -e '$!N;s#\,\n#\,#' tmp.tex | sed -e 's/\\usepackage\([[]\(.*\)[]]\)*{\(.*\)}/\3:\2/g' It matches the second one, but not the one on multiple lines. I think the first sed is wrong somehow... – BadJoke Dec 02 '13 at 20:04
  • And I think I have to remove every newline until the first "}". – BadJoke Dec 02 '13 at 20:16
  • Ok I got something else for newline-removing: sed '/\,$/N;s/\n//' tmp.tex But this one removes only every 2nd newline. – BadJoke Dec 02 '13 at 20:39
  • yes sed only could certainly do the trick but you need to be more explicit with the conversion you want. Ex: why urlcolor is no more in output – NeronLeVelu Dec 02 '13 at 20:46
  • Oops, i will change this. EDIT: Changed. – BadJoke Dec 02 '13 at 20:55
  • And my Output should be: "Thing in curly brackets":"things in square brackets" – BadJoke Dec 02 '13 at 20:59

1 Answers1

0

You can pipe sed command into other sed commands to use a layered approach.

  1. Remove newlines
  2. Convert } into }\n
  3. Convert \usepackage[optional]{packagename} into packagename:[optional]
  4. Remove the spaces and brackets

Here is the script:

sed ':a;N;$!ba;s/\n/ /g' file.latex | sed 's/}/}\n/g' | sed 's/\\usepackage[[:space:]]*\(.\+\)\?[[:space:]]*{\(.\+\)}/\2:\1/' | sed 's/\[//g;s/\]//g;s/[[:space:]]//g'

Here is the result:

hyperref:colorlinks,citecolor=black,urlcolor=black
ngerman:

The first command is taken verbatim from How to insert a newline in front of a pattern?. Some of these commands can be combined, but I find it easier to understand when there is a separate command for each step. If your latex files are small, time should not be a problem.

Community
  • 1
  • 1
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264