It is currently Tue, 07 Dec 2021 02:20:35 GMT



 
Author Message
 Read line at at time and split up that line
I am using ksh on Solaris and want to know is there a more acceptable
way to read a line from an input file and divide it up based on field
seperators than cut ?

ie

while read -r LINE; do
    RT="`echo "${LINE}" | cut -d\| -f1`"
    if [ "${RT}" = "MSH" ]; then
        MSH="${LINE}"
    fi
done < ${HL7_FILE}

Thanks in advance,
-Greg



 Tue, 29 Apr 2008 01:25:16 GMT   
 Read line at at time and split up that line

while IFS=\| read -r RT LINE; do
     if [ "${RT}" = "MSH" ]; then
         MSH="${RT}|${LINE}"
     fi
done < "${HL7_FILE}"

or:

oIFS="$IFS"
IFS="|"
while read -r LINE; do
     set -- $LINE
     if [ "$$1" = "MSH" ]; then
         MSH="${LINE}"
     fi
done < "${HL7_FILE}"
IFS="$oIFS"

Regards,

        Ed.



 Tue, 29 Apr 2008 01:39:01 GMT   
 Read line at at time and split up that line

<snip>
if [ "$1" = "MSH" ]; then

        Ed.



 Tue, 29 Apr 2008 01:43:36 GMT   
 Read line at at time and split up that line

while IFS=\| read -r RT LINE ; do ## Use more variables if you need them
    if [ "${RT}" = "MSH" ]; then
        MSH="$RT|$LINE"
    fi
done < ${HL7_FILE}

   Or:

while read -r LINE; do
    RT="${LINE%%\|*}"
    if [ "${RT}" = "MSH" ]; then
        MSH="${LINE}"
    fi
done < ${HL7_FILE}

   You can also split a LINE by changing IFS:

IFS=\|
set -f -- $LINE
RT=$1
F2=$2
: etc....

   If all you want to do is assign the line if it begins with MSH, you
   don't need to split it up:

case $LINE in
     MSH\|*) MSH=$LINE ;;
esac

--
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |          is released under the
   2005, Apress                 |     GNU General Public Licence



 Tue, 29 Apr 2008 01:54:19 GMT   
 Read line at at time and split up that line
Thanks, all.  This gives me a lot more to think about.  There are some
good examples in here too.
-Greg


 Tue, 29 Apr 2008 03:41:47 GMT   
 Read line at at time and split up that line
How about in the following case, where if the line begins with CD2, I
need to split it up farter for additional processing...
    if [ "${RT}" = "CD2" ]; then
## Identify some fields from the record
## NAME = Manufacturer name
        NAME="`echo "${LINE}" | cut -d\| -f13`"
        NAME="`echo "${NAME}" | sed -e 's/\^\^/\^/g'`"
        TMP3="`echo "${LINE}" | cut -d\| -f1-12`"
        TMP4="`echo "${LINE}" | cut -d\| -f14-36`"
        LINE="${TMP3}|${NAME}|${TMP4}"
        STOR2="${LINE}"
    fi

I'm not looking for someone to do my work, I'm just looking for the
best (most efficient) way to do this.  the HL7 input file is HUGE, and
I need the script to run as quickly as I can make it.

Again, thanks for all of your help.
-Greg



 Tue, 29 Apr 2008 03:53:14 GMT   
 Read line at at time and split up that line
I've tried a combination of changing IFS, and the case statement...
Now the script is running and running, it gets a segmentation fault
(several times) and never produces any output.

I'm going to have to troubleshoot this further.

Any clues as to why I'm getting segmentation faults?  I'm not very good
at analyzing core dumps.



 Tue, 29 Apr 2008 23:47:40 GMT   
 Read line at at time and split up that line

"the" script? which one?

Use -x flag to start the interpreting shell.

Yes, because the process tries to access a memory region which it is
not allowed to access.

If you followed this newsgroup for a while you will have noticed that
the people whom you ask for help want context as it's standard in
Usenet groups. It is considered inacceptable to force people to look
through pervious postings to collect any information.

Janis



 Wed, 30 Apr 2008 00:36:04 GMT   
 Read line at at time and split up that line

What are you actually running?
Which process dumps core? file core should tell you that.

--
John.



 Wed, 30 Apr 2008 00:44:33 GMT   
 Read line at at time and split up that line
Greg,

I would do something like this:

exec 3<${HL7_FILE}

while read -u3 LINE
do

  oIFS="${IFS}"
  set -a ${LINE}
  IFS="${oIFS}"

  case "${1}" in
    MSH)
      MSH="${LINE}" ;;
    CD2)
      NAME=$(echo ${13} | sed -e ''s/\^\^/\^/g') ;
      TMP3="${12}" ;
      unset TMP4 ;
      typeset -i i=14 ;
      while (( i <= 36 ))
      do
        eval TMP4="${TMP4}\${${i}}"
        (( i += 1 ))
      done ;
      STOR2="${LINE}" ;;
    *)
      continue ;;
  esac

done

You can add as many conditions to the case statement as needed.

Hope that helps ...

Chris



 Wed, 30 Apr 2008 00:42:23 GMT   
 Read line at at time and split up that line

  Troubleshoot what? Please read: <http://cfaj.freeshell.org/google>.

--
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |          is released under the
   2005, Apress                 |     GNU General Public Licence



 Wed, 30 Apr 2008 01:00:31 GMT   
 Read line at at time and split up that line

First of all, you might want to try using ksh93.  On new Solaris
systems it's /usr/dt/bin/dtksh

So, what's the above supposed to be doing?
Try stepping through it.

Also, what's the point?

It's not needed for the case statement to do the match.

ksh93 has substring syntax which completely eliminates
the set/cut whatever.

for ((i = 14; i <=36; i++))

Basically, the above is rather knarly at best.

You'd be better off listing some of the input and what
you wanted as output -- and buying and reading a ksh93 book --
than to continue with this.



 Wed, 30 Apr 2008 01:15:53 GMT   
 Read line at at time and split up that line
I didn't want to make the assumption that he was using ksh93.

Some people (unfortunately I am one of them) do not have this by
default - thanks to companies like HP.

You'll find that my script is POSIX compliant should work with just
about any shell that claims to be POSIX compliant.



 Wed, 30 Apr 2008 04:29:36 GMT   
 Read line at at time and split up that line

    What script was that? Please read: <http://cfaj.freeshell.org/google>.

    IIRC, the script you posted earlier, was not POSIX compliant, and
    would tun in bash and ksh, but probably no other shells.

--
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |          is released under the
   2005, Apress                 |     GNU General Public Licence



 Wed, 30 Apr 2008 07:21:51 GMT   
 Read line at at time and split up that line
Chris,

Thanks for pointing out the posting etiquette. I did not realize Google
was not quoting the original message when replying.

Thanks,
Chris



 Sun, 04 May 2008 03:31:29 GMT   
 
   [ 16 post ]  Go to page: [1] [2]

Similar Threads

1. line by line reading of the lines in list

2. Line splitting long lines

3. How to split a single line into multiple lines of 10 characters each

4. how to split input line to one character per line with just shell

5. read from stdin while reading line by line a file

6. Reading a script file line by line.

7. How to read line by line in csh

8. Using read within a loop that parses a file line by line

9. read pipe line by line

10. reading files line-be-line?


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software