LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Add symbol to the end of every other line (https://www.linuxquestions.org/questions/linux-newbie-8/add-symbol-to-the-end-of-every-other-line-4175437152/)

Adzrules 11-14-2012 01:20 PM

Add symbol to the end of every other line
 
Hello everyone,

I'm sure that this is extremely simple, but don't understand the sed command enough to do it myself.

Basically, if I have a list such as that below, I want to add a ; symbol to the end of every other line:

NOOB_1
abcdefghijklmn
NOOB_2
abcdefggggghigglfkj
NOOB_1245
abcddefiigohgggg

So I want to alter the above to give the following:

NOOB_1
abcdefghijklmn;
NOOB_2
abcdefggggghigglfkj;
NOOB_1245
abcddefiigohgggg;

Thanks to anyone that can help. I have managed to add a symbol to the end of EVERY line, but not every OTHER line.

druuna 11-14-2012 02:18 PM

Have a look at this:
Code:

gawk '{ if ( NR%2 != 1 ) { print $0";" } else { print } }' infile

colucix 11-14-2012 02:35 PM

Code:

sed -i '0~2s/$/;/' file
From the sed manual page:
Code:

The following address types are supported:

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.


Adzrules 11-15-2012 10:01 AM

That's perfect. Thank you both. I'm getting the hang of sed now :)


All times are GMT -5. The time now is 09:41 AM.