LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-20-2010, 01:05 PM   #1
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 122

Rep: Reputation: 15
Question Sed: perform replacement only on even-numbered lines?


Hi all,

I'm sure this is possible but I can't figure it out. I have a file formatted like this:
Code:
>SEQUENCE1
FGFD665:?@663:BBBBAA?A@<<4!1004699<7444399>???888455555?AAA?AABBB@@@<<<34
>SEQUENCE2
>&<4!1004699<7444399>???888455555?AAA?AABBB@@@<<<34BBB@@
>SEQUENCE3
???888455555?AAA?AABBB@@@<<<341004699<7444399>???888BBB@@
The odd-numbered lines containing >SEQUENCE# are the headers and the even-numbered lines containing a variety of characters represent DNA sequence read quality scores.

I want to replace all of the greater-than (">") symbols in the quality scores (even-numbered lines) with a "0" but I want to leave the greater-than symbols on the odd-numbered lines alone. I would just use sed -i '/^>/! s/>/0/g' $FileName but sometimes the quality scores begin with a ">" as well.

I'm sure there is a simple modification of a normal sed command with an "n" inserted somewhere but I can't figure it out.

Any suggestions would be greatly appreciated!
Kevin
 
Old 07-20-2010, 01:54 PM   #2
timsgreen
LQ Newbie
 
Registered: Mar 2006
Posts: 14

Rep: Reputation: 2
Does this have to be accomplished with sed? In my opinion this would be much easier if you were receptive to using Perl as an option.
Let me know if you have any interest in pursuing this route.

-tim
 
1 members found this post helpful.
Old 07-20-2010, 02:07 PM   #3
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 122

Original Poster
Rep: Reputation: 15
Hey Tim,

I really don't care how it gets done. I figured there would be an easy way to do it with sed but I feel a little better if that's not the cause because I sure couldn't figure it out.

Thanks!
Kevin
 
Old 07-20-2010, 02:27 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
man sed > addresses (assuming gnu sed)
Code:
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.)
Code:
$ sed '2~2 s/>/0/g' file.txt
>SEQUENCE1
FGFD665:?@663:BBBBAA?A@<<4!1004699<74443990???888455555?AAA?AABBB@@@<<<34
>SEQUENCE2
0&<4!1004699<74443990???888455555?AAA?AABBB@@@<<<34BBB@@
>SEQUENCE3
???888455555?AAA?AABBB@@@<<<341004699<74443990???888BBB@@
You can also nest expressions with { } for more fine-grained control. If you actually wanted to exclude even-numbered lines that began with ">", for example:
Code:
$ sed '2~2 {/^>/! s/>/0/g}' file.txt
>SEQUENCE1
FGFD665:?@663:BBBBAA?A@<<4!1004699<74443990???888455555?AAA?AABBB@@@<<<34
>SEQUENCE2
>&<4!1004699<7444399>???888455555?AAA?AABBB@@@<<<34BBB@@
>SEQUENCE3
???888455555?AAA?AABBB@@@<<<341004699<74443990???888BBB@@

Last edited by David the H.; 07-20-2010 at 02:30 PM. Reason: minor correction
 
1 members found this post helpful.
  


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
[SOLVED] Command to swap even and odd numbered lines in a file Vilael Linux - Newbie 2 05-28-2010 01:02 PM
Sed command to print matching lines and 2 lines above.. DX398 Programming 12 10-01-2008 09:25 AM
deleting even numbered lines bharatbsharma Programming 7 11-26-2007 06:34 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 10:19 AM
replacement with sed: replace pattern with multiple lines Hcman Programming 5 11-18-2004 08:40 AM


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