Skip to main content
Added solution to salvage multiple spaces.
Source Link
user232326
user232326

Simply:

awk '$1=NR "."'

This will change the first field (first text before a blank space) to the line (record) number and a dot. Since the record number is always positive and greater than 0, the result of the assignment will always be true causing the line (record) to be printed with the first field changed. 

As the first field gets changed the NF (number of fields) and the zero field ($0) get re-calculated transforming all repeated white-space to one space. 

That is: lines that have multiple adjacent spaces (and tabs) are coalesced into a single space.

If multiple spaces need to be salvaged from awk processing, it becomes quite more convoluted (only GNU awk has patsplit):

awk '{
       n=patsplit($0,f,/[^ \t\n]+/,s);   # split the line storing whitespace.
       f[1] = NR "."                     # only change actually needed.
       for(i=0;i<=n;i++)                 # for all fields (and separators)
       {  
          printf("%s%s",f[i],s[i])       # print each field and separator.
       }
       print("")                         # cause the printing of an OFS
     }
    ' patsplitfile

Simply:

awk '$1=NR "."'

This will change the first field (first text before a blank space) to the line (record) number and a dot. Since the record number is always positive and greater than 0, the result of the assignment will always be true causing the line (record) to be printed with the first field changed. As the first field gets changed the NF (number of fields) and the zero field ($0) get re-calculated transforming all repeated white-space to one space. That is: lines that have multiple adjacent spaces (and tabs) are coalesced into a single space.

Simply:

awk '$1=NR "."'

This will change the first field (first text before a blank space) to the line (record) number and a dot. Since the record number is always positive and greater than 0, the result of the assignment will always be true causing the line (record) to be printed with the first field changed. 

As the first field gets changed the NF (number of fields) and the zero field ($0) get re-calculated transforming all repeated white-space to one space. 

That is: lines that have multiple adjacent spaces (and tabs) are coalesced into a single space.

If multiple spaces need to be salvaged from awk processing, it becomes quite more convoluted (only GNU awk has patsplit):

awk '{
       n=patsplit($0,f,/[^ \t\n]+/,s);   # split the line storing whitespace.
       f[1] = NR "."                     # only change actually needed.
       for(i=0;i<=n;i++)                 # for all fields (and separators)
       {  
          printf("%s%s",f[i],s[i])       # print each field and separator.
       }
       print("")                         # cause the printing of an OFS
     }
    ' patsplitfile
Added coment from roaima.
Source Link
user232326
user232326

Simply:

awk '$1=NR "."'

This will change the first field (first text before a blank space) to the line (record) number and a dot. Since the record number is always positive and greater than 0, the result of the assignment will always be true causing the line (record) to be printed with the first field changed. As the first field gets changed the NF (number of fields) and the zero field ($0) get re-calculated transforming all repeated white-space to one space. That is: lines that have multiple adjacent spaces (and tabs) are coalesced into a single space.

Simply:

awk '$1=NR "."'

This will change the first field (first text before a blank space) to the line (record) number and a dot. Since the record number is always positive and greater than 0, the result of the assignment will always be true causing the line (record) to be printed with the first field changed.

Simply:

awk '$1=NR "."'

This will change the first field (first text before a blank space) to the line (record) number and a dot. Since the record number is always positive and greater than 0, the result of the assignment will always be true causing the line (record) to be printed with the first field changed. As the first field gets changed the NF (number of fields) and the zero field ($0) get re-calculated transforming all repeated white-space to one space. That is: lines that have multiple adjacent spaces (and tabs) are coalesced into a single space.

Source Link
user232326
user232326

Simply:

awk '$1=NR "."'

This will change the first field (first text before a blank space) to the line (record) number and a dot. Since the record number is always positive and greater than 0, the result of the assignment will always be true causing the line (record) to be printed with the first field changed.