Mittwoch, 13. April 2011

Shell 005: String processing

A directory contains the following

~/shell $ ls -l
00_01_boxpdb.py
00_01_realign.py
00_02_callPDB2PQR.py
00_03_getPointCharges.py
00_04_getCharge.py
01_01_connect.py


How can the leading terms be removed?


~/shell $ for i in 0*
> do
> mv $i ${i:6}
> done


The syntax is ${i:FROM:BY}. The indexing starts from 0. Also

# Is it possible to index from the right end of the string?

echo ${stringZ:-4} # abcABC123ABCabc
# Defaults to full string, as in ${parameter:-default}.
# However . . .

echo ${stringZ:(-4)} # Cabc
echo ${stringZ: -4} # Cabc
# Now, it works.
# Parentheses or added space "escape" the position parameter.

Keine Kommentare:

Kommentar veröffentlichen