Results 1 to 5 of 5
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 09-21-2012 #1Just Joined!
- Join Date
- Sep 2012
- Posts
- 3
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.
- 09-22-2012 #2Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,700
use sed. in a nutshell:
s/regexp/replacement/
put what you want to match on in regexp and put what you want to replace it with in replacement.
in the regexp, along with the string you want to match on, you can use a "." to match any character and a "*" to match more than one character, and the "$" to match the end of the string.
use it like this:
echo "some string"|sed -e 's/regexp/replacement/'
- 09-24-2012 #3Just Joined!
- Join Date
- Sep 2012
- Posts
- 3
Example of regexp?
Would you mind posting some example of regexp and how to use it? The expression I want to replace (or truncate) has spaces in it and the comand below appears to ignore everything after spaces.
Here is the command I ran and the output
echo "prof_dir/symcan/articles/blah/UAT/8045.xml: <attr>2010</attr>" | sed -e '.xml: <attr>2010</attr>/ '
Output --> prof_dir/symcan/articles/blah/UAT/8045attr>/attr>
I am trying to replace ".xml: <attr>2010</attr>" with a space (Basically that is last part of the string and I want to get rid of it.
Thanks much for your suggestion and help.
Last edited by threezerous; 09-24-2012 at 03:17 PM.
- 09-24-2012 #4Just Joined!
- Join Date
- Sep 2012
- Posts
- 3
- 09-25-2012 #5Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,700
i see you already worked it out w/cut, but here is what i would have suggested:
note that sed is using the pipe ("|") as the separator instead of the forward slash. that way, you can use the forward slash as part of the regexp.Code:echo $string|sed -e 's|\.xml: <attr>2010</attr>.*$||'


Reply With Quote

