2

I have a lot script in which it is reading the input parameter. I need to increment all the input parameter by 1. So in case if it 1 it need to be 2 & 2 need to 3 etc. Is there any unix command which can replace this in one go for all scripts instead of going to each scripts and doing it manually. I am new to Unix not sure if there is any way to do it. Below is the example. Appreciate any help on this.

Before Changes

#!/bin/ksh
hv_bus_date="CAST('$1' AS DATE FORMAT 'YYYYMMDD')"
hv_octs_sys_wid=$2
hv_act_id=$3

After Changes

#!/bin/ksh
hv_bus_date="CAST('$2' AS DATE FORMAT 'YYYYMMDD')"
hv_octs_sys_wid=$3
hv_act_id=$4
4
  • Is it possible for those scripts to take more than 9 positional parameters? Commented Dec 8, 2019 at 12:15
  • It will be below 10 parameters Commented Dec 8, 2019 at 12:34
  • 1
    Then I'd go with something like 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 Commented Dec 8, 2019 at 12:36
  • Hi Oguz, I tried using this on multiple files..it is working. Thanks for your reply. Commented Dec 8, 2019 at 14:45

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Ravinder, I tried it by putting the correct file, now it is working..thanks a lot for your prompt response.
@RameshS, btw thanks for asking this question, I too will make a note of this EDIT program since it does it without GNU awk too, cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.