Operating System - Linux
1832780 Members
3057 Online
110045 Solutions
New Discussion

Re: Limiting characters upto <xyz> columns

 
amit mehta_2
Regular Advisor

Limiting characters upto <xyz> columns

Hi,

Recently i did some code changes in one of the text file.
During the code review,i've been asked to allign the comments
in this file to XYZ columns(say XYZ=40).
Now the problem is that this file is a huge one and
it would be really pathetic if i go ahead and do it manually.
I think there must be a better way of doing it,Can it be done
through a shell script ?

Can this variable(COLUMNS) is used for doing the same?

hpia14:/>set|grep -i columns
COLUMNS=155

4 REPLIES 4
Matti_Kurkela
Honored Contributor

Re: Limiting characters upto <xyz> columns

The COLUMNS variable is not really suitable for this, as it only tells the programs how wide your terminal display is supposed to be. What happens to longer lines depends on the settings of the terminal (or terminal emulator, like xterm). You would then have to capture the text _as displayed by your terminal_. There are simpler ways to do this.

To cut all lines to specified maximum length, you might do something like:

cut -c 1-40 new.txt

This cuts off all lines after the 40th character.

But I'm not sure whether this is really what "align comments to XYZ columns" means. I'd think it would mean moving all comments towards the right to _begin_ at the requested column. A task like this would be called "pretty-printing" or "beautifying" your source code.

For this, you need a program that understands enough of your text file to identify what is a comment and what isn't.

Since you did not specify the programming language (or whatever) your text file is in, it's difficult to find a suitable program for you.

For example, a common tool for C programming language would be "indent". To move all comments to column 40 (or further, if there is more than 40 characters on the left side of the comment) could be done with:
indent -c40 -o newfile.c oldfile.c

Indent is available in the HP-UX Porting Archive:
http://hpux.asknet.de/hppd/hpux/Gnu/indent-2.2.9/

You might want to Google for a suitable
program for your programming language. Good search words are the name of your programming language, "prettyprint" and/or "beautifier".

MK
MK
amit mehta_2
Regular Advisor

Re: Limiting characters upto <xyz> columns

Thanks a lot Matti,

My text file contains shell scripts.
Bill Hassell
Honored Contributor

Re: Limiting characters upto <xyz> columns

Since your file contains shell scripts, theuse of cut to chop off all characters on the right starting at column 41 will destroy the scripts. The command called fold will wrap characters onto the next line starting at a specific column. There is a very nice tool called adjust which will wrap characters past a given column onto the next line without breaking in the middle of words.

However, none of these are suitable for shell scripts. As you mentioned, there are comments that are very long (an error by the script writer) and if you simply break the line at column 40, the next line will not have the requisite # at the beginning of the line. So the script will fail as it tries to run the newline of comments.

But even more difficult to handle will be command lines that are too long and must be split with a special characters (\) to escape the special meaning of the end-of-line. This is a very common situation when a programmer uses the tab character. 8 characters is a massive waste of space on the line. I typically use and indent of 3, sometimes just 2 characters for deep indentation. I am always aware that 80 characters is the industry default screen width so i don't write code beyond that width. Instead, I will break up lines using \ and especially use a line break for every command in a long pipe.

COLUMNS=155 sound like you are using Xwindows and have fallen into the trap of instantly resizable terminal screens. It is nice to drag a new character space with one click but as you have seen, code becomes quite difficult to manage and most certainly becomes a pain to print and distribute for code reviews. You can handle this in the future by using a restricted screen size for your editing window, say COLUMNS=40 (LINES can be any length). Use a separate wide screen for debug and testing.

Sorry, but every long line will have to be processed by hand. NOTE: this will be highly invasive!!! That means that just getting all the lines to fit in 40 columns will require a lot of new lines that are correctly formatted, comments with # and line breaks with \ at meaningful locations. 40 characters is quite unreasonable for scripts. Now if this restriction only applies to comments, the task gets a little easier and less invasive to the code. 40 sounds like a limit imposed so the text will be visible in the back of the room during a code review -- not a reasonable request considering the possibility of creating mistake in the code. Instead, I would download the Microsoft magnfier called ZoomIt:

http://www.microsoft.com/technet/sysinternals/utilities/zoomit.mspx

Now you can zoom in on a few lines and read the text quite easily at the back of the room.


Bill Hassell, sysadmin
Hein van den Heuvel
Honored Contributor

Re: Limiting characters upto <xyz> columns

>> allign the comments

Are those comments to (structured) 'code' or (free format) random 'text'?

As Bill indicates, this may be rather tricky, sacrificing the actual intend for the purpose of readability.

You would have to very clearly define what a comment looks like, how a script can recognize a comment without getting confused by text data which just looks like a comment.

You would have to very clearly identify what to do with lines exceeding the comment start location: Split brute force? Truncate? Recognize words boundaries? Spacing? Ignore? Break the aligment rule?

As Bill implies... what problem are you really trying to solve?
- a presentation problem?
- a permanent record
- a temporary aid?

If you want to pursue automting this, and would appreciate further help, then please reply with a (text!) attachment showing a relevant sample input and desired output.
And you may also want to indicate which tools are at your disposal (shell? perl? awk? python?...)

Hope this helps some,
Hein.