-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_ops.sh
49 lines (34 loc) · 1.29 KB
/
string_ops.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/bash
STRING="this is a string"
echo ${#STRING}
SUBSTRING="hat"
#`expr index "$STRING" "$SUBSTRING"` - Revisit this
# Substring Extraction
POS=1
LEN=3
echo ${STRING:$POS:$LEN}
# IF $LEN is omitted, extract from $POS to end of line
echo ${STRING:1}
echo ${STRING:12}
# Code to extract the Firstname from a data record
#DATARECORD="last=Clifford,first=Johnny Boy,state=CA"
#COMMA1=`expr index "$DATARECORD" ','`
#CUTOFF1=${DATARECORD:$COMMA1}
#COMMA2=`expr index "$CUTOFF" ','`
#echo $DATARECORD
#echo $COMMA1
#echo $CUTOFF1
#echo $COMMA2
# expr doesn't work as per the tutorial - will revisit later
NEW_STRING="to be or not to be"
echo ${NEW_STRING[@]/be/eat} # Replace the first occurrence of substring
NEW_STRING="to be or not to be"
echo ${NEW_STRING[@]//be/eat} # Replace all occurrences of the substring
NEW_STRING="to be or not to be"
echo ${NEW_STRING[@]// not/} # Delete all occurrences of the substring
NEW_STRING="to be or not to be"
echo ${NEW_STRING[@]/#to be/eat now} # Replace occurrence of substring if at the beginning
NEW_STRING="to be or not to be"
echo ${NEW_STRING[@]/%be/eat} # Replace occurrence of substring if at the end
NEW_STRING="to be or not to be"
echo ${NEW_STRING[@]/%be/be on $(date +%Y-%m-%d)} # replace occurrence of substring with shell command output