Inserting a line to a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting a line to a file
# 1  
Old 06-07-2014
Linux Inserting a line to a file

Hi,

consider a file called mobile.txt as follows:
Quote:
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
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).
Quote:
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 help me out with this script.
# 2  
Old 06-07-2014
Code:
$ 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

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

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


Last edited by Akshay Hegde; 06-07-2014 at 12:15 PM.. Reason: to add END block and fix old
# 3  
Old 06-07-2014
Kindly try for this input with the same code you given above:
Code:
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:
Code:
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):
Code:
awk -F, 'function dothis(last){
              if(f && /Type:/ || last)
                {
                    f  = 0
                    if(last)
                    {
                        print p RS $0 
                        next
                    }
                    else    print p
                } 
                  }


Last edited by Gautham; 06-07-2014 at 12:00 PM.. Reason: Changed the format, added changes
# 4  
Old 06-07-2014
I just fixed original solution try, so you want to update plan only for first found pattern only ?
# 5  
Old 06-07-2014
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..

Last edited by Gautham; 06-07-2014 at 12:17 PM.. Reason: additional info
# 6  
Old 06-07-2014
Quote:
Originally Posted by Gautham
Kindly try for this input with the same code you given above:
Code:
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 output is coming as given below:
Code:
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

In Final output
Code:
Type:lovers,Loc=South  --> 1
003,PLAN3,60Days 
004,PLAN4,60Days 
005,PLAN5,60Days 
006,PLAN6,60Days  <-- You need new line here but
Type:lovers,Loc=South  --> 2
003,PLAN3,60Days 
004,PLAN4,60Days 
005,PLAN5,60Days   <-- You don't want new line here right ?
Type:lovers,Loc=East 
003,PLAN3,60Days 
004,PLAN4,60Days

Did you try my fix in #2 ?
# 7  
Old 06-07-2014
Yes sir, i tried your #2:

For this input:
Code:
 
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:

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


Last edited by Gautham; 06-07-2014 at 12:35 PM.. Reason: changed format
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