LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 02-18-2008, 03:41 AM   #1
rahulruns
LQ Newbie
 
Registered: Feb 2008
Posts: 21

Rep: Reputation: 0
Post SHELL Script to insert lines after certain Intervals


I need to write a SHELL Scripts for inserting a particular line at certain interval to a file. For suppose the after every line 13 I need to insert a line in a particular file.

I am using Korn SHELL.
 
Old 02-18-2008, 04:04 AM   #2
indiancosmonaut
Member
 
Registered: Feb 2007
Posts: 65

Rep: Reputation: 15
Hi rahul,

Use the following code snippet...

Code:
#!/usr/bin/ksh

while read line
do
   # echo each line in yourfile.
   echo "$line" 

   <<your code to check for the thirteenth line and insert your line>>

done < ./yourfile > ./outfile
This will create your outfile in the pwd.

Kind regards,

indiancosmonaut
 
Old 02-18-2008, 04:22 AM   #3
rahulruns
LQ Newbie
 
Registered: Feb 2008
Posts: 21

Original Poster
Rep: Reputation: 0
Well Thank you for your information. Actually this I too had in my mind but the problem is this code needs an increment where 13 is the increment factor but the number of lines in the file is not known. Also I do not get the point to echo each line. The main part is the code to check the thirteenth line repetatively. This is a problem because when I use awk the NR does not take any variable as an input. It should be a number. So how to count lines then and insert after thirteenth line, because I need to repeat this activity after every 13th line.

Hope you understood what I said?

Rahul
 
Old 02-18-2008, 04:42 AM   #4
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
awk '{print $0; NR%13==0 {print "inserted line"}' file

I found your original description confusing:
Quote:
For suppose the after every line 13 I need to insert a line in a particular file.
Instead of "after every 13th line I want to insert a line.

If you want to insert a blank line after every thirteenth line, then replace "inserted line" with "".

Last edited by jschiwal; 02-18-2008 at 04:47 AM.
 
Old 02-18-2008, 05:22 AM   #5
rahulruns
LQ Newbie
 
Registered: Feb 2008
Posts: 21

Original Poster
Rep: Reputation: 0
awk: cmd. line:1: {print $0; NR%13==0 {print "++++++++++++++++++++++++++++++"}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:2: (END OF FILE)
awk: cmd. line:2: syntax error


This is the error what I get when I run the command u have provided
I have even inserted other braces then also the error is same

awk: cmd. line:1: {print $0; NR%13==0 {print "++++++++++++++++++++++++++++++"}}
awk: cmd. line:1: ^ syntax error

Rahul

Last edited by rahulruns; 02-18-2008 at 05:24 AM.
 
Old 02-18-2008, 05:25 AM   #6
rahulruns
LQ Newbie
 
Registered: Feb 2008
Posts: 21

Original Poster
Rep: Reputation: 0
Well the above output does not save here correctly. The syntax error is at the brace of the second print statement in both the cases.
 
Old 02-18-2008, 05:29 AM   #7
rahulruns
LQ Newbie
 
Registered: Feb 2008
Posts: 21

Original Poster
Rep: Reputation: 0
Quote:
awk: cmd. line:1: {print $0; NR%13==0 {print "++++++++++++++++++++++++++++++"}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:2: (END OF FILE)
awk: cmd. line:2: syntax error
This is the output I get
 
Old 02-19-2008, 12:28 AM   #8
indiancosmonaut
Member
 
Registered: Feb 2007
Posts: 65

Rep: Reputation: 15
I used the following command.

Code:
awk ' { print $0; if (NR%13==0) {print "inserted line"}}' file
Hi jschiwal,

Are you using some different version of awk? Your command is not working on my AIX 5.3.0.

Kind regards,

indiancosmonaut
 
Old 02-19-2008, 04:26 AM   #9
rahulruns
LQ Newbie
 
Registered: Feb 2008
Posts: 21

Original Poster
Rep: Reputation: 0
Well thanks for the formatting got my answer. Well can you specify what does NR%13==0 mean
 
Old 02-26-2008, 01:21 AM   #10
indiancosmonaut
Member
 
Registered: Feb 2007
Posts: 65

Rep: Reputation: 15
NR here means "Number of Records". So whenever NR is a multiple of 13, it would go into the `if block`.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script: insert line in a file noir911 Programming 6 02-04-2008 10:42 PM
Shell Script: want to insert values in database when update script runs ring Programming 2 10-25-2007 10:48 PM
insert lines in a function using shell script shyamdey Programming 1 08-30-2006 07:48 AM
Shell script for insert ip address into an ordered list of IPs inTrouble? Linux - Newbie 2 10-27-2003 02:21 AM


All times are GMT -5. The time now is 01:41 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration