------------------------------------------------------------------------ convert csv to latex (preliminary step to preparing a table ------------------------------------------------------------------------ (N. Blagorodnova, Abhimanyu) $ cat file.csv #note blank line A, B, C, D 1, 2, 3, 4 #Solution 1a,1b: $ tr ',' '&' < file.csv | sed 's;$;\\\\;' file.csv #non traditional use of ";" #this also edits blank lines $ tr ',' '&' < files.csv | sed '/^$/!s;$;\\\\;' file.csv # Solution 2a,2b: $ awk -F, '{gsub(/,/,"&"); print $0 "\\\\"}' file.csv $ awk -F, '!/^$/{gsub(/,/,"&"); print $0 "\\\\"}' file.csv Now we can convert this to a stand alone utility $ cat csv2tex 1 #!/bin/bash 2 # csv2tex infile.txt (will produce infile.tex) 3 infile=$1 4 file=${infile%.*} #base_name of input file 5 ofile=file".tex" #output file is now "base_name.t" 6 printf "\\\begin{data}\n" > $ofile 7 awk -F, '!/^$/{gsub(/,/,"&"); print $0 "\\\\"}' $infile >> $ofile 8 printf "\\\end{data}\n" >> $ofile $ chmod +x csv2tex $ ./csv2tex file.csv \begin{data} A, B, C, D\\ 1, 2, 3, 4\\ \end{data} ------------------------------------------------------------------------ Now if you want to do this with only "sed". ------------------------------------------------------------------------ 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!