How to remove text in each line after hyphen?


 
Thread Tools Search this Thread
Special Forums UNIX Desktop Questions & Answers How to remove text in each line after hyphen?
# 1  
Old 09-20-2010
Data 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
# 2  
Old 09-20-2010
Please post what you tried and what error you got.
# 3  
Old 09-20-2010
Code:
$ 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

# 4  
Old 09-20-2010
kurumi - rather then just provide an answer let's try to help the user learn something.
# 5  
Old 10-18-2010
Frank I appreciate your approach and Karumi well done!!
# 6  
Old 10-18-2010
Code:
[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 ~]$

# 7  
Old 10-18-2010
@ScrutiDude

Damned ! i forgot that one ! LoL
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print unique text in field before hyphen

Trying to print the unique values in $2 before the -, currently the count is displayed. Hopefully, the below is close. Thank you :). file chr2:46603668-46603902 EPAS1-902|gc=54.3 253.1 chr2:211471445-211471675 CPS1-1205|gc=48.3 264.7 chr19:15291762-15291983 NOTCH3-1003|gc=68.8 195.8... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Grep command to ignore line starting with hyphen

Hi, I want to read a file line by line and exclude the lines that are beginning with special characters. The below code is working fine except when the line starts with hyphen (-) in the file. for TEST in `cat $FILE | grep -E -v '#|/+' | awk '{FS=":"}NF > 0{print $1}'` do . . done How... (4 Replies)
Discussion started by: Srinraj Rao
4 Replies

3. Shell Programming and Scripting

How to remove blank line from a text file?

Hi All, I am creating a text file using perl. The first record I am writing as "$line" and all the other as "\n$line". At the end the file is having N number of lines. I am using this file for MLOAD (Teradata), which is reading N+1 lines in the file and failing.I am not able to find new line... (2 Replies)
Discussion started by: unankix
2 Replies

4. Shell Programming and Scripting

Script to remove line of text

Hello, I have a line that needs to be removed using sed -i to perform the replace in place. The issue that I have is the files on each server may contain extra rows after the last logger definition. So I can't count accurately from the last row. So from the example the line that contains... (8 Replies)
Discussion started by: jaysunn
8 Replies

5. Shell Programming and Scripting

[Help] Remove text line containing *

Hi everybody, I have a perl script that I used to collect traceroute data from some site. It create output on the text file. The problem is , some site usually timed out when it's being tracerouted. And it show * * * on the line. Here is the example hop list to www.example.com 192.168.3.1... (3 Replies)
Discussion started by: franzramadhan
3 Replies

6. Shell Programming and Scripting

how to remove all text including 2 certain character in each line!

Hi I have a file which has aroun 200 line and it is like this: GROUP2-WDI">GROUP2-WDI GROUP3-WDI">GROUP3-WDI KL2P0508BC">KL2P0508BC KL2P0508BIT">KL2P0508BIT KL3P0506BC">KL3P0506BC KL3P0506BUS">KL3P0506BUS KLD1F0507DBT">KLD1F0507DBT KLD1F0507DIT">KLD1F0507DIT KLD1F0510DBT">KLD1F0510DBT... (3 Replies)
Discussion started by: digitalmahdi
3 Replies

7. Shell Programming and Scripting

remove specified line from text

hi, I wish to move a specified line from a text file: ltoremove=5 #remove 5th line i=1 while read line ; do if ; then ... i=$(($i+1)) done < "txt.txt" how should this actually be done? is there a shorter faster way to do this? :wall: Thanks, Ted (2 Replies)
Discussion started by: ted_chou12
2 Replies

8. UNIX for Dummies Questions & Answers

Remove text in line

Hi, i have a text file with some links and i want to remove some characters from it. My text file is something like this: <p> <b> Arquivo CASC2270.09o transferido com sucesso. </b> </p><p> <b> Verificando CASC2270.09o ... </b> </p><p> <b> Iniciando processamento de CASC2270.09o ... </b>... (11 Replies)
Discussion started by: limadario
11 Replies

9. Shell Programming and Scripting

add a hyphen every 2 characters of every line

I have a text file like this with hundreds of lines: >cat file1.txt 1027123000 1027124000 1127125000 1128140000 1228143000 > all lines are very similar and have exactly 10 digits. I want to separate the digits by twodigit and hyphens....like so, > 10-27-12-30-00 10-27-12-40-00... (7 Replies)
Discussion started by: ajp7701
7 Replies

10. Shell Programming and Scripting

use of hyphen in #! line

In one script i have seen - in #! line can somebody explain the meaning of -(hyphen) here #! /bin/sh - (7 Replies)
Discussion started by: Dhruva
7 Replies
Login or Register to Ask a Question