Resolved Renumbering Java constants in text file

Discussion in 'Mac Programming' started by wrldwzrd89, Jul 5, 2011.

  1. wrldwzrd89, Jul 5, 2011
    Last edited: Jul 5, 2011

    macrumors G4

    wrldwzrd89

    Joined:
    Jun 6, 2003
    Location:
    Solon, OH
    #1
    I have a text file of a bunch of constants in Java format:
    Code:
    public static final int CONSTANT_ZIPPY = 3;
    public static final int CONSTANT_FOO = 0;
    public static final int CONSTANT_BAR = 1;
    public static final int CONSTANT_BAZ = 2;
    
    What I'd like to do is produce a file with all the numbers changed to the line number minus 1, like so:
    Code:
    public static final int CONSTANT_ZIPPY = 0;
    public static final int CONSTANT_FOO = 1;
    public static final int CONSTANT_BAR = 2;
    public static final int CONSTANT_BAZ = 3;
    
    My Google-fu is weak today, and my brain can't make sense of the Google search results. I'm sure there's a way to do this with awk, sed, perl, or some other UNIXy commands, but I cannot figure out how. Help? :confused:

    EDIT: Just found a solution. awk to the rescue!
    Code:
    awk '/[0-9]*/ {print $1 " " $2 " " $3 " " $4 " " $5 " " $6 " " FNR - 1 ";"}' filename.txt
    
    ... does exactly what I want.
     

Share This Page