If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Parsing files

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-07-07, 19:46
krishna.reddy krishna.reddy is offline
Registered User
 
Join Date: Jan 2006
Posts: 5
Thumbs up 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
Reply With Quote
  #2 (permalink)  
Old 04-08-07, 05:20
Tyveleyn Tyveleyn is offline
Registered User
 
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
Reply With Quote
  #3 (permalink)  
Old 04-19-07, 15:29
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
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
Reply With Quote
  #4 (permalink)  
Old 04-20-07, 04:02
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
Krisna asked
Quote:
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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On