sed in while loop producing arithmetic output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed in while loop producing arithmetic output
# 1  
Old 01-28-2009
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
# 2  
Old 01-28-2009
Quote:
Originally Posted by javathecat
This is only the second script I have written so please bare with me here.

Sorry, I don't know you well enough to bare with you ... or do you mean bear with me?
Quote:
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.

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

heres the code:

Please put code inside [code] tags.
Quote:
Code:
#Set the line number variables
filelinenumber=1
datalinenumber=8

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


The usual way to count the number of lines in a file is:
Code:
line=$( wc -l < "$FILE" )

Quote:
Code:
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


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

Instead, redirect the output of the loop:

Code:
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

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

It would be better done in awk

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

# 3  
Old 01-29-2009
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
# 4  
Old 02-04-2009
I got the awk code to work too. It's impressively faster than calling sed.

This code didn't quite work on my machine:
Code:
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:
Code:
awk ' BEGIN { fln = 1; dln = 8 }
      NR == fln {print; fln += 11}
      NR == dln {print; dln += 11}
' test1.out > mpckawk2.out

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing the output of sed using a loop

So I am writing a bash script that will search a file line by line for unix timestamps, store all of the timestamps into an array, then check how many of those timestamps were created within the last hour, and finally increment a counter every time it finds a timestamp created within the last hour.... (6 Replies)
Discussion started by: jsikarin
6 Replies

2. Shell Programming and Scripting

awk script not producing output

Hi, I have a text file with some thousands of rows of the following kind (this will be referred to as the inputFileWithColorsAndNumbers.txt): Blue 6 Red 4 Blue 3 Yellow 4 Red 7 Colors in the left column and a number in the right one for each line. I want to run an awk... (5 Replies)
Discussion started by: Zooma
5 Replies

3. Shell Programming and Scripting

Csh arithmetic not producing the expected value

My original post did not show up properly. I am trying again. I have a simple tsch script that does some basic arithmetic. The calculated value was not producing the result I was expecting. I wrote a sample script to illustrate the things that I tried. #!/bin/tcsh @ count = 43 @... (3 Replies)
Discussion started by: sgualandri
3 Replies

4. Shell Programming and Scripting

awk producing too many fields

Hey guys, awk is putting out too many results, two of each specifically. See below. Code: read CPU_VENDOR_ID <<< $(cat /proc/cpuinfo | grep "vendor_id" | awk -F: '{print $2}') echo $CPU_VENDOR_ID echo Output: AuthenticAMD AuthenticAMD I only want it to print out "AuthenticAMD" once. ... (7 Replies)
Discussion started by: 3therk1ll
7 Replies

5. Shell Programming and Scripting

Performing arithmetic operations on output of `wc -l`

Hi I want to perform arithmetic operations on output of `wc -l`. for example user046@sshell ~ $ ls -l total 0 where "total 0" will increase one line in wc -l filecount=`ls -l | wc -l` here $filecount will be 1 but is should be 0 how to get rid of it ? (1 Reply)
Discussion started by: anandgodse
1 Replies

6. Shell Programming and Scripting

Arithmetic operation between columns using loop variable

Hi I have a file with 3 columns. say, infile: 1 50 68 34 3 23 23 4 56 ------- ------- I want to generate n files from this file using a loop so that 1st column in output file is (column1 of infile/(2*n+2.561)) I am doing like this: for ((i=1; i<=3; i++)) do a=`echo... (3 Replies)
Discussion started by: Surabhi_so_mh
3 Replies

7. Shell Programming and Scripting

Help, while loop and sed not producing desired output

Hello everyone, I need some assistance with what I thought would have been a very simple script. Purpose of Script: Script will parse through a source file and modify (search/replace) certain patterns and output to stdout or a file. Script will utilize a "control file" which will contain... (12 Replies)
Discussion started by: packetjockey
12 Replies

8. Shell Programming and Scripting

Strings to integers in an arithmetic loop

Hi all, Could someone please advise what is the correct syntax for my little script to process a table of values? The table is as follows: 0.002432 20.827656 0.006432 23.120364 0.010432 25.914184 0.014432 20.442655 0.018432 20.015243 0.022432 21.579517 0.026432 18.886874... (9 Replies)
Discussion started by: euval
9 Replies

9. Shell Programming and Scripting

sed or awk for arithmetic task

I have file with this type of format 01.02.09 08:30 bob jill mark 01.04.09 07:00 bob jill mark tom I want to count the names after the date /ime line (01.02.09 08:30) and add that number after the time like this 01.02.09 08:30 3 01.04.09 07:00 4 I don't care about... (6 Replies)
Discussion started by: marcelino
6 Replies

10. Shell Programming and Scripting

Sed subsitution on loop output in shell script

I have a forloop which checks a log for a set of 6 static IP addresses and each IP found is logged to a file which is then mailed to me. After the forloop I always have a text file that may contain up to 6 IP addresses or may contain 0. What I want to do is substitute the IP addresses (if any)... (2 Replies)
Discussion started by: Moxy
2 Replies
Login or Register to Ask a Question