开发者

Awk removing all blank lines in a script (not with command line)

开发者 https://www.devze.com 2023-02-06 12:36 出处:网络
I am converting a csv file into a pdf using LaTeX. My awk script looks like this: BEGIN {printf \"\\\\documentclass[a4paper,notitlepage]{report}\\n\" \\

I am converting a csv file into a pdf using LaTeX. My awk script looks like this:

BEGIN {printf "\\documentclass[a4paper,notitlepage]{report}\n" \
"\\usepackage[utf8]{inputenc}\n"\
"\\usepackage[francais]{babel}\n"\
"\\usepackage[T1]{fontenc}\n"\
"\\usepackage{hyperref}\n"\
"\\usepackage{textcomp}\n"\
"\\usepackage[left=1cm]{geometr开发者_Python百科y}\n"\
..........
{print $1"\n" \
$2"\n" \
$3"\n" \
$4"\t"\
$5"\t" \
$6"\t" \
$7"\t" \
$8"\t" \
$9"\t" \
$10"\n""\\linebreak" \
......
$39"\t"\
$40;}

END {print "\\end{document}";}

I have found various flavors of commands to remove blank lines in the command line (awk 'NF', awk '$0!~/^$/ {print $0}') and tried to incorporate them at the end of the script with various combinations of BEGIN and END statements but nothing works.

Could anyone help? Thank you very much in advance...


Try adding !/^$/ before this line {print $1"\n" \:

!/^$/ {print $1"\n" \

Edit:

Here's another approach:

BEGIN {
    ...
}
!/^[[:blank:]]*$/ {    # skip blank *input* lines
    output = $1"\n" \
             $2"\n" \
             ...
             $40
    # remove blank lines from *output*
    # it also removes trailing whitespace from each line
    gsub("([[:blank:]]*\n)+", "\n", output)
    print output
}
END {
   ...
}
0

精彩评论

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