LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed command to replace special character in special line (https://www.linuxquestions.org/questions/linux-newbie-8/sed-command-to-replace-special-character-in-special-line-4175539221/)

rubylu 04-09-2015 10:07 AM

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.

Lnthink 04-09-2015 10:27 AM

Quote:

Originally Posted by rubylu (Post 5344935)
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.

rubylu 04-09-2015 10:37 AM

Thanks Lnthink:

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

Thanks again, and appreciate your help.

Best

Quote:

Originally Posted by Lnthink (Post 5344946)
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.


grail 04-09-2015 01:45 PM

You can tidy this up a little by changing the delimiters to the s/// command:
Code:

sed '/^@HWI/ s@/@#AA/@' file


All times are GMT -5. The time now is 10:26 AM.