Replacing a character between two numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing a character between two numbers
# 1  
Old 01-22-2014
Replacing a character between two numbers

Hi, I need to replace a character between two numbers (specifically a - to a _). The problem is that they can be *any* numbers. So, I need a one liner to turn a file like this:

Code:
1-2
3-4
55-66
4323-12312893

into the following
Code:
1_2
3_4
55_66
4323_12312893

Any help would be appreciated!

Last edited by Scrutinizer; 01-23-2014 at 02:05 AM.. Reason: code tags
# 2  
Old 01-22-2014
Code:
sed 's#\([0-9]\+\)\(-\)\([0-9]\+\)#\1_\3#' file

# 3  
Old 01-22-2014
Hm, that doesn't seem to be working. I tried using tr with [:digit:] but with no use. Any other ideas? Sorry I am not proficient with sed

---------- Post updated at 10:37 PM ---------- Previous update was at 10:29 PM ----------

Code:
sed 's/\([0-9]\)-\([-0-9]\)/\1\_\2/g'

seems to work. thanks for getting me started. from: http://www.windowslinuxosx.com/q/ans...rs-224042.html
# 4  
Old 01-23-2014
I don't know how is your real input, for give input this will work

Code:
$ cat <<test | awk 'gsub(/-/,"_")'
1-2
3-4
55-66
4323-12312893
test

1_2
3_4
55_66
4323_12312893

Code:
$ awk 'gsub(/-/,"_")' file

# 5  
Old 01-23-2014
Note: \+ is GNU sed only.
# 6  
Old 01-23-2014
Why not use (ba)sh builtins?
OSX 10.7.5, default bash terminal.
Code:
Last login: Thu Jan 23 08:04:51 on ttys000
AMIGA:barrywalker~> text="1-2
> 3-4
> 55-66
> 4323-12312893"
AMIGA:barrywalker~> echo "$text"
1-2
3-4
55-66
4323-12312893
AMIGA:barrywalker~> text=$(echo "${text//-/_}")
AMIGA:barrywalker~> echo "$text"
1_2
3_4
55_66
4323_12312893
AMIGA:barrywalker~> _

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

replacing by newline character

I have a file (pema)with a single long record which i have to break up into multiple lines Input s1aaaaaaaaaaaaaaaaaaaaaaas1bbbbbbbbbbs1cccccccccc Output s1aaaaaaaaaaaaaaaaaaaaaaa s1bbbbbbbbbb s1cccccccccc m planning to do it by replacing s1 by \ns1 \n is the new line character i... (5 Replies)
Discussion started by: pema.yozer
5 Replies

2. Shell Programming and Scripting

Mapping and replacing numbers in two files

hi i have two files one of the form 1 2 2 45 3 56 4 98 5 6598 6 98 7 10 8 0 9 15 10 56 This file's significance is that it maps the number in first column to that of the number in second column The other file is of the form 1 2 (1 Reply)
Discussion started by: vaibhavkorde
1 Replies

3. Shell Programming and Scripting

Preserving Empty Lines while Replacing Numbers

Greetings, I am using tcsh to write a script that will replace the numbers in a file with a single number, the caveat is that this file has blank lines which are necessary for another step down the line so I need to preserve the blank lines. I have tried sed and awk but both will collapse the... (1 Reply)
Discussion started by: bonesy
1 Replies

4. Shell Programming and Scripting

Replacing numbers in bash scripts

Hi, I have lines in some files that look exactly as below, and the line numbers they occur in are always the same. (Lines 136-139) W 0.00000000 0.00000000 2.00000000 W 0.50000000 0.50000000 2.50000000 W 0.00000000 0.00000000 3.00000000 W 0.50000000 0.50000000 3.50000000 I'd like to... (0 Replies)
Discussion started by: bluesmodular
0 Replies

5. UNIX for Dummies Questions & Answers

replacing a character

Hi All, contents of my file is like this: xxx xxx1 N N N 0 yyy yyy1 Y N N 0 i want to replace 1st N of xxx xxx1 N N N 0 line with Y. i. e i want the output like this: xxx xxx1 Y N N 0 how can i do this? please help. Thanks (8 Replies)
Discussion started by: Usha Shastri
8 Replies

6. UNIX for Dummies Questions & Answers

replacing 1 character with many : tr

Hi All, I would like to know how, iff at all we can, we may use the 'tr' command to replace a single character with multiple characters. eg: if i have a string valued "him", how can i use 'tr' to replace 'i' with "oo" to make "hoom". Just replacing a single character by many. tried:-... (16 Replies)
Discussion started by: hkansal
16 Replies

7. Shell Programming and Scripting

replacing a character with another

hi i have a file and reading line by line, i need to replace 8-15 and 18-27 charaters with character 'x'. Eg: satyasatxxxxxxxsatxxxxxxxxxtyasatyasatyasatyasatyasatya please help thanks Satya (1 Reply)
Discussion started by: Satyak
1 Replies

8. Shell Programming and Scripting

Need help in finding and replacing port numbers.

Hi All, I am trying to write a shell script which firstly will search some files and then increase the port numbers mentioned in them by a certain no. let me clear it with an example- suppose there r few files a,b,c,d.... file a's content- <serverEntries xmi:id="ServerEntry_1"... (3 Replies)
Discussion started by: ankushsingh10
3 Replies

9. Shell Programming and Scripting

replacing numbers greater than 0 with 1

I have some ASCII files containing numerous numbers. What I'd like to do is replace all numbers greater than 0 with 1. Examples of the numbers include: - 000011 and 000042 Thanks (4 Replies)
Discussion started by: vrms
4 Replies

10. UNIX for Dummies Questions & Answers

Replacing the last character

Hi , I need to change the last character of the line: ie: input file: (raj, muthu, mani, Output: (raj, muthu, mani); so i need to change the last comma by the closing braces so help me out in the issue. Thanks in advance. (2 Replies)
Discussion started by: ithirak17
2 Replies
Login or Register to Ask a Question