Appending to a file without printing to screen


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Appending to a file without printing to screen
# 1  
Old 03-17-2009
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  
Old 03-17-2009
awk '...stuff...' > gradesorted
# 3  
Old 03-17-2009
Quote:
Originally Posted by lazypeterson
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  
Old 03-17-2009
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  
Old 03-17-2009

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  
Old 03-17-2009
Marvellous. Thanks a bunch!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Appending content of a file to another file before a specific character

Hi there, i've got a file with this content $ cat file1 Matt Mar The other file has the same number of lines with this content: $ cat file2 20404=767294 23450=32427 is there a way with either using sed, awk or paste to insert the content of file1 before the "=" character? So... (3 Replies)
Discussion started by: nms
3 Replies

2. UNIX for Dummies Questions & Answers

Accidentally made a screen within a screen - how to move it up one level?

I made a screen within a screen. Is there a way to move the inner screen up one level so that it is at the same level as the first screen running from the shell? (2 Replies)
Discussion started by: phpchick
2 Replies

3. Shell Programming and Scripting

Removing part of a file name and appending into a single file

I have two files like ABC_DEF_yyyyymmdd_hhmiss_XXX.txt and ABC_DEF_yyyyymmdd_hhmiss_YYY.txt. The date part is going to be changing everytime. How do i remove this date part of the file and create a single file like ABC_DEF_XXX.txt. (8 Replies)
Discussion started by: varlax
8 Replies

4. Red Hat

command line tool to disable screen lock and/or screen saver

Hi, I have a simple question : how to disable screen lock and/or sreen saver with command line with RHEL5.4 ? (1 Reply)
Discussion started by: albator1932
1 Replies

5. Shell Programming and Scripting

printing messages on the screen when background process is going on!

hello all, In my script i have written some messages which should come in screen while some background process is going on.. Like for example: if i want to add 2 numbers a and b ...when the addition is going on in the background i should get "ADDING TWO NUMBERS>>PLEASE WAIT " message on... (3 Replies)
Discussion started by: smarty86
3 Replies

6. Shell Programming and Scripting

PERL - printing a hash of hashes to screen

Hi there I have a hash of hashes made up of the following data bge0|100|half|10.36.100.21 bge1|1000|full|10.36.100.22 bge2|1000|full|10.36.100.23 which when i turn into a hash, would look like this inside the system bge0 -> nic_speed -> 100 nic_duplex -> half ... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

7. Shell Programming and Scripting

searching a log file and appending to a .txt file

I'm new to shell scripting and am writing a script to help me log the free memory and hd space on a server. As of now, the script just runs 'df -h' and appends the output to a file and then runs 'top' and appends the output to a log file. What I want to do, is have the script also search the... (3 Replies)
Discussion started by: enator45
3 Replies

8. Shell Programming and Scripting

appending to sed output of one file into the middle of file

hi, i have a file file1 file2 ----------- ----------------- aa bbb ccc 111 1111 1111 ddd eee fff 222 3333 4444 ggg hhh... (5 Replies)
Discussion started by: go4desperado
5 Replies

9. Shell Programming and Scripting

Reading specific contents from a file and appending it to another file

Hi, I need to write a shell script (ksh) to read contents starting at a specific location from one file and append the contents at specific location in another file. Please find below the contents of the source file that I need to read the contents from, File 1 -----# more... (5 Replies)
Discussion started by: dnicky
5 Replies

10. UNIX for Advanced & Expert Users

printing to the screen

HPUX 11.0.X / Korn Shell Hope this makes sense. I have a little function to place things on the screen using the column and row parameters. Question is, how can I be sure of where the current cursor postion is so that if I print something to the upper right hand corner of the screen, I can... (2 Replies)
Discussion started by: google
2 Replies
Login or Register to Ask a Question