Pages

lundi 11 juillet 2011

csh string manipulation: length, substring, index ...

1) get string length
set object = "Processing NGC 2345"
set nchar = `echo $object | awk '{print length($0)}'`

2) substring
One method uses the awk function substr(s,c,n). This returns the substring from string s starting from character position c up to a maximum length of n characters. If n is not supplied, the rest of the string from c is returned.

set caption = "Processing NGC 2345."
set object = `echo $caption | awk '{print substr($0,12,8)}'` # = "NGC 2345"
set objec_ = `echo $caption | awk '{print substr($0,16)}'` # = "2345."

Another method uses the UNIX cut command. It too can specify a range or ranges of characters. It can also extract fields separated by nominated characters.
The -d qualifier specifies the delimiter between associated data (otherwise called fields). Note the the space delimiter must be quoted. The -f qualifier selects the fields. You can also select character columns with the -c qualifier. Both -c and -f can comprise a comma-separated list of individual values and/or ranges of values separated by a hyphen. As you might expect, cut can take its input from files too.
set places = ( Jupiter "Eagle Nebula" "Gamma quadrant" )

set cut1 = `echo $places | cut -d ' ' -f1,3` # = "Jupiter Nebula"
set cut2 = `echo $places[3] | cut -d a -f2` # = "mm"
set cut3 = `echo $places | cut -c3,11` # = "pg"

3) find the index of a substring
This requires the awk function index. This returns zero if the string could not be located. Note that comparisons are case sensitive.

set catal = "NGC "
set number = 2345
set object = "Processing $catal$number."
set cind = `echo $object | awk '{print index($0,"ngc")}'` # = 0
set cind = `echo $object | awk '{print index($0,"NGC")}'` # = 12


A good csh cook book is found http://star-www.rl.ac.uk/star/docs/sc4.htx/sc4.html. I extracted a few examples here.

Aucun commentaire:

Enregistrer un commentaire