split a filename and print to 2 different headings

I need help to split a filename 'a0crk_user:A0-B0123$#%test' into a0crk_user and A0-B0123 and print the output under 2 different columns namely
User and Status.
for eg. the output should like below:
User Status
---- ------
a0crk_user A0-B0123

There are several possibilities to achieve this: all these filenames are of the form

<first_part><delimiter><second_part>

and you want to split them at <delimiter>. This chan be achieved by:

  1. using shell means

${varname#<delimiter>} will expand to the part of varname following delimiter
${varname%<delimiter>
} will expand to the part of varname preceeding delimiter

Example:

a="abc|def"
print - ${a#*|}    # will yield "def"
print - ${a%|*}    # will yield "abc"
  1. using cut

You can use "cut" to split a string at some "field boundaries" delimited by a delimiter character. Quite commonly this is a blank but this doesn't have to be so. See the manpage for "cut" for details:

a="abc|def"
print - $a | cut -d'|' -f1    # will yield "abc"
print - $a | cut -d'|' -f2    # will yield "def"

This would be the preferential method when you have to split your variable not one but several times. In this case you could split to "field" 3,4,5, etc..

  1. using sed/awk

You could use sed or awk to split your variables content into parts. This would be the least preferable method as it would be an overkill for such a problem.

a="abc|def"
print - $a | sed 's/|.*$//'    # will yield "abc"
print - $a | sed 's/^.*|//'    # will yield "def"

After having split your variables into parts you can use "printf" to print the parts as column headers in a table. "printf" has also a manpage and works almost identical to the standard C function printf().

I hope this helps.

bakunin

Other possibilities. With sed:

sed 's/\(.*\):\(.*\)\$.*/User status\n---- ----\n\1 \2/'

With awk:

awk -F"[:$]" '{printf("User Status\n---- ----\n%s %s",$1,$2)}'

If you get errors with awk use nawk, gawk or /usr/xpg4/bin/awk on Solaris.

Regards

Thanks to both of you.