LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 03: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, 04:50 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
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 05:04 AM.
 
2 members found this post helpful.
Old 01-10-2018, 05: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, 05:42 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
Strange. sed should do the job. Which regex did you write? Or did you work with the 'a' command instead?
 
Old 01-10-2018, 10: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, 10:37 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
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, 04:51 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

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


Reply



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 12:31 AM
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 10: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 05:00 AM
Insert a line in the test file with sed say_hi_ravi Programming 16 07-16-2010 10:24 AM
How do I insert a line/value after a particular line, in file with sed Glenn D. Programming 3 01-21-2010 09:14 PM

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

All times are GMT -5. The time now is 02:09 PM.

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
Open Source Consulting | Domain Registration