Search code examples
bashawksedgrepcut

getting first part of a string that has two parts


I have a string that has two parts (path and owner) both separated by a space.

This is the input file input.txt

/dir1/dir2/file1 @owner1
/dir1/dir2/foo\ bar @owner2

I want to extract all the paths to a separate output file - output.txt I cannot use space as delimiter since paths can also have filenames with space and delimiter in them

/dir1/dir2/file1
/dir1/dir2/foo\ bar

Solution

  • Here could be a different way of doing it with rev + GNU grep:

    rev file | grep -oP '.*@ \K.*' | rev
    

    OR

    rev file | grep -oP '.*@\s+\K.*' | rev
    

    With original simple solution go with:

    awk -F' @' '{print $1}' Input_file