Unix Shell - String Parsing in sh

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2005 > String Parsing in sh





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author String Parsing in sh
Muthu Kumar G

2005-11-26, 7:49 am

Hi,
I would like to parse a string abc_def_ghi either based on some
delimter like '_' which has to give me the output as
abc
def
ghi

(OR)

is there anyother way to read the string 'abcdefghi' and parse the
string by 3 characters so that the output will be
abc
def
ghi

thanks
Muthu Kumar G.

Chris F.A. Johnson

2005-11-26, 5:50 pm

On 2005-11-26, Muthu Kumar G wrote:
> Hi,
> I would like to parse a string abc_def_ghi either based on some
> delimter like '_' which has to give me the output as
> abc
> def
> ghi


string=abc_def_ghi
IFS=_
printf "%s\n" $string

Or:

set -f -- $string ## the three parts are now in $1, $2 and $3
printf "%s\n" "$@"

> (OR)
>
> is there anyother way to read the string 'abcdefghi' and parse the
> string by 3 characters so that the output will be
> abc
> def
> ghi


string=abcdefghi
b=${string#???}
printf "%s\n" ${string%"$b"}
printf "%s\n" "${b%???}"
printf "%s\n" "${b#???}"

--
Chris F.A. Johnson <http://cfaj.freeshell.org>
========================================
======================================
Author, Shell Scripting Recipes: A Problem-Solution Approach 2005, Apress
Any code of mine in this post is released under the GNU General Public Licence
William Park

2005-11-27, 2:49 am

Muthu Kumar G <g.muthukumar@gmail.com> wrote:
> Hi,
> I would like to parse a string abc_def_ghi either based on some
> delimter like '_' which has to give me the output as
> abc
> def
> ghi
>
> (OR)
>
> is there anyother way to read the string 'abcdefghi' and parse the
> string by 3 characters so that the output will be
> abc
> def
> ghi
>
> thanks
> Muthu Kumar G.


1. a='abc_def_ghi'
printf '%s\n' ${a|,_}
printf '%s\n' ${a|-_}

2. b='abcdefghi'
printf '%s\n' ${b|+...}

--
William Park <opengeometry@yahoo.ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com