Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know that's super confusing (total noob-sorry!). To clarify, I have a text file that looks like this:

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)

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:


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

Typically a task for sed or awk:

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

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

The easier would be to catch each number by ^\d+ regex, for example:

grep '?' file.txt | grep -o '^\d\+'


  • ^ beginning of the line
  • \d\+ matches any digit character more than once
share|improve this answer
grep '?' your_file | grep -Eo '^[^-]+' 


grep '?' your_file #will get lines that contains '?'
grep -Eo '^[^-]+'  #grep evrything before dash'-'
share|improve this answer
Hi! I think this answer would be more helpful if it explained how it works. Also, it might be worth clarifying what it adds, compared to the fully-explained answer from three years ago. – dhag Nov 12 '15 at 13:35

Your Answer


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.