Solved

conditional insert character with sed or awk

Posted on 2003-11-03
6
1,397 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
6 Comments
 
LVL 7

Expert Comment

by:glassd
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 7

Expert Comment

by:glassd
Comment Utility
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
Comment Utility
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
Comment Utility
perl -e 'undef $/;$x=<>;$x=~s/NIL=\s+SMTU/NIL=999\nSMTU/g;print $x' your-file
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now