Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

conditional insert character with sed or awk

Posted on 2003-11-03
6
Medium Priority
?
1,433 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

618 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