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=                                                  
                       










ardaumutAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

glassdCommented:
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.

0
ardaumutAuthor Commented:
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.        
                                                           
 
0
glassdCommented:
Ah sorry.

In awk, quick and dirty:

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

This will not touch a line unless the following line starts with SMTU. However it will add "999" to any line which is followed by an SMTU line, whether it ends in "=" or not.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

glassdCommented:
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.
0
HamdyHassanCommented:
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
0
ahoffmannCommented:
perl -e 'undef $/;$x=<>;$x=~s/NIL=\s+SMTU/NIL=999\nSMTU/g;print $x' your-file
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.