The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 12:20 PM
Running UNIX commands remotely in Windows box from Unix box – avoid entering password D.kalpana UNIX for Dummies Questions & Answers 1 04-20-2007 05: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 07:57 AM
Unix Sco Open Server, Windows Computers Problem Access Unix Shared Files Help!!!!! haggo Filesystems, Disks and Memory 2 08-23-2006 11:39 AM
Unix History Question: Why are filenames/dirnames case sentsitive in Unix? deckard UNIX for Dummies Questions & Answers 3 03-26-2005 01:59 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-16-2008
guest6 guest6 is offline
Registered User
  
 

Join Date: May 2008
Posts: 8
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 04:31 PM..
  #2 (permalink)  
Old 05-16-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 04:48 PM.. Reason: Note that head reads first n lines every time
  #3 (permalink)  
Old 05-16-2008
guest6 guest6 is offline
Registered User
  
 

Join Date: May 2008
Posts: 8
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
  #4 (permalink)  
Old 05-16-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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'"
  #5 (permalink)  
Old 05-16-2008
guest6 guest6 is offline
Registered User
  
 

Join Date: May 2008
Posts: 8
How do i get rid of the error in the while loop
  #6 (permalink)  
Old 05-16-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Sorry, I post-edited while you were probably writing that comment; scroll back up to see my edited posting.
  #7 (permalink)  
Old 05-16-2008
guest6 guest6 is offline
Registered User
  
 

Join Date: May 2008
Posts: 8
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
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes 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

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



All times are GMT -4. The time now is 01:11 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0