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:
1-2
3-4
55-66
4323-12312893
into the following
1_2
3_4
55_66
4323_12312893
Any help would be appreciated!
Yoda
January 22, 2014, 10:21pm
2
sed 's#\([0-9]\+\)\(-\)\([0-9]\+\)#\1_\3#' file
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 ----------
sed 's/\([0-9]\)-\([-0-9]\)/\1\_\2/g'
seems to work. thanks for getting me started. from: http://www.windowslinuxosx.com/q/answers-use-sed-command-to-replace-appearing-between-numbers-224042.html
I don't know how is your real input, for give input this will work
$ cat <<test | awk 'gsub(/-/,"_")'
1-2
3-4
55-66
4323-12312893
test
1_2
3_4
55_66
4323_12312893
$ awk 'gsub(/-/,"_")' file
Note: \+
is GNU sed only.
Why not use (ba)sh builtins?
OSX 10.7.5, default bash terminal.
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~> _