Search code examples
shellawksedtext-processingcut

Display text file stopping each line at a given character


I have a text file containing French words, here is an example:

hackeur -euse (n.m/f.)
huppe (n. f.) 
huque (n. f.) 
hure (n. f.)

I would like to display the file, stopping each line when the pattern ( is detected. The expected output is:

hackeur -euse
huppe
huque
hure

I would like to do this with command line. Is it feasable ?


Solution

  • One awk idea using ( as a field delimiter:

    $ awk -F' [(]' '{print $1}' french.dat
    hackeur -euse
    huppe
    huque
    hure
    

    A couple more quick-n-dirty ideas ...

    cut:

    $ cut -d'(' -f1 french.dat
    hackeur -euse
    huppe
    huque
    hure
    

    NOTE: this will leave a trailing blank, which can be trimmed off via other commands (eg, sed)

    sed and a capture group:

    $ sed -En 's/^(.*) \(.*/\1/p' french.dat
    hackeur -euse
    huppe
    huque
    hure
    

    NOTE: this assumes a single occurrence of (