[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: awk BEGIN END syntax
From: |
Ivo Palli |
Subject: |
Re: awk BEGIN END syntax |
Date: |
Sun, 23 Jun 2024 17:05:47 +0200 |
User-agent: |
SOGoMail 5.8.2 |
An awk program consists of 3 main parts:
* BEGIN
This is code block that is executed BEFORE handling any data * One or more
blocks of code per line
These get executed for every line of data * END
This code block is executed after all data has been handledCompare these
examples:
awk 'BEGIN{print "Hello world"; exit}'
seq 1 10 | awk '{print $0}'
seq 1 10 | awk '$0%2{print "uneven: " $0} !($0%2){print "even: " $0}'
find /etc -type f -printf "%s\t%f\n" | \
awk 'BEGIN{FS="\t"} {s=s+$1;} END{print("size: " s)}'
This should help you on your way.
On Sunday, June 23, 2024 12:03 CEST, Michael Herdman via Help-gawk
<help-gawk@gnu.org> wrote:
Hi,
How can I resolve the following problem? I have an infile with two
tab-separated columns the first of which contains two words separated by
a single space:
a b g
c d h
e f i
When I run the following awk command, in which the quotation marks are
escaped to retain them:
awk 'BEGIN {print "<tr><td class=\"Col1_col\">"$1"</td><td
class=\"Col2_col\">"$2"</td></tr>"}' infile > outfile
I expect the outfile to contain:
<tr><td class="Col1_col"a b></td><td class="Col2_col"g></td></tr>
but $1 and $2 are not returned.
I know that the BEGIN statement has to be followed somewhere by an END
statement, but I do not see where to put it or what action to assign to it.
--
Michael Herdman
Institut Pasteur (retired)
Visit the cyanobacterial reference phylogeny site:
http://cyanophylogeny.scienceontheweb.net/