Inserting a line to a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting a line to a file
# 8  
Old 06-07-2014
I am confused Smilie

Quote:
Originally Posted by Gautham
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..
Okay Goutam, Answer for post #7

Code:
$ 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


Last edited by Akshay Hegde; 06-07-2014 at 12:48 PM..
# 9  
Old 06-07-2014
Yes it works fine Smilie. 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:

Code:
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.
# 10  
Old 06-07-2014
Okay check all possibilities, it works if you need explanation to understand code read this commented version of same code in post #8

Code:
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

# 11  
Old 06-07-2014
Fine Fine Fine !!!
# 12  
Old 06-09-2014
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 ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Inserting a blank line at the end of a .txt file?

Hi there, I am having this problem: a) I am uploading a txt file from windows (notepad) with some Gaussian 09 command lines; b) Gaussian needs in certain command files, that those files have a blank line at the end of the file! c) I open the command file with vi and no blank line at the of... (2 Replies)
Discussion started by: luismga
2 Replies

2. Shell Programming and Scripting

Inserting a new line into an already existing file

hello .. I am new to perl scripting, I have a text file, and would like to insert 3 new lines into the same file at different line numbers using perl scripting. Any Help regarding this will be very useful. say the file is sample.txt, contents are aaaaa bbbb ccccc dddd eeeee ffffffff... (4 Replies)
Discussion started by: hemalathak10
4 Replies

3. Shell Programming and Scripting

Inserting a line in a file after every alternate line

Friends , I have a large file and i need to insert a line after every line.I am actually unaware how to do it.Any help appreciated. My File control station *ATM* , qread $OSS.Jul13A.FI01 interval 1 intcount 1 control station *ATM* , qread $OSS.Jul13A.FI02 interval 1 intcount... (4 Replies)
Discussion started by: appu2176
4 Replies

4. Shell Programming and Scripting

Inserting IP in one line of a file

Hi, I have this line: ip=111.222.133.144,mac=00:16:3E:2A:08:3C,vifname=veth360','ip=10.2.3.4,vifname=veth360a' ^ | ------- I want to insert this IP 144.133.222.111 between "144"... (4 Replies)
Discussion started by: iga3725
4 Replies

5. Shell Programming and Scripting

Problem inserting text into file after specific line

this is utterly embarassing :( after posting here i revisited my files and found that when i used "vi" instead of a gui based editor, i suddenly found that the indentations were in fact wrong :( sorry about this :( (0 Replies)
Discussion started by: mocca
0 Replies

6. Shell Programming and Scripting

inserting line to a file

I have posted it previously but somehow could not delete the previous post.I felt i could not explain the problem statement well. Here t goes.I have a file say File1. Now i need a specific pattern from the lines to be added to the other line. File: red blue green ABC.txt@ABC END black... (1 Reply)
Discussion started by: ngupta
1 Replies

7. Shell Programming and Scripting

Need Help in Inserting a new line in a file using PERL

I need a perl script to find and replace a specific pattern in a file to a new line. BAsically I have a single line data in a file with 10 mb to 200 MB. I want to put a new line based on a specific pattern to open the file in Excel / Access. Following is the sample data in a file ... (1 Reply)
Discussion started by: portalfaq
1 Replies

8. Shell Programming and Scripting

inserting a new line in a file

I'm sure you guys have answered this elsewhere but I can't seem to find where so here goes. #!/bin/bash n=120 a=$(sed '120q;d' energy.xvg) while ;do a=$(sed $n'q;d' energy.xvg) echo "$a \n" > newfile n=$(($n+100)) done exit 0 that script should read the file energy.xvg, start at... (1 Reply)
Discussion started by: gelitini
1 Replies

9. Shell Programming and Scripting

comparing and inserting common line in other file

Hi, I have two files-- file1- file2:- i have to compare two files and where the uid is same i have to take the password and insert it above the telephone number. the output should be like this-- uid : 1418 common so insert the password thbs above the line telephonenumber. (2 Replies)
Discussion started by: namishtiwari
2 Replies

10. Shell Programming and Scripting

Inserting New Line in File using Sed ??

Dear All, I have a file called football where i have a list of 11 players each on different lines. I wish to add a name of another player on the first line. I have created a file called footballscript in vi writing the following sed command to achieve this ... cat football | sed -e '1 i\... (4 Replies)
Discussion started by: Mary_xxx
4 Replies
Login or Register to Ask a Question