0
Currently I'm using AWK to find and replace a portion of the first three occurrences of a string. The string is formatted as such, and there are many of these strings in the file:
func(tempID="39849235",count='12');
Using this link, I was able to find a method of using AWK to find and replace the first three instances of the string. I changed it to what I needed it to do, and a snippet of my script is below:
id=12349876
awk 'BEGIN {matches=0}
matches < 3 && /.*tempID.*/ { sub(/tempID="[0-9]+"/,"tempID='"$id"'"); matches++ }
{ print $0 }' filName.py >filName.py.changed
The goal of the above code is to match on any line containing tempID and replace the number that is assigned to tempID with a value held in a variable named $id
. The find and replace works well, but the one issue I seem to have is that no matter how I structure it, the output prints the $id without quotes. I've tried escaping quotes and putting single ticks, but regardless the line is changed to:
func(tempID=39849235,count='12');
I've tried removing the double quotes around the replace portion and structuring it as tempID="$id"
, but unfortunately this just replaces the ID number with the string $id
.
Please let me know if there is a way to find and replace the tempID value and surround the value with quotes. I'm not stuck with AWK, so any other method with any other utility such as sed would work fine as well.
Thank you very much! The awk -v was just what I needed! – AndreasKralj – 2018-12-03T23:07:12.573