The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Appending in a file vinay123 Shell Programming and Scripting 1 06-19-2008 03:05 AM
Reading specific contents from a file and appending it to another file dnicky Shell Programming and Scripting 5 10-04-2005 05:45 AM
printing to the screen google UNIX for Advanced & Expert Users 2 10-23-2003 11:19 PM
appending a file chumba UNIX for Dummies Questions & Answers 1 05-24-2001 11:09 AM
Appending out to a file Astudent UNIX for Dummies Questions & Answers 1 10-20-2000 02:17 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 03-17-2009
lazypeterson lazypeterson is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 13
Appending to a file without printing to screen

Code:
#!/bin/csh
awk 'BEGIN     { print "Name           Exam1           Exam2           Exam 3        Total        Grade" }' grades | tee gradesorted
awk '{if ($2+$3+$4<50){grade="F"}else if ($2+$3+$4>49 && $2+$3+$4<65)
{grade="D"}else if ($2+$3+$4>64 && $2+$3+$4<80){grade="C"}
else if ($2+$3+$4>79 && $2+$3+$4<90){grade="B"}else{grade="A"}}
{print $0, "          ", $2+$3+$4, "          ", grade;}' grades | tee gradesorted
What I'm trying to do change both the prints so that what's in the quotations gets appended to the file rather than printed to the screen. I've tried using sed with no success. Can anyone point me in the right direction? Thanks.
  #2 (permalink)  
Old 03-17-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
awk '...stuff...' > gradesorted
  #3 (permalink)  
Old 03-17-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by lazypeterson View Post
Code:
#!/bin/csh

Not that it matters in this instance, but see these pages:
Top Ten Reasons not to use the C shell
Csh problems
Csh Programming Considered Harmful
Quote:
Code:
awk 'BEGIN     { print "Name           Exam1           Exam2           Exam 3        Total        Grade" }' grades | tee gradesorted

Why two awk programs?
Quote:
Code:
awk '{if ($2+$3+$4<50){grade="F"}else if ($2+$3+$4>49 && $2+$3+$4<65)
{grade="D"}else if ($2+$3+$4>64 && $2+$3+$4<80){grade="C"}
else if ($2+$3+$4>79 && $2+$3+$4<90){grade="B"}else{grade="A"}}
{print $0, "          ", $2+$3+$4, "          ", grade;}' grades | tee gradesorted
What I'm trying to do change both the prints so that what's in the quotations gets appended to the file rather than printed to the screen. I've tried using sed with no success. Can anyone point me in the right direction?

Code:
#!/bin/sh

awk 'BEGIN {
   format = "%-15s%-15s%-15s%-15s\n"
   printf format, "Name", "Exam1", "Exam2", "Exam 3", "Total", "Grade"
  }
{
 total = $2 + $3 + $4
      if (total < 50) {grade = "F"}
 else if (total < 65) {grade = "D"}
 else if (total < 80) {grade = "C"}
 else if (total < 90) {grade = "B"}
 else {grade="A"}
}
{printf format, $1, $2, $3, $4, total, grade}' grades > gradesorted
  #4 (permalink)  
Old 03-17-2009
lazypeterson lazypeterson is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 13
I don't know why I was using two awk's...

Thanks for helping clean up my sloppy code. The only problem I'm having, at this point, is sorting the gradessorted file without the header (Name, Exam1, etc.) being on the bottom. Is there a way to exclude lines from a sort?
  #5 (permalink)  
Old 03-17-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361

Pipe the printf through sort:
Code:

awk 'BEGIN {
   format = "%-15s%-15s%-15s%-15s%-15s%-15s\n"
   printf format, "Name", "Exam1", "Exam2", "Exam 3", "Total", "Grade"
  }
{
 total = ( $2 + $3 + $4 ) / 3 # I think you want the average
      if (total < 50) grade = "F"
 else if (total < 65) grade = "D"
 else if (total < 80) grade = "C"
 else if (total < 90) grade = "B"
 else grade="A"

 format = "%-15s%-15s%-15s%-15s%-15d%-15s\n"
 printf format, $1, $2, $3, $4, total, grade | "sort"
} ' grades > gradesorted
  #6 (permalink)  
Old 03-17-2009
lazypeterson lazypeterson is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 13
Marvellous. Thanks a bunch!
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 05:48 PM.


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