Inserting a line to a file

Hi,

consider a file called mobile.txt as follows:

For type lovers, add a new line at the end of it by copying its previous line and add a +1 to the field1, field2

Additionally, there are only 3 plans available to lovers type, so it should not work for lovers type already having 3 lines under it (no need bother about what PLAN). Here it should copy "004,PLAN4,60Days" from lovers type with Loc=South and add +1 to field1 and field2; place that line to next of it.

Output: (highlighted will be the line added to this file).

Kindly help me out with this script.

$ cat file
Type:family,Loc=North
001,PLAN1,30Days
002,PLAN2,25Days
Type:lovers,Loc=South
003,PLAN3,60Days
004,PLAN4,60Days
Type:friends,Loc=West
007,PLAN7,45Days
Type:lovers,Loc=East
003,PLAN3,60Days
004,PLAN4,60Days
005,PLAN5,60Days 
$ awk -F, 'k<c && f && /Type:/{print p;f=0;++k} { if($0 ~ search)f=1;else p=sprintf("%03d%s%s%s%s",$1+1,FS,"PLAN"$1+1,FS,$3)}1;END{if(f)print p}' search='Type:lovers,Loc=South' c='1' file
Type:family,Loc=North
001,PLAN1,30Days
002,PLAN2,25Days
Type:lovers,Loc=South
003,PLAN3,60Days
004,PLAN4,60Days
005,PLAN5,60Days
Type:friends,Loc=West
007,PLAN7,45Days
Type:lovers,Loc=East
003,PLAN3,60Days
004,PLAN4,60Days
005,PLAN5,60Days 

Kindly try for this input with the same code you given above:

Type:lovers,Loc=South
003,PLAN3,60Days
004,PLAN4,60Days
005,PLAN5,60Days
Type:lovers,Loc=South
003,PLAN3,60Days
004,PLAN4,60Days
Type:lovers,Loc=East
003,PLAN3,60Days
004,PLAN4,60Days

The output is coming as given below:

Type:lovers,Loc=South 
003,PLAN3,60Days 
004,PLAN4,60Days 
005,PLAN5,60Days 
006,PLAN6,60Days 
Type:lovers,Loc=South 
003,PLAN3,60Days 
004,PLAN4,60Days 
Type:lovers,Loc=East 
003,PLAN3,60Days 
004,PLAN4,60Days

There is a small change to the code(i'm highlighting it):

awk -F, 'function dothis(last){
              if(f && /Type:/ || last)
                {
                    f  = 0
                    if(last)
                    {
                        print p RS $0 
                        next
                    }
                    else    print p
                } 
                  }

I just fixed original solution try, so you want to update plan only for first found pattern only ?

No, there can be only 3 lines present in such pattern, if there are already 3 lines under that pattern then it should skip such found patterns. But what i can surely tell is, there will be atleast 2lines under such patterns, i needed to add another line by incrementing it ( for all 'n' found patterns in a file ).

Hope you get it..

Did you try my fix in #2 ?

Yes sir, i tried your #2:

For this input:

 
Type:lovers,Loc=South-->1 
003,PLAN3,60Days 
004,PLAN4,60Days
005,PLAN5,60Days 
Type:lovers,Loc=South -->2 
003,PLAN3,60Days 
004,PLAN4,60Days 
Type:lovers,Loc=East 
003,PLAN3,60Days 
004,PLAN4,60Days

The fix code gives me this output:

Type:lovers,Loc=South 
003,PLAN3,60Days 
004,PLAN4,60Days 
005,PLAN5,60Days 
006,PLAN6,60Days 
Type:lovers,Loc=South 
003,PLAN3,60Days 
004,PLAN4,60Days 
Type:lovers,Loc=East 
003,PLAN3,60Days 
004,PLAN4,60Days 
005,PLAN5,60Days

Expected output:

Type:lovers,Loc=South -->1 
003,PLAN3,60Days 
004,PLAN4,60Days 
005,PLAN5,60Days                              //no add line required here
Type:lovers,Loc=South -->2 
003,PLAN3,60Days 
004,PLAN4,60Days
005,PLAN5,60Days                             //added line required here
Type:lovers,Loc=East 
003,PLAN3,60Days 
004,PLAN4,60Days

I am confused :slight_smile:

Okay Goutam, Answer for post #7

$ awk -F, 'f && /Type:/{if(count==2){print p}f=count=0}{ if($0 ~ search){f=1} else{if(f){p=sprintf("%03d%s%s%s%s",$1+1,FS,"PLAN"$1+1,FS,$3);count++}}}1;END{if(f && count==2)print p}' search='Type:lovers,Loc=South'  file

Yes it works fine :). But still there is something missing in it, as in that input given #7 the last type is with East Location so no issues.

Kindly check with this input:

Type:lovers,Loc=South
003,PLAN3,60Days
004,PLAN4,60Days
005,PLAN5,60Days
Type:lovers,Loc=East
003,PLAN3,60Days
004,PLAN4,60Days
Type:lovers,Loc=South
003,PLAN3,60Days
004,PLAN4,60Days
005,PLAN5,60Days

There should not be any changes to above input.

It won't work as it needed to be. I'm checking with all possibilities for a reliable solution.

Okay check all possibilities, it works if you need explanation to understand code read this commented version of same code in post #8

awk -F, '
	 # Previously we found actual pattern and current line pattern is again type
	 f && /Type:/{
			# if lines between pattern is 2 ?
			if(count==2)
			  {
			     # print line defined in variable p
			     print p
			  }
			 
			# Reset variable
			     f = count = 0
                     }
                     { 
			# Search pattern mentioned in variable search
			 if($0 ~ search)
			    {
				# Set variable f
				f=1
                            } 
		        else{
				# pattern not found in current line but previous found
				# pattern is which we are looking for..
				if(f)
				{
				  # increment counter and define new line
				  p=sprintf("%03d%s%s%s%s",$1+1,FS,"PLAN"$1+1,FS,$3)
				  count++
				}
                            }
                    }1
		 END{
			if(f && count==2)
				print p
		    }
          ' search='Type:lovers,Loc=South' file

Fine Fine Fine !!!

Sir, I have a doubt in this. The code checks lines present between two set of 'Type:' pattern, like if there is no 'Type:' in second set and if its not the same with all sets present in the file.

How to overcome this ?