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 know that's super confusing (total noob-sorry!). To clarify, I have a text file that looks like this:

407-OL?
408-2-OL?
408-OL?
418-het?
420-1 and 2- OL?
429-2-left unscored?
430-2-left both unscored?
431-1 and 2- Ri??
436-1-just homozygote?
444-2-het? ins. both
456-2-ins 246 despite slight OL
456-1-ins 245 (weaker)
457-2-Ri?

I want it to return the numbers on the left (before the dash), but only for lines that contain a question mark. In other words, I want this to be the output:

407
408
408
418
420
429
430
431
436
444
457

Any suggestions would be amazingly helpful! Thank you!

share|improve this question
    
To clarify further, there is a new line before each number with a dash: 407-OL? '\n' 408-OL? '\n' etc. etc. –  Atticus29 May 6 '12 at 16:43
    
After reading the question title, I just realized how awkward translating sed to English would be. –  jw013 May 7 '12 at 1:18
add comment

2 Answers

up vote 1 down vote accepted

Probably easiest method: cat some_file | grep '?' | cut -d'-' -f1

  • cat somefile => feed the contents of some_file into the pipe
  • grep '?' => filter only lines containing a ?
  • cut -d'-' -f1 => divide the string into fields with - as field separator, then print field #1
share|improve this answer
    
Fantastic! Thank you very much! It worked! –  Atticus29 May 6 '12 at 17:02
3  
Make a touch a then try again. Will not work anymore. ? is a wildcard character and should be escaped or quoted. –  manatwork May 6 '12 at 17:04
    
I know what you mean, but I can't reproduce it. grep ? should be replaced by grep '\?', is that what you mean? –  jippie May 6 '12 at 17:07
    
I mean “escape or quote”. So either grep \? or grep '?' or grep "?". Otherwise the shell will perform wildcard expansion and ? will be replaced with the list of matching files – all files in the current directory with single character long names. –  manatwork May 6 '12 at 17:15
    
updated my answer –  jippie May 6 '12 at 17:19
show 1 more comment

Typically a task for sed or awk:

sed -n '/?/s/-.*//p' some_file

awk -F- '/\?/{print$1}' some_file
share|improve this answer
add comment

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.