Link to home
Start Free TrialLog in
Avatar of ardaumut
ardaumut

asked on

conditional insert character with sed or awk

hello everybody,
I have a data like above.
I want to insert a smallint character if a character field (like SMTU) exists after every "=" chacter.  I mean in 5th row, data is "17540 NIL=" and  "SMTU30 LTAA 010000" follows it. In this condition I can insert smallint character after "=" and data must be  
17540 NIL=999                                                    
SMTU30 LTAA 010000                                                            
.......
I tried every way but didn't solve. How can i solve it with awk or sed?

mustafa

SMTU14 LTAA 010000                                            
AAXX 01004                                                    
17521 32560 30000 10093 20089 40209 57008 83100 333 83830=    
17515 NIL=                                                    
17540 NIL=                                                    
SMTU30 LTAA 010000                                            
AAXX 01004                                                    
17050 32670 81806 10122 20100 40072 57012 8452/ 333            
84635 88460=                                                  
17128 31640 70000 11003 21019 39066 40172 56004 71011 84550    
333 84640 87360=                                              
17022 32670 61806 10144 20024 40218 58009 84260 333            
84834 86360 92438=                                            
SMTU30 LTAA 010000 CCA                                        
AAXX 01004                                                    
17350 11560 80606 10096 20065 40215 57006 69901 76100 85250 33385830 88358=                                                  
                       










Avatar of glassd
glassd

Not sure exactly what you want.

If you want to change ever occurence of "NIL =" at the end of a line you can do it with sed:
   sed 's/NULL =$/NULL = 999/' infile > outfile
The "$" represents "the end of the line".

With awk:
   awk '{if($0 ~ /NULL =$/) { print $0,"999"} else {print}}' infile > outfile

If the decision is based on the contents of the following line then awk may be better. Give more details and we'll look at it.

Avatar of ardaumut

ASKER

it doesn't depends on NILL. row can finish NILL or any data before "="
first of all, every row finishes "=" character. If a "SMTU" or equivalent character follows up to "=" character -in fact it is beginning of the next row- I can insert a 999 after "=" and before "SMTU".
For example
17022 32670 61806 10144 20024 40218 58009 84260 333            
84834 86360 92438=                                            
SMTU30 LTAA 010000 CCA                                        
AAXX 01004

data must be

17022 32670 61806 10144 20024 40218 58009 84260 333            
84834 86360 92438=999                                            
SMTU30 LTAA 010000 CCA                                        
AAXX 01004          
..........

But if smallint character follows up to "=" char, it doesn't need to change.
for example

17128 31640 70000 11003 21019 39066 40172 56004 71011 84550    
333 84640 87360=                                              
17022 32670 61806 10144 20024 40218 58009 84260 333            
84834 86360 92438=        
data will remain the same.        
                                                           
 
ASKER CERTIFIED SOLUTION
Avatar of glassd
glassd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Slight improvement. This will only change lines ending in "=":

nawk '
{
   if(NR>1 && a ~ /\=$/) {
      if($1 ~ /^SMTU/) {
         print a,"999"
      } else {
         print a
      }
   } else {
      print a
   }
}
{ a=$0 }
END{print a}' infile

I am using Solaris and therefore use nawk. Any version of awk should handle this okay.
mustafa :
If you want awk please try glassd's solution

If you want perl, check below
also let me know if you have any question with perl

===================
input file
===================
$  cat mustafa.txt    
17128 31640 70000 11003 21019 39066 40172 56004 71011 84550    
333 84640 87360=                                              
17022 32670 61806 10144 20024 40218 58009 84260 333            
84834 86360 92438=                                            
SMTU30 LTAA 010000 CCA                                        
AAXX 01004

===================
output file
===================
$ cat mustafa.out    
17128 31640 70000 11003 21019 39066 40172 56004 71011 84550    
333 84640 87360=                                              
17022 32670 61806 10144 20024 40218 58009 84260 333            
84834 86360 92438=                                            9999
SMTU30 LTAA 010000 CCA                                        
AAXX 01004

===================
source code of perl script
===================
$ cat parsa_mustafa.pl
#!/export/vol/bin/perl

#----------------------
# Define input file
#----------------------
$sourceFile="mustafa.txt";

#---------------------------------------------------
# Define output files
#---------------------------------------------------
$DATAFile="mustafa.out";

#----------------------
# Create File Handlers
#----------------------
open(IN,"$sourceFile");
open(OUTA,">$DATAFile");

#--------------------------------------------
# Declare variables  
#--------------------------------------------

while ($myline=<IN>){

        if ( $myline =~ /= *\n$/ )           # Check if line ends by =
        {
           $chop_line=$myline;        
           chop($chop_line);        
           $nextline = <IN> ;                     # read the next line
           if ( $nextline =~ /^SMTU/ )        # Check if starts with SMTU
           {
             print OUTA $chop_line ;
             print OUTA "9999\n" ;
             print OUTA $nextline ;
             next;
           }
           else
           {
             print OUTA $chop_line ;
             print OUTA "\n" ;
             print OUTA $nextline ;
             next;
           }
        }
        else
        {
           print OUTA $myline ;
        }
}

close(IN);
close(OUTA);



Note that because there is a spaces after "=", so 9999 appears after these spaces
let me know if you want to trim this spaces

Hamdy
perl -e 'undef $/;$x=<>;$x=~s/NIL=\s+SMTU/NIL=999\nSMTU/g;print $x' your-file