Menu

#479 Counting fields from the end of the record

None
closed-accepted
nobody
None
5
2022-01-15
2018-09-11
Mikhail T.
No

In a number of situations, one may have to deal with files containing variable number of fields on each line. Often enough the fields one wants to plot can be described by their position from the end, rather than the beginning of the line.

It would be quite natural to plot "foo.txt" using 1:-2 -- except Gnuplot rejects the -2 as invalid instead of interpreting it as the next to last field of every record.

Discussion

  • Ethan Merritt

    Ethan Merritt - 2018-09-11

    Note that negative column numbers already have special meaning. -2 means "the index number of the current data set within a file that contains multiple data sets".
    So your suggestion is reasonable but would have to use some other convention.

     
    • Mikhail T.

      Mikhail T. - 2018-09-11

      Oh, I see. Well, maybe, introducing awk's concept of NF would help? As in: plot "foo.txt" using 1:(NF - 2)?

       
  • Ethan Merritt

    Ethan Merritt - 2021-03-12

    I don't know why it took me so long to figure out how to do this correctly. After several attempts that didn't work acceptably, there is now an implementation in the development version and queued for 5.4.2:

    In the context of a using specifier, the character sequence $# evaluates to the total number of fields available in the current line of data.

    # Plot final column of data from each line of the file:
    plot FOO using 0:(column($#))
    
    # Plot the final column as a function of the last-but-one-column
    plot FOO using (column($# - 1)) : (column($#))
    
     
  • Ethan Merritt

    Ethan Merritt - 2021-03-12
    • status: open --> pending-accepted
    • Group: -->
     
  • Ethan Merritt

    Ethan Merritt - 2022-01-15
    • status: pending-accepted --> closed-accepted
     

Log in to post a comment.

MongoDB Logo MongoDB