LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Writing Screen output to a file and emailing (http://www.linuxquestions.org/questions/linux-newbie-8/writing-screen-output-to-a-file-and-emailing-731520/)

vijayrc 06-08-2009 06:35 PM

Writing Screen output to a file and emailing
 
Hi,

I have a file with the contents as follows

File_Name_Size.txt
1000 FileA
2000 FileB
3000 FileC

where the first field is the size of the file in bytes; while the second field is the filename.

when I do the following;
awk 'BEGIN {printf ("File Totals")}
{ total = total +1}
{printf "%20s,%9d\n", $2, $1 }
END {printf "%20s,%9d\n", "Total File size is " $total}
File_Name_Size.txt

I get the following:
File Totals
FileA 1000
FileB 2000
FileC 3000
Total File Size is 6000

I want this to be written to a file called File_Name_Size_Total.txt
I'm struggling to redirect the printf to this file w/ all syntax error. I'm a newbie and any help would be appreciated on modifying the existing awk statement to write to the file in the format I desire; or any other way to achieve this..

I tried the following:
echo `awk 'BEGIN {printf ("File Totals")}
{ total = total +1}
{printf "%20s,%9d\n", $2, $1 }
END {printf "%20s,%9d\n", "Total File size is " $total}
File_Name_Size.txt` >> File_Name_Size_Total.txt

Results are like this
File Totals FileA 1000 FileB 2000 FileC 3000 Total File Size is 6000

Thanks a bunch

onebuck 06-08-2009 06:39 PM

Hi,

Welcome to LQ!

What about presenting the information or queries too your instructor?

Tinkster 06-08-2009 06:42 PM

Hi, welcome to LQ!

Your problem is that you're being too complicated :)

Give the echo a miss.
Code:

awk 'BEGIN {printf ("File Totals\n")} { total = total +1} {printf "%20s,%9d\n", $2, $1 } END {printf "%20s,%9d\n", "Total File size is " $total}' File_Name_Size.txt >> File_Name_Size_Total.txt


Cheers,
Tink

vijayrc 06-08-2009 06:43 PM

Quote:

Originally Posted by onebuck (Post 3567383)
Hi,

Welcome to LQ!

What about presenting the information or queries too your instructor?


The illustration of my example might sound from a Linux class; but this is the scenario i am working at my workplace and needed some guidance.

onebuck 06-08-2009 06:48 PM

Hi,

Sorry! I've seen that example many times. As 'Tink' stated, you do need to simplify.

vijayrc 06-08-2009 06:49 PM

Quote:

Originally Posted by Tinkster (Post 3567385)
Hi, welcome to LQ!

Your problem is that you're being too complicated :)

Give the echo a miss.
Code:

awk 'BEGIN {printf ("File Totals\n")} { total = total +1} {printf "%20s,%9d\n", $2, $1 } END {printf "%20s,%9d\n", "Total File size is " $total}' File_Name_Size.txt >> File_Name_Size_Total.txt


Cheers,
Tink


I'll try this tomorrow..Thanks for your input.
So this is what I should try to get the output as I needed ? :)

awk 'BEGIN {printf ("File Totals")}
{ total = total +1}
{printf "%20s,%9d\n", $2, $1 }
END {printf "%20s,%9d\n", "Total File size is " $total}
File_Name_Size.txt >> File_Name_Size_Total.txt
File Totals
FileA 1000
FileB 2000
FileC 3000
Total File Size is 6000

Tinkster 06-08-2009 06:58 PM

A couple of minor mistkes in there, but yes, basically that's it.

rikxik 06-08-2009 10:54 PM

Quote:

Originally Posted by vijayrc (Post 3567392)
I'll try this tomorrow..Thanks for your input.
So this is what I should try to get the output as I needed ? :)

awk 'BEGIN {printf ("File Totals")}
{ total = total +1}
{printf "%20s,%9d\n", $2, $1 }
END {printf "%20s,%9d\n", "Total File size is " $total}
File_Name_Size.txt >> File_Name_Size_Total.txt
File Totals
FileA 1000
FileB 2000
FileC 3000
Total File Size is 6000

Its not supposed to work in above form. This would:

Code:

awk 'BEGIN {printf ("File Totals")}
{ total = total + $1}
{printf "%20s,%9d\n", $2, $1 }
END {printf "%20s,%9d\n", "Total File size is ", total}' File_Name_Size.txt >> File_Name_Size_Total.txt



All times are GMT -5. The time now is 12:14 PM.