LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Truncating lines using sed (https://www.linuxquestions.org/questions/linux-newbie-8/truncating-lines-using-sed-796553/)

kmkocot 03-19-2010 02:16 PM

Truncating lines using sed
 
Hi

I am trying to truncate some sequence names to only the first 9 characters using sed. The lines with the name are designated by a ">"

For example: I need this:

>H.neanderthalensis
ATGAAATTCACGCTCAGCTCGATCGCTAGCTAGC
>R.norweignensis
ATGCTCGCTCGATCGCTAGCTCGATCGCTAGCTC

to be truncated to this

>H.neadert
ATGAAATTCACGCTCAGCTCGATCGCTAGCTAGC
>R.norweig
ATGCTCGCTCGATCGCTAGCTCGATCGCTAGCTC

This should be simple, any suggestions?

penguiniator 03-19-2010 03:21 PM

Code:

sed -e 's/\(>.........\).*/\1/'

colucix 03-19-2010 03:26 PM

Using extended regexp, you can try to match 9 characters after the leading > and use parentheses to keep the pattern. Here we go:
Code:

sed -r 's/(^>.{9}).*/\1/' file
Edit: too late! Solution by penguiniator even more simple. :)

penguiniator 03-19-2010 04:07 PM

colucix's solution is actually more precise. The ^ character anchors the regular expression to the beginning of the line, and with -r, allows more concise extended regular expressions.


All times are GMT -5. The time now is 06:18 AM.