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
  part1: a part
  part5: another part
  # comment
  part9: 9th part
  EOF


//Werner

-- 
  "Having a smoking section in a restaurant is like having
          a peeing section in a swimming pool." -- Edward Burr
_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users

Reply via email to