------------------------------------------------------------------------ Convert a comma separate file (csv) to LaTeX style file (N. Blagorodnova) ------------------------------------------------------------------------ Say the input file is file.csv and desired output file is file.tex $ cat file.csv 1,4,5,-2,A 1,3,2,-3,B What you wish is 1 & 4 & 5 & $-$2 & A\cr 1 & 3 & 2 & $-$3 & B\cr What you need to do go from .csv to .tex is replace "," with " & " convert "-" to proper minus "$-$" add "\cr" to the end of line We do this sequentially in three commands $ sed -e 's/,/\ \& /g' -e 's/-/$-$/g' -e 's/$/\\cr/' file.csv 1 & 4 & 5 & $-$2 & A\cr 1 & 3 & 2 & $-$3 & B\cr #compact & satisfying version $ sed 's/,/ \& /g;s/-/$-$/g; s/$/\\cr/' file.csv ------------------------------------------------------------------------ Additional Requirement: Delete comment headers & footers ------------------------------------------------------------------------ Say your csv file had headers and footers that are easily identified (e.g. with a starting %). So you want these lines to be deleted $ $ cat file.csv %header 1,4,5,-2,A 1,3,2,-3,B %footer What is needed identify comment line and delete $ sed '/^ %/d' file.csv | sed 's/,/ \& /g;s/-/$-$/g; s/$/\\cr/' 1 & 4 & 5 & $-$2 & A\cr 1 & 3 & 2 & $-$3 & B\cr #a real sed guru does not use sed more than once $ sed -n '/^ *%/!{s/,/ \& /g;s/-/$-$/g; s/$/\\cr/;p;}' file.csv > file.tex ------------------------------------------------------------------------ Additional Requirement: Add headers & footers ------------------------------------------------------------------------ Now let us get more sophisticated. We wish to insert "\begin{data}" and "\end{data}" Up until now I have been using "sed". However, gsed which is GNU sed, has several attractive features especially for the task at hand. $ gsed -e '1i \\\begin{data}' -e '$a \\\end{data}' file.tex \begin{data} 1 & 4 & 5 & $-$2 & A\cr 1 & 3 & 2 & $-$3 & B\cr \end{data} Normally you would have copied file.tex to another file and then deleted the previous one etc. Instead try this $ gsed -it -e '1i \\\begin{data}' -e '$a \\\end{data}' file.tex In your directory you will find two files: file.tex which is your final file and file.text which is a copy of file.tex before lines were added. Finally if you did feel that it was too much typing then try this! $ sed -n '/^ *%/!{s/,/ \& /g;s/-/$-$/g; s/$/\\cr/;p;}' file.csv | gsed -e '1i \\\begin{data}' -e '$a \\\end{data}' > file.tex #now you are a sed guru!