LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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
 
Search this Thread
Old 09-21-2012, 10:55 AM   #1
threezerous
Member
 
Registered: Jul 2009
Posts: 80

Rep: Reputation: 15
truncate to end of line after a specific string


I thought this would be easy to find on google or figure out myself. But either I am not looking properly or missing the obvious. Apologies in advance if this has been replied elsewhere.

I want to delete all characters to end of line after a specific string in a file.

So my file contents are such (with variable length lines)

aaaabbbbbbbbbbbcccc.hrpt <www-alpha>
dddddbbbbbbbbcccc.hrpt <www-beta>
pppppppppppppppbbbbbbbbbbcccc.hrpt <www-gamma>
xxxbbbbbcccc.hrpt <www-alpha>

The common string above starts with is '.hrpt <www-'
I want to delete everything and inclusive of the common string....so my output file would look like

aaaabbbbbbbbbbbcccc
dddddbbbbbbbbcccc
pppppppppppppppbbbbbbbbbbcccc
xxxbbbbbcccc

I can easily do this using excel on a windows machine, but would like to try using shell scripting.
I played around a bit with sed command with little success.

Thanks much for any assistance.
 
Old 09-21-2012, 04:29 PM   #2
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch
Posts: 421

Rep: Reputation: 18
Quote:
Originally Posted by threezerous View Post
I thought this would be easy to find on google or figure out myself. But either I am not looking properly or missing the obvious. Apologies in advance if this has been replied elsewhere.

I want to delete all characters to end of line after a specific string in a file.

So my file contents are such (with variable length lines)

aaaabbbbbbbbbbbcccc.hrpt <www-alpha>
dddddbbbbbbbbcccc.hrpt <www-beta>
pppppppppppppppbbbbbbbbbbcccc.hrpt <www-gamma>
xxxbbbbbcccc.hrpt <www-alpha>

The common string above starts with is '.hrpt <www-'
I want to delete everything and inclusive of the common string....so my output file would look like

aaaabbbbbbbbbbbcccc
dddddbbbbbbbbcccc
pppppppppppppppbbbbbbbbbbcccc
xxxbbbbbcccc

I can easily do this using excel on a windows machine, but would like to try using shell scripting.
I played around a bit with sed command with little success.

Thanks much for any assistance.
The following assumes that there is never an actual '.' in the part you wish to keep. If that were not so you could substitute just '.*' for '[^\.]'
Code:
~ $ cat <tester
aaaabbbbbbbbbbbcccc.hrpt <www-alpha>
dddddbbbbbbbbcccc.hrpt <www-beta>
pppppppppppppppbbbbbbbbbbcccc.hrpt <www-gamma>
xxxbbbbbcccc.hrpt <www-alpha>
~ $ sed 's/\([^\.]\)\.hrpt.*/\1/' < tester
aaaabbbbbbbbbbbcccc
dddddbbbbbbbbcccc
pppppppppppppppbbbbbbbbbbcccc
xxxbbbbbcccc

Last edited by porphyry5; 09-21-2012 at 04:33 PM. Reason: Added caveat
 
Old 09-21-2012, 04:49 PM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,563
Blog Entries: 1

Rep: Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024
Here are 3 more possible solutions:
Code:
sed 's/\..*//' file  # assuming that the dot is unique and always there
awk -F\. '{ print $1 }' file  # see above
sed '/\.hrpt.*/s///' file

Last edited by sycamorex; 09-21-2012 at 04:50 PM.
 
  


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
how to add string on the beginning and end of the line packets Programming 2 08-03-2010 09:55 PM
[SOLVED] Text on a specific line at the end of a line genderbender Programming 25 07-27-2010 06:47 AM
Append variable string(s) at end of each line schaganti Linux - Newbie 2 10-19-2007 01:31 PM
bash: append string to end of line khairil Programming 6 02-27-2007 05:09 AM
Wierd String appended to end of line lel800 General 8 12-28-2004 03:55 PM


All times are GMT -5. The time now is 07:00 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