How to remove text in each line after hyphen?

Hi, I'm trying to do something relatively simple.

I have a txt file that has the following kinds of lines (and many more lines):

CP19 Oahu - Maunawili Falls
CP20 Oahu - Maunawili Falls
AG12 Oahu - Maunawili Falls
CP22 Oahu - Maunawili Falls, Local area
AG14 Oahu
CP141 KZ102 Kauai - Kokee Milolii Road
CP142 MK1031 Kauai - Kokee Milolii Road
JW14 Oahu - Moana Lua

I would like to remove the text after each "-" in each line that has a hyphen.
In other words, I would like the output to look like:

CP19 Oahu
CP20 Oahu
AG12 Oahu
CP22 Oahu
AG14 Oahu
CP141 KZ102 Kauai
CP142 MK1031 Kauai
JW14 Oahu

I tried sed, but couldn't get it to work. Does anyone have an easy solution to this question?

Thanks

Please post what you tried and what error you got.

$ ruby  -ne 'puts $_.split("-",2)[0]' file
CP19 Oahu
CP20 Oahu
AG12 Oahu
CP22 Oahu
AG14 Oahu
CP141 KZ102 Kauai
CP142 MK1031 Kauai
JW14 Oahu

kurumi - rather then just provide an answer let's try to help the user learn something.

Frank I appreciate your approach and Karumi well done!!

[ctsgnb@shell ~]$ cat yourfile
CP19 Oahu - Maunawili Falls
CP20 Oahu - Maunawili Falls
AG12 Oahu - Maunawili Falls
CP22 Oahu - Maunawili Falls, Local area
AG14 Oahu
CP141 KZ102 Kauai - Kokee Milolii Road
CP142 MK1031 Kauai - Kokee Milolii Road
JW14 Oahu - Moana Lua

[ctsgnb@shell ~]$ awk -F"-" '{print$1}' yourfile
CP19 Oahu
CP20 Oahu
AG12 Oahu
CP22 Oahu
AG14 Oahu
CP141 KZ102 Kauai
CP142 MK1031 Kauai
JW14 Oahu

[ctsgnb@shell ~]$ sed -e 's|-.*||' yourfile
CP19 Oahu
CP20 Oahu
AG12 Oahu
CP22 Oahu
AG14 Oahu
CP141 KZ102 Kauai
CP142 MK1031 Kauai
JW14 Oahu

[ctsgnb@shell ~]$ cut -d'-' -f1 yourfile
CP19 Oahu
CP20 Oahu
AG12 Oahu
CP22 Oahu
AG14 Oahu
CP141 KZ102 Kauai
CP142 MK1031 Kauai
JW14 Oahu

[ctsgnb@shell ~]$

@ScrutiDude

Damned ! i forgot that one ! LoL

Repost:

while read line
do 
  echo "${line%-*}"
done < infile