I have a file Builder.java
, with lines like:
public class Builder{
@Override
public void setCallId(long value) {
set4ByteField(value, 48);
setLogtype(1);
setVerify("ABAB");
}
public void setOriginCallId(long value) {
set8ByteField(value, 52);
}
public void setDateTimeYear(int value) {
set2ByteField(value, 60);
}
...
Then I want to replace only numbers > 50 to number+1, keeping all else as it was. Result:
public class Builder{
@Override
public void setCallId(long value) {
set4ByteField(value, 48);
setLogtype(1);
setVerifyflag("ABAB");
}
public void setOriginCallId(long value) {
set8ByteField(value, 53);
}
public void setDateTimeYear(int value) {
set2ByteField(value, 61);
}
....
}
I tried my best but wrote scripts which do not work, like:
cat Builder.java | awk -F'[,)]' '$2>50 {print $2+1}' > Builder.java
Best Answer
Your
awk
was almost right, but you want to alternate a field and then print the whole line. Also the output field separator is just removed and the missing comma and closing parentheses added manually:Where the
1
is always true, so always prints the line (for all lines) - note that it must be done after you altered the field. I added a match for/ByteField/
for more robustness.For replacing the file: The redundant
cat
and piping to a command that has the same file as output will break. Use other approaches. E.g.With GNU awk
With sponge
Or with help of a temporary file