LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-12-2009, 08:50 AM   #1
Euler2
LQ Newbie
 
Registered: Oct 2009
Posts: 20

Rep: Reputation: 0
Adding a Comma to the end of every nth line in Vi (or sed).


So I have a fairly long list of data that I am trying to put into a math program (maple) but before I can do that I need to edit the format of the data such that at the end of every 25th line I add a comma.

I would prefer a solution that uses vi, but if that's impossible sed would be fine also (or awk).

Thanks.
 
Old 10-12-2009, 08:59 AM   #2
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Here is one with sed

Code:
sed '0~25 s/$/;/' /tmp/lines
 
Old 10-12-2009, 09:16 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
That starts on the 25th line and adds a ";"

If you want line numbers 1, 26, 51, etc. then do sed '1~25s/$/,/'

Last edited by pixellany; 10-12-2009 at 09:18 AM.
 
Old 10-12-2009, 10:10 AM   #4
Euler2
LQ Newbie
 
Registered: Oct 2009
Posts: 20

Original Poster
Rep: Reputation: 0
I try the sed suggestion and it gives back:

sed: 1: "0~25 s/$/;/": invalid command code ~

Any suggestions?

Thanks again for the help
 
Old 10-12-2009, 10:25 AM   #5
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hello Euler2 and wellcome to LQ,

here a vim-solution:
go to the end of the first line where you want to add a ",". Then
Code:
qa25jA,q
This records a macro into buffer a. It goes 25 lines down and adds a "," at the end of the line. To execute the macro type:
Code:
@a
you may give the macro an integer as an argument, for example:
Code:
50@a
will execute 50 times the macro.

Markus

Last edited by markush; 10-12-2009 at 10:27 AM.
 
Old 10-12-2009, 10:36 AM   #6
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by Euler2 View Post
I try the sed suggestion and it gives back:

sed: 1: "0~25 s/$/;/": invalid command code ~

Any suggestions?

Thanks again for the help
Use ' instead of ". Reason is that ~ is a special char on bash. and also ; will be taken as the end of the command line.
 
Old 10-12-2009, 10:38 AM   #7
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,900
Blog Entries: 27

Rep: Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186
Hi,

Welcome to LQ!

Now that we are all aware of your 'needs'. What are your 'deeds'?

It would be nice if you would share what you've attempted.
 
  


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
sed - How do you replace end of line with a space pppaaarrrkkk Programming 7 02-07-2011 12:27 PM
using sed to remove line in a comma-delimited file seefor Programming 4 03-10-2009 04:35 PM
[SOLVED] sed: How to remove the end of a line? angel115 Programming 2 10-01-2007 11:29 AM
[SOLVED] delete the end of each line in a file with sed angel115 Linux - Newbie 3 11-16-2005 05:41 PM
adding new line at end of file airikah Programming 3 11-08-2003 05:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:12 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration