Search code examples
bashsedslash

remove string using sed that include slash


Here is the data I have:

This is a test/><STUFF
This is a test/><TRY

I am trying to be rid of /><STUFF and /><TRY in bash using sed.

So as result having the two sentences.

This is a test
This is a test

Solution

  • Remove everything from the slash:

    $ sed 's_/.*__' file
    This is a test
    This is a test
    

    Note the usage of _ as delimiter, since the typical slash sed 's/find/replace/' file collides with the pattern you are looking for. You can also escape it.

    Or with cut, set the delimiter as a slash and just print the first field:

    $ cut -d'/' -f1 file
    This is a test
    This is a test
    

    Although the cleanest is awk:

    $ awk -F/ '{print $1}' file
    This is a test
    This is a test
    

    A bash solution is:

    while IFS="/" read name _
    do
       echo "$name"
    done < file