Writing a dummy line!! | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Writing a dummy line!!

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-21-2013
begin_2013 begin_2013 is offline
Registered User
 
Join Date: Aug 2013
Last Activity: 26 March 2014, 4:27 AM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Writing a dummy line!!

Hi,
I have a file whihc looks like

Code:
file_1 100 200
file_2 200 300
file_4 400 500

as the file_3 is missing so I want to replace it by

Code:
file_3 0 0

the final output would look like

Code:
file_1 100 200
file_2 200 300
file_3 0 0
file_4 400 500

Any help is highly appreciated.

Regards,

Last edited by Franklin52; 08-21-2013 at 06:56 AM.. Reason: Code tags
Sponsored Links
    #2  
Old 08-21-2013
krishmaths krishmaths is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 30 June 2014, 4:32 AM EDT
Location: Chennai, India
Posts: 443
Thanks: 22
Thanked 70 Times in 68 Posts

Code:
awk '{if("file_"++a==$1) {print} else {while("file_"a!=$1) {print "file_" a++ " 0 0"};print } }'  filename

Shortened version:

Code:
awk '{while("file_"a+1!=$1) {print "file_" a+1 " 0 0";a++} {print;++a}}' filename


Last edited by krishmaths; 08-21-2013 at 06:45 AM..
Sponsored Links
    #3  
Old 08-21-2013
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 23 September 2014, 12:09 PM EDT
Location: Houston, Texas, USA
Posts: 675
Thanks: 0
Thanked 203 Times in 195 Posts
try also:

Code:
awk '$2!=l+1 {for (i=l+1; i<$2; i++) print $1"_"++l,0,0} {print; l=$2}' FS="[_ ]" infile

    #4  
Old 08-21-2013
RavinderSingh13 RavinderSingh13 is online now
Registered User
 
Join Date: May 2013
Last Activity: 1 October 2014, 6:13 AM EDT
Location: Chennai
Posts: 679
Thanks: 193
Thanked 137 Times in 134 Posts
Hello,

Could you please try the following also. Here lets say file name file_entry have all the fields as shown by you.


Code:
awk 'NR==3 {print"file_3 0 0"} 1' file_entry


Output will be as follows.


Code:
file_1 100 200
file_2 200 300
file_3 0 0
file_4 400 500
$


Thanks,
R. Singh
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How do i find the first number in each line and insert dummy string into the missing columns? Szaffy Shell Programming and Scripting 5 11-01-2011 12:27 PM
writing string to specific line aliahsan81 Shell Programming and Scripting 3 10-29-2009 11:48 AM
writing data in a text file at particular line punitpa Shell Programming and Scripting 2 04-14-2009 01:45 PM
Writing line to a file dateez Shell Programming and Scripting 7 04-12-2009 09:01 PM
writing into one line Satyak Shell Programming and Scripting 2 10-20-2008 10:01 AM



All times are GMT -4. The time now is 06:17 AM.