The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Enterprise Unix Roundup: The Ghost of Unix Future - Server Watch iBot UNIX and Linux RSS News 0 12-19-2007 09:20 AM
Running UNIX commands remotely in Windows box from Unix box – avoid entering password D.kalpana UNIX for Dummies Questions & Answers 1 04-20-2007 02:24 AM
FTP script for sending a file from one unix directory to another unix server director raja_1234 Shell Programming and Scripting 1 11-30-2006 04:57 AM
Unix Sco Open Server, Windows Computers Problem Access Unix Shared Files Help!!!!! haggo Filesystems, Disks and Memory 2 08-23-2006 08:39 AM
Unix History Question: Why are filenames/dirnames case sentsitive in Unix? deckard UNIX for Dummies Questions & Answers 3 03-26-2005 10:59 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #8  
Old 05-16-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
You forgot the quotes so we can see if there is any whitespace around those. I would suspect a typo somewhere -- I'm not getting any syntax error from a copy+paste of that script.
Reply With Quote
Forum Sponsor
  #9  
Old 05-16-2008
Registered User
 

Join Date: May 2008
Posts: 8
yeah...my mistake..it worked... i have one question... u suggested to use read.. can u please tell me how to use "read" command.

Is it hard to delete the last 3 three lines in file1.txt using script... i am not sure whether to use script to solve the problem or use c++ program.
Reply With Quote
  #10  
Old 05-17-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
This is not particularly elegant, but at least this demonstrates how to use read.

Code:
# Set IFS to just a newline
IFS='
'
reading=true
while $reading; do

  # Copy file1.txt to a temporary file
  cp file1.txt temp

  for lines in zero one two; do
    if read input; then 
      echo "$input" >>temp
    else
      # Short read -- print a diagnostic to standard error
      echo "$0: reading three lines failed -- abandoning after $lines" >&2
      reading=false
      break
    fi
  done

  # Run external program on temporary file
  externalprogram temp

done <file2.txt
If you'd run head -n 3 inside the loop, with standard input for the loop redirected to come from file2.txt, I suppose that would work, too. The trick is to use redirection to open the file once and then keep on reading without closing the file descriptor. Redirection (with <, or with exec) achieves that for you.

Using a temporary file just seems like better hygiene than continuously butchering the input file, and saves you from having to keep track of how many lines exactly to remove again in case of a short read (what with the possibility of missing newlines at the end and other complications).
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:35 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0