Skip to main content
clarify
Source Link
Thomas Dickey
  • 79.2k
  • 9
  • 189
  • 289

A suitable answer depends on how the names might vary. You could transform the names using the shell's built-in parameter substitution if you assume constant field-widths. That's relatively limited in scope.

More interesting would be using character classes in sed:

newname=$(echo "$oldname" | sed -e 's/^\([[:alpha:]]\+\)\([[:digit:]]\+\)_/\1-\2-/')

that is, after a leading alphabetic prefix, add a dash, and then after the digits ending with an underscore, change that to a dash.

Unlike the possible solutions using parameter substitution, this approach allows any (nonzero) length from the alphabetic prefix and digits. So you could have this as input:

ABS1789_2563-01
ABS1789_2563-02
ABS1789_2563-02
ABSOLUTE1789_2563-01
ABSURD1789_2563-02
ABSOLVE1789_2563-02
PREFIX1793939389_2563-02

put that in a script

#!/bin/sh
for oldname in `cat foo4.txt`
do
newname=$(echo "$oldname" | sed -e 's/^\([[:alpha:]]\+\)\([[:digit:]]\+\)_/\1-\2-/')
echo "$oldname ->$newname"
done

giving this output (in a suitable loop):

ABS1789_2563-01 ->ABS-1789-2563-01
ABS1789_2563-02 ->ABS-1789-2563-02
ABS1789_2563-02 ->ABS-1789-2563-02
ABSOLUTE1789_2563-01 ->ABSOLUTE-1789-2563-01
ABSURD1789_2563-02 ->ABSURD-1789-2563-02
ABSOLVE1789_2563-02 ->ABSOLVE-1789-2563-02
PREFIX1793939389_2563-02 ->PREFIX-1793939389-2563-02

A suitable answer depends on how the names might vary. You could transform the names using the shell's built-in parameter substitution if you assume constant field-widths. That's relatively limited in scope.

More interesting would be using character classes in sed:

newname=$(echo "$oldname" | sed -e 's/^\([[:alpha:]]\+\)\([[:digit:]]\+\)_/\1-\2-/')

that is, after a leading alphabetic prefix, add a dash, and then after the digits ending with an underscore, change that to a dash.

A suitable answer depends on how the names might vary. You could transform the names using the shell's built-in parameter substitution if you assume constant field-widths. That's relatively limited in scope.

More interesting would be using character classes in sed:

newname=$(echo "$oldname" | sed -e 's/^\([[:alpha:]]\+\)\([[:digit:]]\+\)_/\1-\2-/')

that is, after a leading alphabetic prefix, add a dash, and then after the digits ending with an underscore, change that to a dash.

Unlike the possible solutions using parameter substitution, this approach allows any (nonzero) length from the alphabetic prefix and digits. So you could have this as input:

ABS1789_2563-01
ABS1789_2563-02
ABS1789_2563-02
ABSOLUTE1789_2563-01
ABSURD1789_2563-02
ABSOLVE1789_2563-02
PREFIX1793939389_2563-02

put that in a script

#!/bin/sh
for oldname in `cat foo4.txt`
do
newname=$(echo "$oldname" | sed -e 's/^\([[:alpha:]]\+\)\([[:digit:]]\+\)_/\1-\2-/')
echo "$oldname ->$newname"
done

giving this output (in a suitable loop):

ABS1789_2563-01 ->ABS-1789-2563-01
ABS1789_2563-02 ->ABS-1789-2563-02
ABS1789_2563-02 ->ABS-1789-2563-02
ABSOLUTE1789_2563-01 ->ABSOLUTE-1789-2563-01
ABSURD1789_2563-02 ->ABSURD-1789-2563-02
ABSOLVE1789_2563-02 ->ABSOLVE-1789-2563-02
PREFIX1793939389_2563-02 ->PREFIX-1793939389-2563-02
Source Link
Thomas Dickey
  • 79.2k
  • 9
  • 189
  • 289

A suitable answer depends on how the names might vary. You could transform the names using the shell's built-in parameter substitution if you assume constant field-widths. That's relatively limited in scope.

More interesting would be using character classes in sed:

newname=$(echo "$oldname" | sed -e 's/^\([[:alpha:]]\+\)\([[:digit:]]\+\)_/\1-\2-/')

that is, after a leading alphabetic prefix, add a dash, and then after the digits ending with an underscore, change that to a dash.