Results 1 to 4 of 4

Thread: Parsing files

  1. #1
    Join Date
    Jan 2006
    Posts
    5

    Thumbs up Unanswered: Parsing files

    Hi,

    I need to read the file, line by line and do some parsing on them and then write the line into some other file.

    But the problem is, if file_one has a new line, below logic will not work. I'm getting only half of file_one into file_two.

    Any ideas? Is there anything like 'EOF' in Shell for reading the file till the end?

    echo "`cat $file_name`" | while read tmp_line
    do
    cc=`echo "$tmp_line" | wc -w`
    # ^^^^^^^^^^^^^^^ ----->cc will get 0 if tmp_line is a new line.
    if [ $cc -lt 1 ]
    then
    echo "File $file_name has been read." >> $LOG_FILE
    break;
    else
    echo "$file_name $tmp_line" >> $FILE_TWO
    fi
    done

  2. #2
    Join Date
    Aug 2006
    Location
    The Netherlands
    Posts
    248
    Hi,

    In:
    Code:
    echo "`cat $file_name`" | while read tmp_line
    do
        cc=`echo "$tmp_line" | wc -w`
        if [ $cc -lt 1 ]
        then
            echo "File $file_name has been read." >> $LOG_FILE
            break;
        else
            echo "$file_name $tmp_line" >> $FILE_TWO
        fi
    done
    the 'break' command causes the while loop to terminate definitively so with every empty inputline the reading stops. The reading of the complete file is already controlled by 'while read tmp_line' which returns false when there's no line left to be read and thus terminates the loop automatically.

    Regards

  3. #3
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Am I missing something here?

    while read will continue reading until the read returns a non-zero exit status, i.e. when there are no more lines to read.

    I'd do away with needless cat and temporary variable assignments too.

    Code:
    while read line
    do
        # do your parsing here, e.g.
        echo "$line"
    
    done < yourInputFile >> yourOutputFile

  4. #4
    Join Date
    Aug 2006
    Location
    The Netherlands
    Posts
    248
    Krisna asked
    Any ideas? Is there anything like 'EOF' in Shell for reading the file till the end?
    not to rewrite his script. That's what you were missing...

    Regards

Posting Permissions

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