14

I need to extract the characters before the character '('. My file structure is:

rose(good)ook

daisy()no

lilly(bad)fine

sunflower()nice

I need output file as in the form of:

rose

daisy

lilly

sunflower

can anybody tell the command for this.....

awk, grep or sed are preferable...

2
  • awk -F"(" '{print $1}' file i tried this command and its working. I want to know the command in grep... Commented Apr 2, 2014 at 8:33
  • Updated answer.
    – cuonglm
    Commented Apr 2, 2014 at 8:37

4 Answers 4

13

An alternative sed solution:

sed 's/(.*//' file
1
  • Works with a comma too. Thanks! Commented Aug 13, 2020 at 22:24
8

Using cut:

$ <file cut -d \( -f 1
rose

daisy

lilly

sunflower

Using sed:

$ <file sed -e 's/\(.*\)(.*$/\1/'
rose

daisy

lilly

sunflower
1
  • 4
    Note that the behaviour will differ if there's more than one ( per line. Commented Apr 2, 2014 at 9:06
3

An awk solution:

$ awk -F'(' '{print $1}' file
rose

daisy

lilly

sunflower

or GNU grep:

grep -oP '.*?(?=\()' file
1
0

You can use cut command:

cut -f1 -d "(" file_name
2
  • 1
    How is this different than the answer given by @rahmu above?
    – Chris Down
    Commented Apr 2, 2014 at 8:38
  • 2
    @ChrisDown it's badly formated without any explanation ?
    – Kiwy
    Commented Apr 2, 2014 at 9:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .