LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-02-2007, 03:10 PM   #1
methodtwo
Member
 
Registered: May 2007
Posts: 146

Rep: Reputation: 18
awk quandry


Hi I'm a programming newbie and today i was reading "the UNIX programming environment" (Kernigan and pike)and they
set a problem which has got me stumpted.The problem is related to an awk script that folds lines of more than 80 characters.The problem is that you have to modify the awk script(Which i will display below)so that it will fold your lines at blanks or tabs rather than splitting a word in the fold process.So here's the program:

sed 's/<tab>/ /g' $* |
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 out a modified version or give me some suggestions.Thankx in advance.
 
Old 07-02-2007, 09:30 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Sounds like an excellent book for a college course.

What class are you taking?
 
Old 07-04-2007, 04:38 PM   #3
methodtwo
Member
 
Registered: May 2007
Posts: 146

Original Poster
Rep: Reputation: 18
This isn't related to any course I'm taking it's just something I'm doing
at home.
 
Old 07-04-2007, 05:06 PM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
It is difficult reading this code because you didn't put it inside a CODE block.

The line blank = blank " " doesn't look right.

I thought I had an idea of locating the last space before N and print the line like:
Code:
printf "%s\\\n", substr($0,i,m-1)
but when you take tabs into consideration, things are complicated. What do you use for a tabstop width? I don't think that length($0) takes tabs into consideration either so it doesn't look like a simple adjustment of the loop.

Quote:
Originally Posted by Gawk: Effective Awk Programming
• Print the length of the longest line in ‘data’:
expand data | awk ’{ if (x < length()) x = length() }
END { print "maximum line length is " x }’
The input is processed by the expand utility to change tabs into spaces, so the widths
compared are actually the right-margin columns.
They pre-expanded the text before processing with awk.

I created a pdf version of the info manual from the texinfo source in the source gawk package. I highly recommend you do the same.

Last edited by jschiwal; 07-04-2007 at 05:30 PM.
 
Old 07-05-2007, 05:02 PM   #5
methodtwo
Member
 
Registered: May 2007
Posts: 146

Original Poster
Rep: Reputation: 18
thanks for your replies.How could i locate the last space before N?.
 
  


Reply



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
.htaccess quandry slybob Linux - Server 6 02-05-2007 09:26 AM
teaching technology quandry - Advice appreciated! Mohtek General 2 11-01-2006 04:03 PM
URGENT: File Permission Quandry naesyllek Linux - Enterprise 2 08-27-2005 05:07 AM
IPtables Quandry caps_phisto Linux - Security 1 04-18-2005 01:19 AM
Dual Boot Modem Quandry james91911 Linspire/Freespire 0 12-27-2004 05:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:06 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration