bug-gawk
[Top][All Lists]
Advanced

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

The best way to convert space separated text to TSV?


From: Peng Yu
Subject: The best way to convert space separated text to TSV?
Date: Mon, 10 Feb 2020 21:42:20 -0600

Hi,

Many programs (such as wc and ps) print results in tables with one or
more spaces as separators. But the last column allows spaces in them.
To process the output of wc, I came up with the following code
(sometimes I need to manually change the display name such as
"file1"). But it is too verbose.

BEGIN {
        OFS = "\t"
        for(i=1;i<ARGC;++i) {
                fnames[i] = ARGV[i]
        }
        nfiles = ARGC - 1
        delete ARGV
}
{
        match($0, /^[ ]*/)
        line = substr($0, RSTART+RLENGTH)
        NF = 1
        for(i=1; i<=n; ++i) {
                if(match(line, /[ ]+/)) {
                        $i = substr(line, 1, RSTART-1)
                        line = substr(line, RSTART+RLENGTH)
                }
        }
        if(NR <= nfiles) {
                $i = fnames[NR]
        } else {
                if(line "") $i = line
        }
        print
}

$ awk -v n=2 -f ./wc.awk file1 <<EOF
 a bb c
aa  b c
EOF

$ awk -v n=3 -f ./wc.awk <<EOF
 a bb c
EOF

What is the most succinct way to convert such kind of input to TSV
format with gawk? Thanks.

-- 
Regards,
Peng



reply via email to

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