![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to handle multiple rows in a file | ksmbabu | Shell Programming and Scripting | 2 | 05-14-2008 09:44 PM |
| Fast, flexible, calculating from the command line | iBot | UNIX and Linux RSS News | 0 | 03-11-2008 01:40 AM |
| find multiple file types and tar | manthasirisha | Shell Programming and Scripting | 9 | 03-21-2006 08:37 AM |
| Help needed in processing multiple variables in a single sed command. | stevefox | Shell Programming and Scripting | 5 | 12-15-2005 09:06 PM |
| File types help needed | brady9953 | Linux | 3 | 09-07-2003 04:32 AM |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
flexible sed command needed to handle multiple input types
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 |
| Forum Sponsor | ||
|
|
|
|||
|
This is a candidate for lex & yacc if you need to do a lot of it. .
Code:
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
Code:
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;
Code:
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 1. only one out of $1 and $2 is non-empty 2. 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) 3. everything between multi-line matching braces { } need to remain intact. 4. ; at the end I will see if I can figure out lex/yacc |
|||
| Google UNIX.COM |