Results 1 to 2 of 2

Thread: Read field by field between field separator

  1. #1
    Join Date
    Apr 2013
    Beans
    4

    Read field by field between field separator

    Hi, is there a way to read the fields between the field separator? I mean, for example, i have a file structured in this way:
    Code:
    #@;field1#@;field2#@;field3#@;field4#@;field5#@;field6
    ok, now i want to make something like this :
    Code:
    temp=${ENDROW##*#@;}          field1=`printf '"%s"\n' "${temp%%#@;*}"
    where ENDROW is because i made a while condition before:
    Code:
    ENDROW=""
    while read ENDROW;
    do
    temp=${ENDROW##*#@;}          field1=`printf '"%s"\n' "${temp%%#@;*}"
    So i have to read the first field and find that between the first #@; and the second #@; there is the filed1 and go on in this way. The solution i posted doesn't go and the fields are empty any suggestion?Thanks

  2. #2
    Join Date
    Apr 2012
    Beans
    5,158

    Re: Read field by field between field separator

    Not sure what you're asking exactly? you can read into an array something like

    Code:
    IFS=";" read -a array < fieldfile
    Then strip the #@ substrings with ${field//#@/}

    Code:
    $ for field in ${array[@]}; do echo "${field//#@/}"; done
    
    field1
    field2
    field3
    field4
    field5
    field6
    Or you could try

    Code:
    IFS="#@;" read -a array < fieldfile
    then you don't need to strip anything (and also don't get the empty 0th field)

    Code:
    $ for field in ${array[@]}; do echo "$field"; done
    field1
    field2
    field3
    field4
    field5
    field6

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •