PDA

View Full Version : [Resolved] Renumbering Java constants in text file




wrldwzrd89
Jul 5, 2011, 01:42 PM
I have a text file of a bunch of constants in Java format:

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:

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!

awk '/[0-9]*/ {print $1 " " $2 " " $3 " " $4 " " $5 " " $6 " " FNR - 1 ";"}' filename.txt

... does exactly what I want.