Solved

conditional insert character with sed or awk

Posted on 2003-11-03
6
1,407 Views
Last Modified: 2013-12-26
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=                                                  
                       










0
Comment
Question by:ardaumut
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 7

Expert Comment

by:glassd
ID: 9671413
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
 

Author Comment

by:ardaumut
ID: 9671626
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
 
LVL 7

Accepted Solution

by:
glassd earned 250 total points
ID: 9671865
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:glassd
ID: 9671926
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
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 9673115
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 9674384
perl -e 'undef $/;$x=<>;$x=~s/NIL=\s+SMTU/NIL=999\nSMTU/g;print $x' your-file
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
xyBalance chalenge 58 102
My project did see openJDK that I installed. What could be the problem 7 161
Unix Command -- Challenging  question 7 101
Change to event 1 129
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question