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 08: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 03: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 09:59 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-16-2008
Registered User
 

Join Date: May 2008
Posts: 8
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Help in unix

Hi,

I am a beginner in unix shell scripting. I wish to do the following things:

a) Read 3 new lines from a file (file2.txt) and appending them at the end of another file file1.txt.

I wrote the script as follows:

#! /bin/sh

set i = 0
set count =0
count = (wc -l file2.txt)
while(i -le $count)
do
i = i + 3;
head -i file2.txt >> file1.txt
done

I am getting an error in line "count = (wc -l file2.txt)". There is something wrong with my syntax because the script is not working. Please help.

Thanks

Last edited by guest6; 05-16-2008 at 01:31 PM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-16-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,253
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
set is not a useful sh command in this context, and you need to avoid having spaces on either side of the equals signs. Also take care to put spaces where you do need them.

Code:
#! /bin/sh

i=0
count=$(wc -l < file2.txt)
while [ $i -le $count ]
do
   i=`expr $i + 3`
   head -n $i file2.txt >> file1.txt
done
As such, simply head -n 3 file2.txt >>file1.txt should do what you want. Or maybe I misunderstand your problem description. Either way, your loop will read from the beginning of file2.txt on each iteration, which doesn't seem useful.

Last edited by era; 05-16-2008 at 01:48 PM. Reason: Note that head reads first n lines every time
Reply With Quote
  #3 (permalink)  
Old 05-16-2008
Registered User
 

Join Date: May 2008
Posts: 8
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Thanks .. i removed spaces and now that error is gone..

I am getting an error in while loop. The error is

"[: too many arguments"

I am using the while loop because i wish to read first 3 lines from file2.txt and append them in file1.txt. Then run some program and delete the 3 lines appended. Then append the next 3 lines from file2.txt to file1.txt and run the program. I have to do this till all the lines in file2.txt have been read.

Could you suggest a way to delete the last three lines from file.

I appreciate your instant help.

Thanks
Reply With Quote
  #4 (permalink)  
Old 05-16-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,253
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
I would suggest to keep on reading from the file using read instead.

The [ error is probably because you forgot to put in the < where I indicated. It might help to add a statement to print the variables just before the while so you can see what is getting compared.

Code:
echo "Here we are just before the while -- count is '$count' and i is '$i'"
Reply With Quote
  #5 (permalink)  
Old 05-16-2008
Registered User
 

Join Date: May 2008
Posts: 8
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
How do i get rid of the error in the while loop
Reply With Quote
  #6 (permalink)  
Old 05-16-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,253
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Sorry, I post-edited while you were probably writing that comment; scroll back up to see my edited posting.
Reply With Quote
  #7 (permalink)  
Old 05-16-2008
Registered User
 

Join Date: May 2008
Posts: 8
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
I rechecked my code. I added the line you suggested. I am getting the output

Here we are just before the while -- count is 9 and i is 0
Reply With Quote
  #8 (permalink)  
Old 05-16-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,253
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
  #9 (permalink)  
Old 05-16-2008
Registered User
 

Join Date: May 2008
Posts: 8
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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 (permalink)  
Old 05-17-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,253
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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 UNIX.COM
Reply

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
421 service not available, remote server has closed connection ^m autosys awk trim bash eval bash exec bash for loop command copy/move folder in unix couldn't set locale correctly curses.h cut command in unix daemon process export command in unix find grep find mtime find null character in a unix file grep multiple lines grep or grep recursive hp-ux ifconfig inaddr_any inappropriate ioctl for device lynx javascript mailx attachment mget mtime ping port remove first character from string in k shell replace space by comma , perl script scp recursive segmentation fault(coredump) sftp script snoop unix stale nfs file handle syn_sent tar exclude tar extract to folder test: argument expected unix unix .profile unix forum unix forums unix internals unix interview questions unix simulator unix.com vi select all vi substitute vi+substitute+end+of+line+character while loop within while loop shell script


All times are GMT -7. The time now is 05:41 AM.


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

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101