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

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.

#! /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.

Thanks .. i removed spaces and now that error is gone..:slight_smile:

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

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.

echo "Here we are just before the while -- count is '$count' and i is '$i'"

How do i get rid of the error in the while loop

Sorry, I post-edited while you were probably writing that comment; scroll back up to see my edited posting.

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

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.

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.

This is not particularly elegant, but at least this demonstrates how to use read.

# 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).