regex - Add/Insert lines before pattern such that lines to be added have part of the pattern in uppercase -



regex - Add/Insert lines before pattern such that lines to be added have part of the pattern in uppercase -

i have add together compiler directives before class declaration :

`ifndef mipi_rffe_scoreboard__sv `define mipi_rffe_scoreboard__sv class mipi_rffe_scoreboard extends uvm_scoreboard;

i tried :

sed 's/class (\w+)(.*)/\`ifndef \u\1__sv\2/' mipi_rffe_scoreboard.sv

which gives :

sed: -e look #1, char 38: invalid reference \2 on `s' command's rhs

i have seen using sed, insert line below (or above) pattern?. here kamal's reply solves part of problem need capture "mipi_rffe_scoreboard" , alter uppercase

you run perl one-liner on file:

perl -pe 's/(class (\w+))/`ifndef \u$2\e__sv\n`define \u$2\e__sv\n\n$1/'

it captures lines containing "class" followed word , prepends 2 lines. \u used transform match uppercase , \e end transformation.

testing out:

$ perl -pe 's/(class (\w+))/`ifndef \u$2\e__sv\n`define \u$2\e__sv\n\n$1/' <<<"class mipi_rffe_scoreboard extends uvm_scoreboard;" `ifndef mipi_rffe_scoreboard__sv `define mipi_rffe_scoreboard__sv class mipi_rffe_scoreboard extends uvm_scoreboard;

alternatively (tested on gnu sed, i'm confident won't work on other versions):

sed -r 's/class (\w+)/`ifndef \u\1\e__sv\n`define \u\1\e__sv\n\n&/'

-r enables extended regex mode, allows utilize ( ) capture patterns. normally, have escape them using \( \).

regex perl shell awk sed

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -