Hello, I need a smart sed command that can take any of the following two as an input and give below mentioned output. As you can see, I am trying to convert some C code
INPUT:
struct abc_sample1 {
char myString[32];
UINT16 myValue1[10];
UINT64 myValue2;
};
typedef struct {
char myString[32];
UINT16 myValue1[10];
UINT64 myValue2;
} abc_sample1;
OUTPUT:
ABC_DataDesc_T abc_sample1_desc[] = {
char myString[32];
UINT16 myValue1[10];
UINT64 myValue2;
};
Thanks a bunch
This is a candidate for lex & yacc if you need to do a lot of it. .
awk ' BEGIN {found=0;
keep=""
abc_found=0;
replacement=sprintf("%s\n%s\n%s\n%s\n%s\n",
"ABC_DataDesc_T abc_sample1_desc[] = {",
"char myString[32];",
"UINT16 myValue1[10];",
"UINT64 myValue2;",
"};" )
}
{
if( index($0, "}")> 0 && found==1 ){
if($0 ~ /abc_sample1/) {abc_found=1}
if(abc_found==1) { print replacement }
else {printf"%s%s\n", keep, $0}
keep=""
found=0
abc_found=0
continue
}
if($0 ~ /struct/ ){ found=1 }
if($0 ~ /abc_sample1/) {abc_found=1}
if(found==1) { keep=sprintf("%s%s\n", keep, $0) }
else {print $0}
} ' filename.c
input
struct abc_sample1 {
char myString[32];
UINT16 myValue1[10];
UINT64 myValue2;
};
typedef int fd;
typedef struct{
char customer_number [9];
char premise_number [7];
char service_number [4];
char bill_no [6];
char record_type [8];
}SORT_KEY;
typedef struct {
SORT_KEY sort_key;
char service_category[4];
char conversion_factor [8];
char meter_number [10];
char days_of_service [3];
char meter_previous_read_date [11];
char meter_previous_read_value [10];
char meter_present_read_date [11];
char meter_present_read_value [10];
char meter_consumption_value [10];
char meter_read_type [4];
char meter_multiplier [8];
char meter_read_route [8];
char units_of_leakage [10];
char chng_out_meter_number [10];
char chng_out_DOS [3];
char chng_out_previous_read_date [11];
char chng_out_previous_read_value [10];
char chng_out_present_read_date [11];
char chng_out_present_read_value [10];
char chng_out_consumption_value [10];
char chng_out_read_type [4];
char chng_out_multiplier [8];
char previous_printed_date [11];
} my_urrshis_t;
typedef struct {
char myString[32];
UINT16 myValue1[10];
UINT64 myValue2;
} abc_sample1;
output
ABC_DataDesc_T abc_sample1_desc[] = {
char myString[32];
UINT16 myValue1[10];
UINT64 myValue2;
};
typedef int fd;
typedef struct{
char customer_number [9];
char premise_number [7];
char service_number [4];
char bill_no [6];
char record_type [8];
}SORT_KEY;
typedef struct {
SORT_KEY sort_key;
char service_category[4];
char conversion_factor [8];
char meter_number [10];
char days_of_service [3];
char meter_previous_read_date [11];
char meter_previous_read_value [10];
char meter_present_read_date [11];
char meter_present_read_value [10];
char meter_consumption_value [10];
char meter_read_type [4];
char meter_multiplier [8];
char meter_read_route [8];
char units_of_leakage [10];
char chng_out_meter_number [10];
char chng_out_DOS [3];
char chng_out_previous_read_date [11];
char chng_out_previous_read_value [10];
char chng_out_present_read_date [11];
char chng_out_present_read_value [10];
char chng_out_consumption_value [10];
char chng_out_read_type [4];
char chng_out_multiplier [8];
char previous_printed_date [11];
} my_urrshis_t;
ABC_DataDesc_T abc_sample1_desc[] = {
char myString[32];
UINT16 myValue1[10];
UINT64 myValue2;
};
Thanks a lot for your input and code. As I see following two inputs
Input pattern is
^.*struct $1 { multi-line structure } $2;
Output is
ABC_DataDesc_T $1/$2_desc { multi-line structure };
where
- only one out of $1 and $2 is non-empty
- Anything before string "struct" (including struct) is replaced by a static string ABC_DataDesc_T (sorry about the confusing name, which made it look that it depends on name of struct)
- everything between multi-line matching braces { } need to remain intact.
- ; at the end
I will see if I can figure out lex/yacc