Basic String Operations
The shell allows some common string operations which can be very useful for script writing.
The shell allows some common string operations which can be very useful for script writing.
String Length#
# 1234567890123456
STRING="this is a string"
echo ${#STRING} # 16Index#
Find the numerical position in $STRING of any single character in $SUBSTRING that matches. Note that the ‘expr’ command is used in this case.
STRING="this is a string"
SUBSTRING="hat"
expr index "$STRING" "$SUBSTRING" # 1 is the position of the first 't' in $STRINGSubstring Extraction#
Extract substring of length $LEN from $STRING starting after position $POS. Note that first position is 0.
STRING="this is a string"
POS=1
LEN=3
echo ${STRING:$POS:$LEN} # hisIf :$LEN is omitted, extract substring from $POS to end of line
STRING="this is a string"
echo ${STRING:1} # $STRING contents without leading character
echo ${STRING:12} # ringSimple data extraction example:#
# Code to extract the First name from the data record
DATARECORD="last=Clifford,first=Johnny Boy,state=CA"
COMMA1=`expr index "$DATARECORD" ','` # 14 position of first comma
CHOP1FIELD=${DATARECORD:$COMMA1} #
COMMA2=`expr index "$CHOP1FIELD" ','`
LENGTH=`expr $COMMA2 - 6 - 1`
FIRSTNAME=${CHOP1FIELD:6:$LENGTH} # Johnny Boy
echo $FIRSTNAMESubstring Replacement#
STRING="to be or not to be"Replace first occurrence of substring with replacement
STRING="to be or not to be"
echo ${STRING[@]/be/eat} # to eat or not to beReplace all occurrences of substring
STRING="to be or not to be"
echo ${STRING[@]//be/eat} # to eat or not to eatDelete all occurrences of substring (replace with empty string)
STRING="to be or not to be"
echo ${STRING[@]// not/} # to be or to beReplace occurrence of substring if at the beginning of $STRING
STRING="to be or not to be"
echo ${STRING[@]/#to be/eat now} # eat now or not to beReplace occurrence of substring if at the end of $STRING
STRING="to be or not to be"
echo ${STRING[@]/%be/eat} # to be or not to eatreplace occurrence of substring with shell command output
STRING="to be or not to be"
echo ${STRING[@]/%be/be on $(date +%Y-%m-%d)} # to be or not to be on 2012-06-14Try it
Expected output
Warren Buffett said:
Life is like a snowball. The important thing is finding wet snow and a really long hill.
and I say:
Life is like a football. The important thing is getting wetSolution
#!/bin/bash
BUFFETT="Life is like a snowball. The important thing is finding wet snow and a really long hill."
# write your code here
ISAY=$BUFFETT
change1=${ISAY[@]/snow/foot}
change2=${change1[@]//snow/}
change3=${change2[@]/finding/getting}
loc=`expr index "$change3" 'w'`
ISAY=${change3::$loc+2}
# Test code - do not modify
echo "Warren Buffett said:"
echo $BUFFETT
echo "and I say:"
echo "$ISAY"Get the Bash agent pack
A battle-tested AGENTS.md, the review checklist, and the failure-mode cheat sheet for Bash. One email, then occasional updates when the tooling shifts. No course pitch.
AGENTS.md now — no email needed.