Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

sed command to delete everything after > on line

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-18-2012
LMHmedchem LMHmedchem is offline
Registered User
 
Join Date: Mar 2010
Last Activity: 11 March 2015, 1:40 AM EDT
Location: Boston
Posts: 276
Thanks: 120
Thanked 5 Times in 5 Posts
sed command to delete everything after > on line

I have a large data file that I need to edit. There are lines like,

Code:
>  <IDNUMBER> (ST000002)
>  <SUPPLIER> (ST000002)
>  <IDNUMBER> (ST000004)
>  <SUPPLIER> (ST000004)

and I need to delete everything after the >, excepting the end of line.

Code:
>  <IDNUMBER>
>  <SUPPLIER>
>  <IDNUMBER>
>  <SUPPLIER>

I am not completely sure that the > appears only in this place, so I think I need to do something like,


Code:
sed 's/>\ \ <*>\ (*)/>/g'

But I have misplaced the list of all of my sed commands and don't remember the syntax, what needs to be escaped, etc.

Help would be appreciated.

LMHmedchem

Last edited by Scrutinizer; 06-18-2012 at 01:40 PM.. Reason: code tags
Sponsored Links
    #2  
Old Unix and Linux 06-18-2012
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 1 September 2015, 11:41 AM EDT
Location: Amsterdam
Posts: 10,431
Thanks: 358
Thanked 2,769 Times in 2,475 Posts
Try:

Code:
sed 's/[^>]*$//'

Sponsored Links
    #3  
Old Unix and Linux 06-18-2012
LMHmedchem LMHmedchem is offline
Registered User
 
Join Date: Mar 2010
Last Activity: 11 March 2015, 1:40 AM EDT
Location: Boston
Posts: 276
Thanks: 120
Thanked 5 Times in 5 Posts
Quote:
Originally Posted by Scrutinizer View Post
Try:

Code:
sed 's/[^>]*$//'

That deletes the trailing content I don't want, but also deletes everything else in the file. Maybe I was unclear, there are allot of other lines in the file that just need to be printed, only the lines that look like,

Code:
>  <IDNUMBER> (ST000002)

need to be processed.

In other words, something like,

Code:
  9  7  1  0  0  0  0
  6 15  1  0  0  0  0
  5 14  1  0  0  0  0
  3  2  1  0  0  0  0
 13  5  1  0  0  0  0
 17 16  2  0  0  0  0
M  END
>  <IDNUMBER> (ST000004)
ST000004

>  <SUPPLIER> (ST000004)
TimTec

processed to,

Code:
  9  7  1  0  0  0  0
  6 15  1  0  0  0  0
  5 14  1  0  0  0  0
  3  2  1  0  0  0  0
 13  5  1  0  0  0  0
 17 16  2  0  0  0  0
M  END
>  <IDNUMBER>
ST000004

>  <SUPPLIER>
TimTec

Your sample code gave me,

Code:






>  <IDNUMBER>


>  <SUPPLIER>

Thanks,

LMHmedchem
    #4  
Old Unix and Linux 06-18-2012
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 1 September 2015, 11:41 AM EDT
Location: Amsterdam
Posts: 10,431
Thanks: 358
Thanked 2,769 Times in 2,475 Posts
I see, yes then it should be:

Code:
sed 's/>[^>]*$/>/'

or perhaps:

Code:
sed '/^>/s/>[^>]*$/>/'

The Following User Says Thank You to Scrutinizer For This Useful Post:
LMHmedchem (06-18-2012)
Sponsored Links
    #5  
Old Unix and Linux 06-18-2012
LMHmedchem LMHmedchem is offline
Registered User
 
Join Date: Mar 2010
Last Activity: 11 March 2015, 1:40 AM EDT
Location: Boston
Posts: 276
Thanks: 120
Thanked 5 Times in 5 Posts
Thanks, that worked great. Sed is such a great tool. I had a 250K text file with odd formatting that would have blown up the next tool in the chain and sed fixed it in 23s. At times, it does look a bit like hieroglyphics, and I really need to find my notes, but there is nothing like the right tool for the job.

Thanks again,

LMHmedchem
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
delete the line with a particular string in a file using sed command. PrasadAruna Shell Programming and Scripting 5 05-09-2012 06:06 AM
sed command to delete line dvah Shell Programming and Scripting 1 04-11-2011 06:56 AM
SED delete only first line command - Help need gwgreen1 Shell Programming and Scripting 2 07-31-2010 07:39 AM
delete contents from command line juanbro UNIX for Dummies Questions & Answers 4 12-23-2003 11:25 AM
Can't delete the user from command line spavlov UNIX for Advanced & Expert Users 4 09-23-2002 09:38 AM



All times are GMT -4. The time now is 12:07 PM.