LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-10-2018, 04:24 AM   #1
tiny.deluxe
LQ Newbie
 
Registered: Apr 2017
Posts: 6

Rep: Reputation: Disabled
insert line in file every 1000th line via sed or in vi


Hi all,
you helped me out once and I hope you can do it once again.
My problem:
I have a more or less big file with many update statements (about 590000)for execution in an Oracle database. Now I need to insert a "commit" line after every 1000th line.
Do you have any idea to do this with 'sed' or directly in 'vi'?

Thanks in advance for your help.

Best regards
Elke
 
Old 01-10-2018, 05:50 AM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 2,776
Blog Entries: 3

Rep: Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228
Some versions of sed can do it.

Code:
sed -e '0~5 s/$/\nfoo/' 
sed -e '0~5 afoo'
Check your manual page to see if the 'step' option is there:
first~step

Match every step'th line starting with line first. For example,
``sed -n 1~2p'' will print all the odd-numbered lines in the
input stream, and the address 2~5 will match every fifth line,
starting with the second. first can be zero; in this case, sed
operates as if it were equal to step. (This is an extension.)
Otherwise, if you need something portable, you could try awk

Code:
awk '{print;} !(++a%5) {print "foo";}'
Edit: the 'a' command is shorter than the 's' command

Last edited by Turbocapitalist; 01-10-2018 at 06:04 AM.
 
2 members found this post helpful.
Old 01-10-2018, 06:39 AM   #3
tiny.deluxe
LQ Newbie
 
Registered: Apr 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
@Turbocapitalist:
thanks a lot. The second advice (awk) did it. sed replaced each fifth line with "commit" instead of inserted it.

Best Regards
Elke
 
Old 01-10-2018, 06:42 AM   #4
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 2,776
Blog Entries: 3

Rep: Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228
Strange. sed should do the job. Which regex did you write? Or did you work with the 'a' command instead?
 
Old 01-10-2018, 11:34 AM   #5
tiny.deluxe
LQ Newbie
 
Registered: Apr 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
I used
sed -e '0~5 commit;' test.sql > test1.sql

and in test1.sql where the same amount of lines as in test.sql but each fifth row was replaced by commit.
 
Old 01-10-2018, 11:37 AM   #6
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 2,776
Blog Entries: 3

Rep: Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228
Ah. You missed the actual sed command: 'a'
Code:
sed -e '0~5 acommit;' test.sql > test1.sql
See 'man sed' for the details. You've also figured out how to increase the steps to 1000 or more, I presume?
 
Old 01-11-2018, 05:51 AM   #7
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 601

Rep: Reputation: 280Reputation: 280Reputation: 280
Without the a (append) it becomes a c (change) followed by the string "ommit".
 
1 members found this post helpful.
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
insert tab at beginning of each line in file - and other sed questions robotrock Linux - Newbie 6 08-19-2015 01:31 AM
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 11:48 AM
[SOLVED] trying to insert text in the last line of a file with sed command.... Sayan Acharjee Linux - General 7 10-04-2010 06:00 AM
Insert a line in the test file with sed say_hi_ravi Programming 16 07-16-2010 11:24 AM
How do I insert a line/value after a particular line, in file with sed Glenn D. Programming 3 01-21-2010 10:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:37 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration