Printing with awk Utility

Trying to use the printf with awk to send some data to print.

Here is the statement in my script:

echo "$i $state $q_date $project $title" | awk '{ printf "%-11s%-2s%-7s%-17s%-60s\n", $1, $2, $3, $4, $5 }' >>$tempdir/$Manager.msg

The problem is that I am trying align the fields - which is OK up to $4, but because the variable $title is several words so the utility only sees the first word in $title and prints it. I tried everything like \"$title\" '$title' ${title} nothing works...

By the time awk sees the line, the protecting quotes are already removed. But even if you protected the quotes with backslashes so that awk would receive "My First Report", awk would print "My for $5 because, by default, words are delimited with white space.

You can change your word separation to something else such as commas so that an awk field can contain spaces:

v3="My First Report"
echo $v1,$v2,$v3,$v4 | awk -F, '{print $3}'
 

awk Remove Form Feed

I need to create a awk script to remove 2 form feed marks from the beginning of the file. To make sure it will not print 2 empty pages. I cannot make it work.

The output will be putting all form feed lines in the begining...

-- Begin Script
{
if (FNR<=2) {
if ( substr($0,1,1)=="\f" ) { print substr($0,2) }
}
else { print $0 }
}

----------------------------------------
Change this:
FNR <=2 {sub(/\f+/,"",$0)} ; {print}

See Also
Unix Administrator Career Path

Have a Unix Problem
Do you have a UNIX Question?

Unix Books :-
UNIX Programming, Certification, System Administration, Performance Tuning Reference Books

Return to : - Unix System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.