EDIT: To save output into Input_file(s) itself without a GNU awk try following(but make sure you run my 1st solution to see if output is looking correct).
Let's say we have scripts whose values we need to change.
-rw-rw-r-- 1 ubuntu ubuntu 83 Dec 8 14:34 file2.ksh
-rw-rw-r-- 1 ubuntu ubuntu 83 Dec 8 14:34 file1.ksh
Our awk script's name is script.ksh to make sure this script is NOT coming under its own radar :)
cat script.ksh
awk '
FNR==1{
close(out)
out="out"++count
rename=(rename?rename ORS:"") "mv " out OFS FILENAME
}
match($0,/\$[0-9]+/){
before=substr($0,1,RSTART)
value=substr($0,RSTART+1,RLENGTH)
rest=substr($0,RSTART+RLENGTH)
if(value<9){
value++
}
print before value rest > (out)
}
END{
if(rename){
system(rename)
}
}
' file*sh
Now when we run the above script by doing ./script.ksh and we can one of the file named file1.ksh see its contents have been changed now as follows.
BEFORE:
cat file1.ksh
hv_bus_date="CAST('$1' AS DATE FORMAT 'YYYYMMDD')"
hv_octs_sys_wid=$2
hv_act_id=$3
AFTER:
cat file1.ksh
hv_bus_date="CAST('$2' AS DATE FORMAT 'YYYYMMDD')"
hv_octs_sys_wid=$3
hv_act_id=$4
1st solution: Could you please try following, considering that your script names are ending with .sh extensions and this will NOT save output into Input_file(s), it will print output on terminal only for you to check output if its coming fine.
awk '
match($0,/\$[0-9]+/){
before=substr($0,1,RSTART)
value=substr($0,RSTART+1,RLENGTH)
rest=substr($0,RSTART+RLENGTH)
if(value<9){
value++
}
print before value rest
}
' *sh
2nd solution(With only newer versions of GNU awk): Once you are happy with results of above command and if you have gawk command then try following, this should save output into Input_file(s) itself. IMHO this needs gawk 4.1.0 + version.
awk -i inplace -v INPLACE_SUFFIX=.bak '
match($0,/\$[0-9]+/){
before=substr($0,1,RSTART)
value=substr($0,RSTART+1,RLENGTH)
rest=substr($0,RSTART+RLENGTH)
if(value<9){
value++
}
print before value rest
}
' *sh
printf 's/\$%d/$%d/g\n' {1,{2..8}{,},9} | tac | sed -i -f - file1 file2 .... Don't try this without backuping inputs though, it's not really reliable