LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 04-09-2015, 11:07 AM   #1
rubylu
LQ Newbie
 
Registered: Apr 2015
Posts: 2

Rep: Reputation: Disabled
sed command to replace special character in special line


I'm a biochemist, and need to deal with a file with millions line sequence data.

the input file is:

@HWI-ST1324:186:HBEEWADXX:1:1101:1164:2175/1
CCTAGC
+
?@@D:A
@HWI-ST1324:126:HBEEWADXX:2:1101:1164:2111/2
CCAAGC
+
?@/D:A
...

My problem is to replace "/" in each line start with "@HWI" to "#AA/", ie. the outcome should be:
@HWI-ST1324:186:HBEEWADXX:1:1101:1164:2175#AA/1
CCTAGC
+
?@@D:A
@HWI-ST1324:126:HBEEWADXX:2:1101:1164:2111#AA/2
CCAAGC
+
?@/D:A
...

Any suggestion to solve this question?

Thanks for your input, much appreciate.
 
Old 04-09-2015, 11:27 AM   #2
Lnthink
Member
 
Registered: May 2010
Location: Lafayette, LA
Distribution: Ubuntu, RH, Fedora
Posts: 38

Rep: Reputation: 8
Quote:
Originally Posted by rubylu View Post
I'm a biochemist, and need to deal with a file with millions line sequence data.

the input file is:

@HWI-ST1324:186:HBEEWADXX:1:1101:1164:2175/1
CCTAGC
+
?@@D:A
@HWI-ST1324:126:HBEEWADXX:2:1101:1164:2111/2
CCAAGC
+
?@/D:A
...

My problem is to replace "/" in each line start with "@HWI" to "#AA/", ie. the outcome should be:
@HWI-ST1324:186:HBEEWADXX:1:1101:1164:2175#AA/1
CCTAGC
+
?@@D:A
@HWI-ST1324:126:HBEEWADXX:2:1101:1164:2111#AA/2
CCAAGC
+
?@/D:A
...

Any suggestion to solve this question?

Thanks for your input, much appreciate.

sed "/^\@HWI/ s/\//\#AA\//" < inputfile > outputfile
or
cat inputfile | sed "/^\@HWI/ s/\//\#AA\//" > outputfile

You may not need the backslashes in front of @ or # - you can try it without it.
It's just an escape character for special handling of certain characters - I'm guessing you don't really need it,
but it generally never hurts if you're not sure.

So, the sed expression *might* work just as well with... "/^@HWI/ s/\//#AA\//"
The first phrase, before the space is the part that looks at the beginning of the line(using the ^ symbol) for the @HWI string.
For all occurrences where this is true, it will substitute (the "s" command) any "/" (which must be backslashed for special handling, because forward slash characters are used to delimit the bounds of a subst command), with "#AA/" (once again, before the "/" there's a backslash for special handling).

That should handle your problem, and give you some info on how to handle it next time yourself, if something similar comes up.

Hope this helps.
 
1 members found this post helpful.
Old 04-09-2015, 11:37 AM   #3
rubylu
LQ Newbie
 
Registered: Apr 2015
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks Lnthink:

It works, and save me lots of time to try.

Thanks again, and appreciate your help.

Best

Quote:
Originally Posted by Lnthink View Post
sed "/^\@HWI/ s/\//\#AA\//" < inputfile > outputfile
or
cat inputfile | sed "/^\@HWI/ s/\//\#AA\//" > outputfile

You may not need the backslashes in front of @ or # - you can try it without it.
It's just an escape character for special handling of certain characters - I'm guessing you don't really need it,
but it generally never hurts if you're not sure.

So, the sed expression *might* work just as well with... "/^@HWI/ s/\//#AA\//"
The first phrase, before the space is the part that looks at the beginning of the line(using the ^ symbol) for the @HWI string.
For all occurrences where this is true, it will substitute (the "s" command) any "/" (which must be backslashed for special handling, because forward slash characters are used to delimit the bounds of a subst command), with "#AA/" (once again, before the "/" there's a backslash for special handling).

That should handle your problem, and give you some info on how to handle it next time yourself, if something similar comes up.

Hope this helps.
 
1 members found this post helpful.
Old 04-09-2015, 02:45 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
You can tidy this up a little by changing the delimiters to the s/// command:
Code:
sed '/^@HWI/ s@/@#AA/@' file
 
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] Expect syntax issue when calling a bash sed command that uses special character '}' YortheHunter Programming 6 08-10-2012 10:31 AM
search and replace string with special character perl or sed. Noobux Programming 4 05-21-2012 04:16 PM
Find and Replace character/special character from the file MyRelam Red Hat 8 05-21-2012 01:52 AM
[SOLVED] sed command to replace special character / Lokelo Linux - Newbie 11 11-23-2011 08:59 AM


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