-1

I have to add compiler directives before class declaration like :

`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 expression #1, char 38: invalid reference \2 on `s' command's RHS

I have seen Using sed, Insert a line below (or above) the pattern?. Here Kamal's answer solves only the part of the problem as I need to capture "mipi_rffe_scoreboard" and change it to uppercase

Community
  • 1
  • 1
Chandan Choudhury
  • 323
  • 2
  • 3
  • 11
  • Please take care with your formatting. To highlight a block of code, you can select it and use Ctrl-k, or click the "Code Sample" button. – Tom Fenech Oct 27 '14 at 18:47
  • 1
    @anubhava I was reluctant to edit as it is unclear whether the "\" and "**" are part of the input. Hopefully Chandan will edit to clarify. – Tom Fenech Oct 27 '14 at 18:51
  • I was trying to highlight those with bold font.I am getting `ifndef and `define using sed -i '/pattern/i \ line1 \ line2' inputfile but need the MIPI_RFFE_SCOREBOARD__SV also – Chandan Choudhury Oct 27 '14 at 18:57
  • 1
    It is not possible to combine formatting with code samples. If you want to draw attention to a specific part, you can mention it in the question. Any characters that are not in your original input should be removed. – Tom Fenech Oct 27 '14 at 18:59
  • Also include your expected output. – anubhava Oct 27 '14 at 19:01
  • @Tom No need I think it looks okay now. – Chandan Choudhury Oct 27 '14 at 19:04
  • @anubhava my file has only : class mipi_rffe_scoreboard extends uvm_scoreboard; line at present I need to make it like : `ifndef MIPI_RFFE_SCOREBOARD__SV `define MIPI_RFFE_SCOREBOARD__SV class mipi_rffe_scoreboard extends uvm_scoreboard; – Chandan Choudhury Oct 27 '14 at 19:08
  • Did you try the code I suggested? Let me know if you're having any problems with it. – Tom Fenech Oct 28 '14 at 00:30

1 Answers1

1

You could run this perl one-liner on your 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 by a word and prepends the two lines. \U is used to transform the match to uppercase and \E to end the transformation.

Testing it 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 fairly confident that this 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, which allows you to use ( ) to capture patterns. Normally, you would have to escape them using \( \).

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141