0

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 (which is a requirement)

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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/"

3 Comments

If I use you example, I get sed: -e expression #1, char 20: invalid reference \1 on `s' command's RHS I rewrite it to: sed "s/R1(.*)\.TXT/\1/"
I believe that if you write it RegEx like that your file name is going to have to start with "R1". Whats your "uname -a"?
upps, took the wrong code. Should be sed "s/R[0-9](.*)\.TXT/\1/"
0

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

1 Comment

On the old unix system I code for I get "bad substitution" when I type f="${f%.TXT}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.