LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-14-2012, 01:20 PM   #1
Adzrules
LQ Newbie
 
Registered: Oct 2012
Posts: 14

Rep: Reputation: Disabled
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.
 
Old 11-14-2012, 02:18 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Have a look at this:
Code:
gawk '{ if ( NR%2 != 1 ) { print $0";" } else { print } }' infile
 
1 members found this post helpful.
Old 11-14-2012, 02:35 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.

Last edited by colucix; 11-14-2012 at 02:40 PM. Reason: Added reference.
 
1 members found this post helpful.
Old 11-15-2012, 10:01 AM   #4
Adzrules
LQ Newbie
 
Registered: Oct 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
That's perfect. Thank you both. I'm getting the hang of sed now
 
  


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 add a character to the end of each line keenboy Linux - General 2 08-05-2010 12:36 PM
how to add string on the beginning and end of the line packets Programming 2 08-03-2010 09:55 PM
add more than one line to end of file Siljrath Linux - General 1 08-18-2009 11:01 PM
add text to end of line? tatoosh Linux - Newbie 8 07-31-2009 03:28 AM
Shell scripting: How to add characters at the end of the line Micro420 Programming 7 05-18-2007 01:56 AM


All times are GMT -5. The time now is 07:03 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