Removing hyphen from beginning and end of a word only.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing hyphen from beginning and end of a word only.
# 1  
Old 02-05-2014
Removing hyphen from beginning and end of a word only.

It is very simple to remove a hyphen from a word anywhere in that word using a simple sed command (sed -i 's/-//g' filename), but I am not able to figure out how to do this:

For example,
Code:
apple
-orange
tree
pipe-
banana-shake
dupe-

What my output should look like:
Code:
apple
orange
tree
pipe
banana-shake
dupe

I want to do this in Linux with BASH.
# 2  
Old 02-05-2014
You can also use sed for the task:

Code:
sed -e 's/^-//' -e 's/-$//' filename

This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Selecting text on multiple lines, then removing a beginning and end patterns

I have a file similar to the below. I am selecting only the paragraphs with @inlineifset. I am using the following command sed '/@inlineifset/,/^ *$/!d; s/@inlineifset{mrg, @btpar{@//' $flnm >> $ofln This produces @section Correlations between seismograms,,,,}} ... (5 Replies)
Discussion started by: Danette
5 Replies

2. Shell Programming and Scripting

How to append in the beginning not at the end?

Hi, I now that >> will append text to the end of the text that is already inside the file. How to append the new text infront of the text that is already in the file. Thanks for any input. Regards, Chandu (3 Replies)
Discussion started by: chandrakanth
3 Replies

3. Shell Programming and Scripting

Add words in beginning , end after removing a word in a file

My file has the entries like below... /dev/sds /dev/sdak /dev/sdbc /dev/sdbu I want to make the file like below echo 1 > /sys/block/sds/device/rescan echo 1 > /sys/block/sdak/device/rescan echo 1 > /sys/block/sdbc/device/rescan echo 1 > /sys/block/sdbu/device/rescan (2 Replies)
Discussion started by: saravanapandi
2 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Appending beginning of filename to end

Hi Guys, I have serveral directories like this: (2013) blablabla(blabla) - blabla (blabla) or (1997) blablabla(blabla) - blabla (blabla) and have to rename them to something like that: blablabla(blabla) - blabla (blabla) (2013) and blablabla(blabla) - blabla (blabla) (1997) Easy... (2 Replies)
Discussion started by: Nateshift
2 Replies

5. Shell Programming and Scripting

trying to add text to beginning and end of each line

Well here goes: I tried to write a batch file that adds a specific fixed text to each line of an already existing text file. for the adding text infront of each line I tried this: for /F "delims=" %%j in (list.txt) do echo.STARTTEXT\%%j >> list.txt for adding text after each line I... (0 Replies)
Discussion started by: pasc
0 Replies

6. Programming

sed no match word end with hyphen

Hi, This is my first post.It has two parts first part: I want to match a line that starts with whitespace tab or similar followed by must start with specific word and not match same word start with hyphen like this: grep height file1: height: 150px; line-height: 1.5em; height:... (4 Replies)
Discussion started by: medium_linux
4 Replies

7. Shell Programming and Scripting

Remove comma and next rows beginning from the end

Hello friends, I have a file which consists of many rows, I use a couple of commands to convert it so i can use in a database query for filtering. I need the first columns (msisdns) in a row, seperated with commas, 9855162267,4,5,2010-11-03 17:02:07.627 9594567938f,5,5,2010-11-02... (9 Replies)
Discussion started by: EAGL€
9 Replies

8. Shell Programming and Scripting

Removing from beginning to first : using awk

A have a file npt02-sr40-syn-dc0p014-32x24drw.log:0. Best Value = 0.00144914 npt02-sr40-syn-dc0p014-32x24drw.log:1. Best Value = 0.00115706 npt02-sr40-syn-dc0p014-32x24drw.log:2. Best Value = 0.00094345 npt02-sr40-syn-dc0p014-32x24drw.log:3. Best Value = 0.000925552... (11 Replies)
Discussion started by: kristinu
11 Replies

9. UNIX for Dummies Questions & Answers

cat a file from end to beginning

Is there an option, for cat, head, tail, or is there any way, to display a file from last line to first? For example, my file looks like this: aaaa bbbb cccc eeee and I would like to print or display it like this: eeee cccc bbbb aaaa thanks (5 Replies)
Discussion started by: jpprial
5 Replies
Login or Register to Ask a Question