LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 06-08-2009, 06:35 PM   #1
vijayrc
LQ Newbie
 
Registered: Jun 2009
Posts: 5

Rep: Reputation: 0
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
 
Old 06-08-2009, 06:39 PM   #2
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 11,141
Blog Entries: 3

Rep: Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406
Hi,

Welcome to LQ!

What about presenting the information or queries too your instructor?
 
Old 06-08-2009, 06:42 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,974
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
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

Last edited by Tinkster; 06-08-2009 at 06:43 PM.
 
Old 06-08-2009, 06:43 PM   #4
vijayrc
LQ Newbie
 
Registered: Jun 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by onebuck View Post
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.
 
Old 06-08-2009, 06:48 PM   #5
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 11,141
Blog Entries: 3

Rep: Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406
Hi,

Sorry! I've seen that example many times. As 'Tink' stated, you do need to simplify.
 
Old 06-08-2009, 06:49 PM   #6
vijayrc
LQ Newbie
 
Registered: Jun 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Tinkster View Post
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
 
Old 06-08-2009, 06:58 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,974
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
A couple of minor mistkes in there, but yes, basically that's it.
 
Old 06-08-2009, 10:54 PM   #8
rikxik
Member
 
Registered: Dec 2007
Posts: 88

Rep: Reputation: 19
Quote:
Originally Posted by vijayrc View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Output Redirection - Trying to output to screen and file? helptonewbie Linux - Newbie 7 03-19-2009 07:05 AM
writing output of a program to a file only when all of it is ready pgb205 Linux - Software 4 12-10-2008 02:01 PM
writing output to file Hockeyfan Linux - General 3 10-24-2005 08:02 PM
Emailing webalizer output benmartins Linux - Newbie 0 03-22-2004 09:37 PM
Log Screen Output to a file sdandeker Linux - Newbie 3 09-17-2003 02:57 AM


All times are GMT -5. The time now is 04:39 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration