LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 02-18-2014, 10:34 AM   #1
nouse
LQ Newbie
 
Registered: Sep 2013
Posts: 21

Rep: Reputation: Disabled
How to perform a global character replacement in a txt, excluding some lines


Hi!

I have a file containing
>SampleAA
UUUUUU
>SampleAB
UGUUUU
.
.
.
>SampleAU
UGUUUU

I want to replace any instance of U to T (for those being interested, its changing RNA code to DNA code sequences), but absolutely not in the header lines starting with ">".
So in the end, it should look like

>SampleAA
TTTTTT
>SampleAB
TGTTTT
.
.
.
>SampleAU
TGTTTT

Is that possible?
The header lines are simplified, in reality there are much longer and diverse.

Thank you for help!
 
Old 02-18-2014, 10:41 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Here is a solution with sed:
Code:
sed -i.bck '/^>/!s/U/T/g' file
The -i.bck option is meant to change the content of the file and to make a backup copy of the original file itself (the copy being file.bck). I put it just for safety, until you're sure the result is exactly what you're looking for.
 
1 members found this post helpful.
Old 02-18-2014, 10:47 AM   #3
nouse
LQ Newbie
 
Registered: Sep 2013
Posts: 21

Original Poster
Rep: Reputation: Disabled
Thanks!

I looked in the man of sed, but its very hard to understand your line.
If you have time, could you please explain the syntax, especially of !s?

It worked fine!

edit:
Now i realise there are spaces in the sequence data, which i would like to replace with nothing. Spaces in the header line should be kept. How would that work out?


sed -i.bck '/^>/!s/U/T/g' file

What are the signs for "space" and "nothing" to replace U and T, respectively?

Last edited by nouse; 02-18-2014 at 10:54 AM.
 
1 members found this post helpful.
Old 02-18-2014, 11:00 AM   #4
tshikose
Member
 
Registered: Apr 2010
Location: Kinshasa, Democratic Republic of Congo
Distribution: RHEL, Fedora, CentOS
Posts: 286

Rep: Reputation: 61
This Perl script should do

Quote:
#!/usr/bin/perl

while (<>) {
tr/UT/TU/ unless /^\>/;
print;
}
Test it, as I haven't.
 
Old 02-18-2014, 11:10 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
The command
Code:
!s
means "don't substitute", that is "don't change lines with > at the beginning, but change all the other ones". If you want to remove blank spaces add another sed command:
Code:
sed -i.bck '/^>/!s/U/T/g;/^>/!s/ \+//g' file
Here the regular expression / \+/ means "one or more blank spaces" since you may have two or more consecutive spaces. And in this case the replacement string is empty //.
 
2 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
cut first 10 lines of file master.txt and paste in ab1.txt and so on yogeshkumkar Programming 4 08-31-2011 08:23 AM
Sed: perform replacement only on even-numbered lines? kmkocot Linux - Newbie 3 07-20-2010 02:27 PM
rsync question - Excluding files with a certain character in the filename EmigrantChris Linux - General 2 04-21-2009 11:07 PM
excluding some lines from compiling enzom83 Programming 2 10-13-2007 01:42 PM
Perform action based on email txt plisken Linux - General 1 03-06-2007 10:49 PM


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