Skip to main content
added 381 characters in body
Source Link
glenn jackman
  • 88.5k
  • 16
  • 124
  • 179

Here's some fairly cryptic awk:

awk '{for (i=1; i<=NF; i+=2) {printf "%s ", $(i + (NR > FNR))}; print ""}' file file

The first time you process the file, NR > FNR will be false, or 0.
The second time you process the file, NR > FNR will be true, or 1.


This one requires a single pass through the file, but you have to keep half the file in memory:

awk '
    {
        for (i=1; i<NF; i+=2) {
            printf "%s ", $i
            row[NR] = row[NR] $(i+1) " "
        }
        print ""
    }
    END {
        for (i=1; i<=NR; i++) print row[i]
    }
' file

Here's some fairly cryptic awk:

awk '{for (i=1; i<=NF; i+=2) {printf "%s ", $(i + (NR > FNR))}; print ""}' file file

The first time you process the file, NR > FNR will be false, or 0.
The second time you process the file, NR > FNR will be true, or 1.

Here's some fairly cryptic awk:

awk '{for (i=1; i<=NF; i+=2) {printf "%s ", $(i + (NR > FNR))}; print ""}' file file

The first time you process the file, NR > FNR will be false, or 0.
The second time you process the file, NR > FNR will be true, or 1.


This one requires a single pass through the file, but you have to keep half the file in memory:

awk '
    {
        for (i=1; i<NF; i+=2) {
            printf "%s ", $i
            row[NR] = row[NR] $(i+1) " "
        }
        print ""
    }
    END {
        for (i=1; i<=NR; i++) print row[i]
    }
' file
Source Link
glenn jackman
  • 88.5k
  • 16
  • 124
  • 179

Here's some fairly cryptic awk:

awk '{for (i=1; i<=NF; i+=2) {printf "%s ", $(i + (NR > FNR))}; print ""}' file file

The first time you process the file, NR > FNR will be false, or 0.
The second time you process the file, NR > FNR will be true, or 1.