LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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
 
LinkBack Search this Thread
Old 03-19-2010, 02:16 PM   #1
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 94

Rep: Reputation: 15
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?
 
Old 03-19-2010, 03:21 PM   #2
penguiniator
Member
 
Registered: Feb 2004
Location: Milton, WA
Distribution: Fedora
Posts: 440
Blog Entries: 3

Rep: Reputation: 60
Code:
sed -e 's/\(>.........\).*/\1/'
 
Old 03-19-2010, 03:26 PM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,375

Rep: Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911
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.

Last edited by colucix; 03-19-2010 at 03:28 PM.
 
Old 03-19-2010, 04:07 PM   #4
penguiniator
Member
 
Registered: Feb 2004
Location: Milton, WA
Distribution: Fedora
Posts: 440
Blog Entries: 3

Rep: Reputation: 60
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.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sed two lines down secretlydead Programming 2 04-24-2009 10:56 AM
Sed command to print matching lines and 2 lines above.. DX398 Programming 12 10-01-2008 08:25 AM
delete lines with sed LuciusHunt Programming 1 04-19-2008 02:40 PM
Sed empty lines nc3b Programming 4 08-27-2006 06:37 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM


All times are GMT -5. The time now is 12:50 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration