开发者

Extracting a string from a file name

My script takes a file name in the form R#TYPE.TXT (# is a number and TYPE is two or three characters). I want my script to give me TYPE. What should I do to get it? Guess I need to use awk and sed.

I'm using /bin/sh (w开发者_如何学运维hich is a requirement)


you can use awk

$ echo R1CcC.TXT | awk '{sub(/.*[0-9]/,"");sub(".TXT","")}{print}'
CcC

or

$ echo R1CcC.TXT | awk '{gsub(/.*[0-9]|\.TXT$/,"");print}'
CcC

and if sed is really what you want

$ echo R9XXX.TXT | sed 's/R[0-9]\(.*\)\.TXT/\1/'
XXX


I think this is what you are looking for.

$ echo R3cf.txt | sed "s/.[0-9]\(.*\)\..*/\1/"

cf

If txt is always upper case and the filename always starts with R you could do something like.

$ echo R3cf.txt | sed "s/R[0-9]\(.*\)\.TXT/\1/"


You can use just the shell (depending what shell your bin/sh is:

f=R9ABC.TXT
f="${f%.TXT}"       # remove the extension
type="${f#R[0-9]}"  # remove the first bit
echo "$type"        # ==> ABC
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜