Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to change awk fold utility

Status
Not open for further replies.

methodtwo

Technical User
Jul 5, 2007
2
GB
The awk program that i will include below is an implmentation of the unix fold command.The program currently folds lines of input that are more than N characters.I want to modify it so that it will fold lines only on blanks(so that it won't split words when folding the input lines).So here's the unmodified source:
code:
sed 's/<tab>/ /g' $* | #substitute a tab for 8 spaces
awk '
BEGIN {
N = 80
for(i=1;i<=N; i++)
blanks = blanks " "
}
{ if((n = length($0)) <= N)
print
else {
for(i = 1; n > N; n -= N) {
printf "%s\\\n", substr($0, i, N)
i += N;
}
printf "%s%s\n", substr(blanks, 1, N-n), substr($0, i)
}
} '

So please could someone kindly type a modified version that will not split words when folding or give me some suggestions.Oh and no this is not part of a college course etc.Thankx in advance
 
fold -w N [red]-s[/red]

should do nicely I think.


HTH,

p5wizard
 
Furthermore the fold command handles the tab stops.
FYI your sed command substitutes a tab for 8 spaces instead of the number of spaces to the next tab stop.(use pr -te instead)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top