sed in while loop producing arithmetic output

Hi all

Just wondering if someone can help me with this. I'm trying to write a script that processes the output of another file and prints only the lines I want from it.

This is only the second script I have written so please bare with me here. I have referred to the literature and some of the previous posts and I'm sure I have the logic and syntax correct. It computes, but it takes ages and gives me the wrong output.

heres the code:

#Set the line number variables
filelinenumber=1
datalinenumber=8

lines=`sed -n '$=' test1.out`

while [ $datalinenumber -le $lines ]
do

# print the selected lines into the new file mpck2.out
sed -n "${filelinenumber}p" test1.out` > mpck2.out
sed -n "${datalinenumber}p" test1.out > mpck2.out

# increment the line number variables
filelinenumber=`expr $filelinenumber + 11`
datalinenumber=`expr $datalinenumber + 11`

done

Instead of a file full of the program lines I want I'm just getting this
$ cat mpck2.out
average 156956

Sorry, I don't know you well enough to bare with you ... or do you mean bear with me?

It takes ages because you are calling sed many, many times.

Please put code inside

 tags.



#Set the line number variables
filelinenumber=1
datalinenumber=8

lines=`sed -n '$=' test1.out`

[/quote]

[indent]
The usual way to count the number of lines in a file is:

line=$( wc -l < "$FILE" )

Every time you use > mpck2.out, you are truncating the file before writing to it.

Instead, redirect the output of the loop:

while [ $datalinenumber -le $lines ]
do
  # print the selected lines into the new file mpck2.out
  sed -n "${filelinenumber}p" test1.out`
  sed -n "${datalinenumber}p" test1.out

  # increment the line number variables (no need for expr)
  filelinenumber=$(( $filelinenumber + 11 ))
  datalinenumber=$(( $datalinenumber + 11 ))
done > mpck2.out

It would be better done in awk

awk ' BEGIN { fln = 1; dln = 8 }
NR == fln || NR == dln { print }
      { fln += 11; dln += 11 }
' test1.out > mpck2.out

Excellent! It Works. I Appreciate your help with that.

Its very good to see the code work.

I will take a look at implementing the awk solution also if it requires less computation. I did notice that others have been referred to it in quite a few of the other posts.

Cheers

I got the awk code to work too. It's impressively faster than calling sed.

This code didn't quite work on my machine:

awk ' BEGIN { fln = 1; dln = 8 }
NR == fln || NR == dln { print }
      { fln += 11; dln += 11 }
' test1.out > mpck2.out

However this did and it definitely pointed me in the right direction:

awk ' BEGIN { fln = 1; dln = 8 }
      NR == fln {print; fln += 11}
      NR == dln {print; dln += 11}
' test1.out > mpckawk2.out