Help in unix


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help in unix
# 1  
Old 05-16-2008
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 05:31 PM..
# 2  
Old 05-16-2008
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 05:48 PM.. Reason: Note that head reads first n lines every time
# 3  
Old 05-16-2008
Thanks .. i removed spaces and now that error is gone..Smilie

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  
Old 05-16-2008
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  
Old 05-16-2008
How do i get rid of the error in the while loop
# 6  
Old 05-16-2008
Sorry, I post-edited while you were probably writing that comment; scroll back up to see my edited posting.
# 7  
Old 05-16-2008
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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

VIP Membership - The UNIX and Linux Forums - Get Your UNIX.COM Email Address Here

We work hard to make The UNIX and Linux Forums one of the best UNIX and Linux knowledge sources on the net. The site is certainly one of the top UNIX and Linux Q&A sites on the web. In order to provide certain members the best quality account services, you can now get some great extra features by... (2 Replies)
Discussion started by: Neo
2 Replies

2. Shell Programming and Scripting

File Transfer from Window server to UNIX and UNIX to UNIX

Dear All, Can someone help to command or program to transfer the file from windows to Unix server and from one unix server to another Unix server in secure way. I would request no samba client. (4 Replies)
Discussion started by: yadavricky
4 Replies

3. UNIX for Dummies Questions & Answers

How does unix system administration, unix programming, unix network programming differ?

How does unix system administration, unix programming, unix network programming differ? Please help. (0 Replies)
Discussion started by: thulasidharan2k
0 Replies

4. Shell Programming and Scripting

Batch job in unix server to move the pdf file from unix to windows.

Hi Experts, I have a requirement where i need to setup a batch job which runs everymonth and move the pdf files from unix server to windows servers. Could some body provide the inputs for this. and also please provide the inputs on how to map the network dirve in the unix like that... (1 Reply)
Discussion started by: ger199901
1 Replies

5. Shell Programming and Scripting

FTP script for sending a file from one unix directory to another unix server director

Hi, My local server is :/usr/abcd/ Remote server is :/Usr/host/test/ I want to send files from local unix directory(All files starting with O_999) to remote host unix directory. Can any body give me the Unix Shell script to do this. One more doubt: Shall we need to change the file... (1 Reply)
Discussion started by: raja_1234
1 Replies

6. UNIX for Advanced & Expert Users

missing Path(in UNIX) when i launch a job on to unix machine using windows SSh

hi i want run an unix application from a windows program/application.i am using SSH(command line version)to log on to a unix machine from windows. the application has to read a configuration file inorder to run. the configuration file .CFG is in bin in my home directory. but the application... (1 Reply)
Discussion started by: megastar
1 Replies

7. UNIX for Dummies Questions & Answers

Unix History Question: Why are filenames/dirnames case sentsitive in Unix?

I tried looking for the answer online and came up with only a few semi-answers as to why file and directory names are case sensitive in Unix. Right off the bat, I'll say this doesn't bother me. But I run into tons of Windows and OpenVMS admins in my day job who go batty when they have to deal... (3 Replies)
Discussion started by: deckard
3 Replies

8. UNIX for Dummies Questions & Answers

UNIX problem? Unix programm runs windows 2000 CPU over 100%

Okee problems...!! What is happening: Unix server with some programms, workstations are windows 2000, the workstations work good but when you start a programm on the Unix server the CPU of the workstations go to 100% usage resulting that the system gets very slow. The programm well its running so... (2 Replies)
Discussion started by: zerocool
2 Replies
Login or Register to Ask a Question