Click to See Complete Forum and Search --> : Mail script


darshephard
09-12-2007, 09:16 AM
I am a newbie to the Linux world so sorry for the basic question. I am trying email a file using postfix to my inbox. The problem comes from inserting the text file as the body of the email. If I use the mail command only I get an unformatted chunck of text.

What I need is a \n (newline) at the end of each line of text in the file. I accomplished the task using awk, but wondered if there was a better way using something different. My working command is listed below. Thanks in advance for any input.

awk 'sub("$", "\n")' /tmp/testfile > /tmp/testfile.txt; mail -s "testfile in body" darshephard@help.com < /tmp/testfile.txt

flukshun
09-13-2007, 10:41 PM
doesn't $ match a newline? or maybe it matches \r as well, and thats what the initial file is using instead of \r? just doesnt make sense that you can replace $ when there's nothing there to differentiate the lines to begin with..

in any case, i think sed is more suited for substitutions. try this instead of the awk command:

sed -i 's/$/\n/g' testfile

or if you dont want to modify the original file, just omit the -i and redirect it to a newfile. but theres nothing wrong with using awk, if it works just stick with it. good to be aware of sed's usefulness though.

hotcold
09-14-2007, 01:10 PM
Hi.

If the problem is DOS-Mac-like end-of-lines, \r, or embedded returns, as opposed to *nix end-of-lines, \n, then there are a number of utilities available, but it would be hard to beat:
tr '\015' '\012' <input-file >output-file
for brevity when \r is the only character you need transformed. Normal DOS are \r\n (CR-LF). See man tr for details ... cheers, hotcold

See also: http://en.wikipedia.org/wiki/Newline

( edit 1: extra information )