LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-30-2011, 09:30 PM   #1
sorrymouse
LQ Newbie
 
Registered: Oct 2011
Posts: 2

Rep: Reputation: Disabled
using sed to remove all characters on a line except the first


I have a file that is structured as follows:

@HW367
TCTGTCTGATC
+HW367
########
@HW785
GCGCTGCG
+HW785
##%##DD

etc. The lengths of lines are variable, and consist of many special characters. Following either the @HW or +HW each has a unique id number (in pairs of two, so each number has one + and one @ entry). I need to remove everything on the + line except the +. I have been trying modify a sed script:

sed '/+/,/\n/ s/*//'

to do this, modified by me from this one:

sed '/start/,/stop/ s/#.*//'

But I don't really know what I am doing. Any ideas would be really appreciated.

Thank you!
 
Old 10-30-2011, 09:59 PM   #2
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84
Code:
sed -r 's/^(.).*$/\1/g'
s/what-to-match/what-to-replace-it-with/g

s=replace
g="global" flag for replace

what to match:
^ = beginning of line
(.) = a character. sourround it in parenthesis to refer to it later as backreference #1 (the \1)
.* = any characters following
$ = end of line

what to replace it with:
\1 = the captured expression in the previous section of the s command

the -r switch i've put it mostly so I don't have to escape the parenthesis, otherwise it would have look like this:

Code:
sed 's/^\(.\).*$/\1/g'
edit
sorry i just see you only need to do this operation in lines beginning with a "+", this will do:

Code:
sed -r '/\+/s/^(.).*$/\1/g'
Of course, since you already know you'll use always a "+" for replacement you could leave it fixed too:

Code:
sed -r 's/^\+.*$/+/g'

Last edited by Juako; 10-30-2011 at 10:05 PM.
 
Old 10-31-2011, 05:36 AM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by Juako View Post
Code:
sed -r '/\+/s/^(.).*$/\1/g'
Hi,

that is a good idea. But you really do not need the 'g' flag at the end here. Since you only want to keep the first character there is no need for 'global' repetition of the 's' command.
 
Old 10-31-2011, 09:10 AM   #4
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84
Quote:
Originally Posted by crts View Post
Hi,

that is a good idea. But you really do not need the 'g' flag at the end here. Since you only want to keep the first character there is no need for 'global' repetition of the 's' command.
You're right that "global" isn't necessary here. But as it happens that in most cases I end up using it I just tend to leave it out only when it affects the result (in this case i would have omitted it if the OP wanted to, say, transform just the first ocurrence of a "+" and keep processing). But, since here the command will stop anyway after first match, cutting the line to a "+", it doesn't affect anything in practice, thus I went away with my common pattern.

Last edited by Juako; 10-31-2011 at 09:11 AM.
 
Old 10-31-2011, 11:33 AM   #5
sorrymouse
LQ Newbie
 
Registered: Oct 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank you!

This seems to be doing the trick. I appreciate the effort this community puts into helping people like me out!
 
  


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
SED - remove last four characters from string 3saul Linux - Software 5 07-28-2014 07:25 AM
[SOLVED] Need help in replacing set of characters in a specific line using sed or awk bbachu Programming 15 01-03-2011 02:01 AM
remove particular characters using sed dsids Linux - Software 7 12-14-2010 01:10 AM
Using sed to remove all but the last 17 characters on a line simplified Programming 5 06-04-2010 04:33 AM
Getting last characters of a line with sed command LULUSNATCH Programming 4 12-21-2005 10:33 AM


All times are GMT -5. The time now is 08:46 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration