Friday, August 14, 2015

String manipulation in bash


#!/bin/bash

a="MANUEL ARTURO IZQUIERDO"
l=`echo ${#a}-1 | bc`

for q in `seq 0 $l` 
do 
   echo "|'''"${a:$q:1}"'''"
done


get the output:

 |'''M'''
 |'''A'''
 |'''N'''
 |'''U'''
 |'''E'''
 |'''L'''
 |''' '''
 |'''A'''
 |'''R'''
 |'''T'''
 |'''U'''
 |'''R'''
 |'''O'''
 |''' '''
 |'''I'''
 |'''Z'''
 |'''Q'''
 |'''U'''
 |'''I'''
 |'''E'''
 |'''R'''
 |'''D'''
 |'''O'''

The access to the string goes in the form:

 ${varname:index:lenght}

No comments: