On 5/13/08, Dr. Werner Fink <[EMAIL PROTECTED]> wrote:
> On Tue, May 13, 2008 at 01:18:40PM +0200, William James wrote:
>  > Imagine I have a file like this:
>  >
>  > buf="$(cat <<EOF
>  > part1: a part
>  > part5: another part
>  > # comment
>  > part9: 9th part
>  > EOF
>  > )"
>  >
>  > How can I get the line containing 'part5' using ksh93 string
>  > operators, avoiding both 'while read...' and 'sed'? I tried
>  > ${buf##pattern}, ${buf#pattern}, ${buf%pattern}, ${buf%%pattern} but
>  > they all _remove_ the string pattern. I need a string operator which
>  > only leaves the string matching pattern and removes the rest.
>
>
> Not a string operator but this seems to work:
>
>
>   buf="$(cat <<EOF
>   part1: a part
>   part5: another part
>   # comment
>   part9: 9th part
>   EOF
>   )"
>
>
>   OIFS="$IFS"
>   IFS=$'\n'
>   lines=(${buf})
>   IFS="$OIFS"
>
>   typeset -i [EMAIL PROTECTED]
>   while let i-- ; do
>       case "${lines[$i]}" in
>       part5:*)
>           echo "${lines[$i]}"
>           break
>           ;;
>       esac
>   done
>
>  on the other hand, a read loop within a pipe or simply a loop
>  with reading from stdin on the file seems much faster:
>
>   while read line ; do
>       case "$line" in
>       part5:*)
>           echo "$line"
>           break
>           ;;
>       esac
>   done <<EOF

I wish to avoid using read. /bin/sed can cut and paste fields based on
pattern and I'm searching for something similar in ksh93, i.e. the
reverse operation of ${buf##pattern}, instead of removing pattern
remove all characters which do not match pattern.

Cheers,
William
-- 
    @,,@   William James
   (\--/)  [EMAIL PROTECTED]
  (.>__<.) GNU/Solaris hacker
_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users

Reply via email to