I have text file format like this
SKI
SKII
SKIII
SKIV
SKV
SKVI
SKVII
I want to convert it with sed command into numerical. Can you help me out?
SK1
SK2
SK3
SK4
SK5
SK6
SK开发者_JS百科7
Thanks a lot!
There's no trivial way to do that. Brute force plus some care is required:
sed -e 's/SKIII/SK3/' \
-e 's/SKII/SK2/' \
-e 's/SKIV/SK4/' \
-e 's/SKVIII/SK8/' \
-e 's/SKVII/SK7/' \
-e 's/SKVI/SK6/' \
-e 's/SKV/SK5/' \
-e 's/SKI/SK1/' "$@"
The longer sequences must precede the shorter ones, in general. And this only deals with 1-8; generalizing it is modestly hard.
Actually the numbers are needed from 1 to 29. How can I deal with it? Same method?
If your forego the SK prefix, then with a little care, you do not need 29 separate -e
expressions (though that would work, of course). This script will take you to 39 (tested to 31, the maximum number of days in a month); the extensions needed to get to 99 are fairly obvious, I believe (13 more -e
expressions, I think). Note the special-case handling for numbers containing zeroes.
sed \
-e 's/IX/9/' \
-e 's/XXX$/30/' \
-e 's/XXX/3/' \
-e 's/XX$/20/' \
-e 's/XX/2/' \
-e 's/X$/10/' \
-e 's/X/1/' \
-e 's/VIII/8/' \
-e 's/VII/7/' \
-e 's/III/3/' \
-e 's/II/2/' \
-e 's/IV/4/' \
-e 's/VI/6/' \
-e 's/V/5/' \
-e 's/I/1/' "$@"
精彩评论