Find the answer to your Linux question:
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.
  1. #1
    Just Joined!
    Join Date
    Sep 2012
    Posts
    3

    Arrow 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.

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    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/'

  3. #3
    Just 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.



    Quote Originally Posted by atreyu View Post
    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/'
    Last edited by threezerous; 09-24-2012 at 03:17 PM.

  4. #4
    Just Joined!
    Join Date
    Sep 2012
    Posts
    3

    Solution

    I was able to make it work with

    echo string | cut -f1 -d\.;done
    (delete everything after dot)

    Thanks


    Quote Originally Posted by threezerous View Post
    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.

  5. #5
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    Quote Originally Posted by threezerous View Post
    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.
    i see you already worked it out w/cut, but here is what i would have suggested:

    Code:
    echo $string|sed -e 's|\.xml: <attr>2010</attr>.*$||'
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •