regex - Linux CLI change price (awk or sed?) -


i have price strings formatted $25.00 in various html files. use linux command line (bash, presumably awk or sed) increase each price dollar amount ($3 in case).

in short, need find $nn.00 , replace $(n+3)n.00

started put don't know how add 3 sed -r 's/([^$][0-9][0-9][.]00). ????' file.html

thanks!

sample data:

$ cat prices_file.html <p>$25.00</p><p>$78.00</p> <p>$2.00</p> <p>$101.00</p> 

solution perl:

$ perl -pi.bak -e 's/\$(\d+\.\d+)/sprintf("\$%.2f", $1 + 3)/eg' prices_file.html 

after:

$ cat prices_file.html <p>$28.00</p><p>$81.00</p> <p>$5.00</p> <p>$104.00</p> 

above example 1 of common perl use cases substitution. backup original file (in prices_file.html.bak) in case unwanted it.

what maybe not common evaluation modifier (s///e) allows execute arbitrary perl code in substitution. global modifier (s///g) tells perl replace occurrences (here in context of line, if remove g modifier if replace first price in 1st line of given sample data).

in sprintf("\$%.2f", $1 + 3) replacement, $1 refers matched group [(\d+\.\d+)].


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -