The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-19-2008
Registered User
 

Join Date: Mar 2008
Posts: 9
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-19-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,459
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
input
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;
output
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;
};
Reply With Quote
  #3 (permalink)  
Old 03-19-2008
Registered User
 

Join Date: Mar 2008
Posts: 9
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
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0