GIDForums  

Go Back   GIDForums > Computer Programming Forums > Miscellaneous Programming Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Sep-2007, 06:18
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 167
dabigmooish will become famous soon enough

Read a file line by line, shell script


I've got a file that is of variable length, it will always have a minimum of two lines but no maximum. The first line contains either ON or OFF. The rest of the lines are numbered and contain the Month Day Time. The file looks like this:

Code:
ON 1 09 04 1030 2 09 04 1100 3 09 04 2315 4 09 05 0700 ...

I'm trying to write some code that will remove the second line of the file and renumber the others. Here is what I had:

Code:
overwrite() { # Variable is a counter used for special situations where all entries # # in the on_off file are deleted on run. Without this variable no new# # entries will be written # RUN=`expr $RUN + 1` echo "ON" >> $ON_OFF_FILE.$RUN # Loop that Creates the temp file while [ $TEMP2 -lt $TLINES ] || [ $TEMP2 -eq $TLINES ] do LINETOWRITE=`grep "$TEMP2 " $ON_OFF_FILE | awk '{print $2,$3,$4}' ` echo "`expr $TEMP2 - 1` $LINETOWRITE" >> $ON_OFF_FILE.$RUN TEMP2=`expr $TEMP2 + 1` done mv $ON_OFF_FILE.$RUN $ON_OFF_FILE }

This works fine as long as the date isn't the smaller then the number of entries in the file. When it is, it causes issues. I was thinking of using the cut command with a "/n" delineator but I can't get that to work. Does anyone have any suggestions as to how to do this?
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead."
-Thomas Paine
www.sullivan-county.com/deism.htm
  #2  
Old 05-Sep-2007, 08:49
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,305
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Read a file line by line, shell script


Quote:
Originally Posted by dabigmooish
I'm trying to write some code that will remove the second line of the file and renumber the others.

Are you always going to write "ON" as the first line of the output file, as your code seems to indicate?

Since you don't indicate any kind of error checking, couldn't you just use something like the following:
Code:
#!/bin/bash # # Replace the first line of the file with "ON" # Skip the second line # Renumber the first field of the remaining lines # INPUTFILE=test.txt OUTPUTFILE=modified.txt echo "ON" > $OUTPUTFILE awk '(NR > 2) {$1=$1-1; print}' $INPUTFILE >> $OUTPUTFILE

(This is just to show how simple the awk stuff can be. Whatever you did to get the input file and output file names is still up to you.)

File test.txt:
Code:
OFF 1 09 04 1030 2 09 04 1100 3 09 04 2315 4 09 05 0700
File ]kbd]modified.txt[/kbd] after running the above script
Code:
ON 1 09 04 1100 2 09 04 2315 3 09 05 0700

On the other hand, If, as you described it, you want to reproduce the first line exactly as it appeared, and then skip the second line and renumber the rest, then the awk part could look like:

Code:
#!/bin/bash # # Reproduce the first line # Skip the second line # Renumber the first field of the remaining lines # INPUTFILE=test.txt OUTPUTFILE=modified.txt awk '(NR == 1){print} (NR > 2) {$1=$1-1; print}' $INPUTFILE > $OUTPUTFILE

File modified.txt after this one:
Code:
OFF 1 09 04 1100 2 09 04 2315 3 09 05 0700

If this doesn't meet your needs, then maybe you could give a complete program specification.

Regards,

Dave
  #3  
Old 05-Sep-2007, 09:41
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 167
dabigmooish will become famous soon enough

Re: Read a file line by line, shell script


Thanks this is what I need. I think I'm still stuck thinking approaching shell programming as if it were C++. I've never seen awk used like that and it never occured to me to try that. Anyway to answer your questions, Yes I will always put ON as the first line, if it's OFF the script won't run anyway. All the error checking is elsewhere. This is but one function of many. Thanks for your help dave.
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead."
-Thomas Paine
www.sullivan-county.com/deism.htm
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
Help! Problems encountered while burning CD's RayDarkness Computer Software Forum - Windows 1 18-Nov-2006 04:54
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 02:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:39.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.