-1

I have the following string, which it will always be 35 characters long:

S202SCTRXBAVCWPJAC001181204120000.N

I would like to cut 3 characters (position 17-19), JAC in this case, to remain only

S202SCTRXBAVCWP001181204120000.N

Is there a way to achieve this in bash?

3

2 Answers 2

4
strIn=S202SCTRXBAVCWPJAC001181204120000.N
strOut=${strIn:0:15}${strIn:18}
echo "$strOut"

...uses only bash-built-in functionality to emit:

S202SCTRXBAVCWP001181204120000.N

...as it emits the first 15 characters starting at position 0, then everything after position 18.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, all seems to be working but not in a case statement. I am trying like this case $FORM in JAC ) BNAME=$(basename $FILE) ;; # FNAME=${BNAME:0:15}${BNAME:18} ;; FNAME=${BNAME/$FORM/} ;; MSGIF="pacs.xxx.sct.s.scf" ;; cp -p $FILE $DDIRD/$FNAME ;; esac and get error ./ScriptRBRS.sh: line 21: syntax error near unexpected token ;;' ./ScriptRBRS.sh: line 21: ` FNAME=${BNAME:0:15}${BNAME:18} ;;'`
You need to use ;, not ;;, to separate multiple commands within the same case entry -- ;; is only for separating completely different cases. So that's a general using-case-correctly issue, not something specific to this answer.
0

Agree with the answer by Charles Duffy. If you know you want specifically "JAC" instead of what indices you want removed, you could do:

str="S202SCTRXBAVCWPJAC001181204120000.N"
echo "${str/JAC/}"

2 Comments

Thank you, all seems to be working but not in a case statement. I am trying like this case $FORM in JAC ) BNAME=$(basename $FILE) ;; # FNAME=${BNAME:0:15}${BNAME:18} ;; FNAME=${BNAME/$FORM/} ;; MSGIF="pacs.xxx.sct.s.scf" ;; cp -p $FILE $DDIRD/$FNAME ;; esac and get error ./ScriptRBRS.sh: line 22: syntax error near unexpected token ;;' ./ScriptRBRS.sh: line 22: ` FNAME=${BNAME/$FORM/} ;;'`
Interesting the OP accepted this answer, as this only works to cut the three characters at position 17-19 if those three characters are JAC, so it works for what the OP said "JAC in this case" but it doesn't work for the general case of characters 17-19 for any string. Charles Duffy's answer works for any case.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.