2

https://www.gnu.org/software/gawk/manual/gawk.html based on section 1.1.2 1.1.2 Running awk Without Input Files I can understand awk '{print $0}' marks.txt it's just like echo. it will return

1)  Amit    Physics  80
2)  Rahul   Maths    90
3)  Shyam   Biology  87
4)  Kedar   English  85
5)  Hari    History  89

https://stackoverflow.com/questions/2961635/using-awk-to-print-all-columns-from-the-nth-to-the-last?answertab=votes#tab-top

The following code also work as expected.

awk '{print $0}' marks.txt  | awk '{print $2 "\t" $3 "\t"$4}'

I don't understand awk '{$1=""; print $0}' marks.txt Even though now intuitively make sense.

3
  • Are you asking for explanation what does awk '{$1=""; print $0}' marks.txt do? It's explained in SO question you linked: Print all but the first column: Commented Jan 8, 2022 at 14:47
  • $1="" seems make sense. But I guess I am ask $1="" manual reference.
    – jian
    Commented Jan 8, 2022 at 14:48
  • It's worth mentioning that that is not a good approach to print all columns from the Nth to the last since it also changes the white space between the columns and needs an additional step to remove the leading white space it leaves behind since it's not deleting the field, it's just changing it to null and leaving the separator(s) around it.
    – Ed Morton
    Commented Jan 9, 2022 at 12:21

1 Answer 1

5

$1=""; print $0 clears the first field and recalculates the whole line before printing it. This is documented in the Changing Fields section of the GAWK manual:

When the value of a field is changed (as perceived by awk), the text of the input record is recalculated to contain the new field where the old one was. In other words, $0 changes to reflect the altered field.

This is true in all implementations of AWK, it’s not a GNU extension; The AWK Programming Language describes this behaviour as follows:

This is important: when $0 is changed by assignment or substitution, $1, $2, etc., and NF will be recomputed; likewise, when one of $1, $2, etc., is changed, $0 is reconstructed using OFS to separate fields.

5
  • I would like to add that this way the separator before the second column is preserved.
    – Cyrus
    Commented Jan 8, 2022 at 22:24
  • 2
    Wish they'd said "reconstructed" instead of "recalculated" in the docs. "Recalculated" makes it sound like what's happening is just a math operation whereas $0 is actually being rebuilt from it's fields plus the OFS string.
    – Ed Morton
    Commented Jan 9, 2022 at 14:33
  • 1
    To confuse (or maybe help clarify) matters further, see also echo a b c | awk '{$1=""; $0 = $0; $1 = $1; print $0}' Commented Jan 11, 2022 at 14:27
  • @StéphaneChazelas Ah, thanks! I was wondering why the first field simply did not disappear. So it's not as simple as just setting $1 to an empty string, you also have to provoke awk to re-form $0.
    – Kusalananda
    Commented Jan 11, 2022 at 14:29
  • 1
    Note that FS is involved in regenerating $1, $2, NF... when $0 is modified, while it's OFS that is involved to regenerate $0 when $1, $2... (and NF with some implementations) are modified. Commented Jan 11, 2022 at 14:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .