The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
total output from a file created in a while loop Pablo_beezo Shell Programming and Scripting 7 01-29-2009 11:26 AM
While loop using command output... elbombillo Shell Programming and Scripting 6 12-02-2008 04:47 PM
Loop column output handband2 UNIX Desktop for Dummies Questions & Answers 1 11-03-2008 10:46 PM
Sed subsitution on loop output in shell script Moxy Shell Programming and Scripting 2 09-28-2008 05:49 AM
Producing visually pleasant documents from plain text with reStructuredText and rst2a iBot UNIX and Linux RSS News 0 04-29-2008 05:40 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-28-2009
javathecat javathecat is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 5
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 (permalink)  
Old 01-28-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,362
Quote:
Originally Posted by javathecat View Post
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 (permalink)  
Old 01-29-2009
javathecat javathecat is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 5
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 (permalink)  
Old 02-04-2009
javathecat javathecat is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 5
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

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 09:07 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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