help-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Parsing multiline string line by line


From: Nelson H. F. Beebe
Subject: Re: Parsing multiline string line by line
Date: Thu, 2 Mar 2023 07:39:36 -0700

List member goncholden <goncholden@protonmail.com> asks about
splitting a multiline string into lines for separate processing.  Here
is an example:

        % cat foo.awk
        BEGIN {
        str=" \n \
        Here is the first line \n \
        Another line \n \n"

            n = split(str, parts, "\n")
            for (k = 1; k <= n; ++k) printf("line[%d] = [%s]\n", k, parts[k])
        }

        % awk -f foo.awk
        line[1] = [ ]
        line[2] = [ Here is the first line ]
        line[3] = [ Another line ]
        line[4] = [ ]
        line[5] = []

The split() function is extremely useful, and worthy of study in the
gawk manual or awk books.

-------------------------------------------------------------------------------
- Nelson H. F. Beebe                    Tel: +1 801 581 5254                  -
- University of Utah                                                          -
- Department of Mathematics, 110 LCB    Internet e-mail: beebe@math.utah.edu  -
- 155 S 1400 E RM 233                       beebe@acm.org  beebe@computer.org -
- Salt Lake City, UT 84112-0090, USA    URL: http://www.math.utah.edu/~beebe/ -
-------------------------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]