Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

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

4 Answers 4

An alternative sed solution:

sed 's/(.*//' file
share|improve this answer

Using cut:

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

daisy

lilly

sunflower

Using sed:

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

daisy

lilly

sunflower
share|improve this answer
3  
Note that the behaviour will differ if there's more than one ( per line. –  Stéphane Chazelas Apr 2 at 9:06

An awk solution:

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

daisy

lilly

sunflower

or GNU grep:

grep -oP '.*?(?=\()' file
share|improve this answer
3  
Or grep -o '^[^(]*' –  Stéphane Chazelas Apr 2 at 9:05
    
it is working......thanks u so much –  Rincy Raphael Apr 2 at 9:14

You can use cut command:

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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