bash variable string operations
$ dummy=ABCDEFGHIJ
$ echo ${#dummy} # string length '#'
10
$ echo ${dummy:0:2} # slice w/ offset and [length]
AB
$ echo ${dummy:5} # default length: all
FGHIJ
$ echo ${dummy:-3} # no space before '-', not working
ABCDEFGHIJ
$ echo ${dummy: -3} # MUST have space, it matters
HIJ
$ echo ${dummy: -7:4}
DEFG
# pipe to other commands
$ echo $dummy | tail -c 3 # character, last 3 including '\n'
IJ
$ echo $dummy | cut -c2-5 # 'cut' to specify character positions
BCDE
$ echo $dummy | cut -c2,4-5
BDE
$ echo $dummy | grep -o ...$ # last 3 any characters
HIJ
$ echo $dummy | egrep -o '.{1,3}$'
HIJ
Labels: bash, egrep, length, script, slice, string, tail, variable
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home