LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Adding a Comma to the end of every nth line in Vi (or sed). (https://www.linuxquestions.org/questions/linux-newbie-8/adding-a-comma-to-the-end-of-every-nth-line-in-vi-or-sed-761353/)

Euler2 10-12-2009 07:50 AM

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.

zhjim 10-12-2009 07:59 AM

Here is one with sed

Code:

sed '0~25 s/$/;/' /tmp/lines

pixellany 10-12-2009 08:16 AM

That starts on the 25th line and adds a ";"

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

Euler2 10-12-2009 09:10 AM

I try the sed suggestion and it gives back:

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

Any suggestions?

Thanks again for the help

markush 10-12-2009 09:25 AM

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

zhjim 10-12-2009 09:36 AM

Quote:

Originally Posted by Euler2 (Post 3716441)
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.

onebuck 10-12-2009 09:38 AM

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.


All times are GMT -5. The time now is 02:29 AM.