0

I have many files that contain similar strings as:

>33100_Pseudomonas_etcetera1
texttexttext
>33632_Pseudomonas_etcetera2
texttexttext
>36406_Pseudomonas_etcetera3
texttexttext

and I need outputs like this:

>Pseudomomas_etcetera1
texttexttext
>Pseudomomas_etcetera2
texttexttext
>Pseudomomas_etcetera3
texttexttext

Pretty sure there is a simple sed solution which I can't seem to figure out

Notes: The characters are always five numbers followed by an underscore followed by Pseudomonas_etcetera. This string is always at the beginning of its line and it always starts with ">". All files have a ".sorted" extension.

3

sed does it:

sed -E 's/>([0-9]+_)(\w+)/>\2/' files

if your pattern exactly in question:

sed -E 's/([0-9]+_)//' files
  • -E use extended regular expressions
  • ([0-9]+_)(\w+) defines two Word Pattern region which you would access them with \1 and \2.
  • Note that if the header looks like >098_thing more things here, then the trailing words would be removed by the first sed command. The second sed command would remove digits followed by an underscore anywhere in the file. – Kusalananda Aug 1 '18 at 13:38
1

Using standard sed, and assuming all files are located in the current directory:

mkdir fixed || exit 1   # exits if the output directory already exists
for filename in *.sorted; do
    sed '/^>[0-9]*_/s//>/' "$filename" >"fixed/$filename"
done

This will iterate over all filenames matching the pattern *.sorted, and for each one run a short sed script.

The sed script will locate all Fasta headers that start with a number followed by a underscore. When it find one, it will replace the header marker, the number and the underscore with just the > header marker.

The result will be written to new files in the fixed subdirectory.

1

[OP wrote] Pretty sure there is a simple sed solution which I can't seem to figure out.

That is correct.

[OP wrote] Notes: The characters are always five numbers followed by an underscore followed by Pseudomonas_etcetera. This string is always at the beginning of its line and it always starts with ">". All files have a ".sorted" extension.

Your description of the pbm statement is pretty exact, in the sense that it can be gainfully translated into sed code. Here's how:

  • This string is always at the beginning of its line and it always starts with ">": regex => /^>/
  • The characters are always 5 numeric characters: regex => /^>[0-9][0-9][0-9][0-9][0-9]/
  • Followed by an underscore followed by "Pseudomonas_etcetera": regex => /^>[0-9][0-9][0-9][0-9][0-9]_Pseudomonas_etcetera/
  • The basic syntax of the substitute command in sed is: s/regex/rpl_str/, meaning whatever portion of the pattern space (normally the current line, aka, record) is matched by the regex (on the LHS of the s/// command, is to be substituted with the replacement string on the RHS of the s/// command.
  • So your command to do the substitution is:
  • sed -e 's/^>[0-9][0-9][0-9][0-9][0-9]_Pseudomonas_etcetera/>Pseudomonas_etcetera/' inp1.sorted
  • Disclaimer: Not tested it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.