12

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

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

An alternative sed solution:

sed 's/(.*//' file
6

Using cut:

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

daisy

lilly

sunflower

Using sed:

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

daisy

lilly

sunflower
  • 4
    Note that the behaviour will differ if there's more than one ( per line. – Stéphane Chazelas Apr 2 '14 at 9:06
3

An awk solution:

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

daisy

lilly

sunflower

or GNU grep:

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

You can use cut command:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.